Java UsernameNotFoundException的处理程序&;春季oauth中的不良凭证例外

Java UsernameNotFoundException的处理程序&;春季oauth中的不良凭证例外,java,spring,spring-mvc,spring-security,spring-security-oauth2,Java,Spring,Spring Mvc,Spring Security,Spring Security Oauth2,我正在尝试为UsernameNotFoundException和BadCredentialsException设置一个处理程序,用于密码授权oauth流(Spring Outh)。处理程序的用途是每当抛出这些异常中的任何一个时,在数据库中增加一个计数器 我不确定这个处理程序需要设置在什么位置 <http pattern="/oauth/token" create-session="stateless" authentication-manager-ref="authenticatio

我正在尝试为UsernameNotFoundException和BadCredentialsException设置一个处理程序,用于密码授权oauth流(Spring Outh)。处理程序的用途是每当抛出这些异常中的任何一个时,在数据库中增加一个计数器

我不确定这个处理程序需要设置在什么位置

 <http pattern="/oauth/token" create-session="stateless"   authentication-manager-ref="authenticationManager" xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/oauth/token" access="ROLE_USER"  />
    <anonymous enabled="false" />
    <http-basic entry-point-ref="clientAuthenticationEntryPoint"  />

    <custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />

    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>


<!-- The OAuth2 protected resources are separated out into their own block so we can deal with authorization and error handling 
    separately. This isn't mandatory, but it makes it easier to control the behaviour. -->
<http pattern="/public/**" create-session="stateless" entry-point-ref="oauthAuthenticationEntryPoint"
    access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security">
    <anonymous enabled="false" />
    <intercept-url pattern="/public/registration" access="ROLE_USER,SCOPE_READ"  />
    <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>

<http use-expressions="true" create-session="stateless" entry-point-ref="oauthAuthenticationEntryPoint">
    <intercept-url pattern="/public/registration/activation/**" access="permitAll" />
</http>

<authentication-manager alias="authenticationManager"  xmlns="http://www.springframework.org/schema/security">
    <sec:authentication-provider user-service-ref="clientDetailsUserService" />
    <sec:authentication-provider ref="daoProvider">
    </sec:authentication-provider>
</authentication-manager>


<beans:bean id="customUserDetailService" class="com.cointraders.api.services.UserDetailsServiceImpl" />


<beans:bean id="daoProvider" class="com.cointraders.api.daoauthproviders.CustomDaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="customUserDetailService"/>
    <beans:property name="passwordEncoder" ref="passwordEncoder" />
</beans:bean>

<beans:bean id="clientDetails" class="org.springframework.security.oauth2.provider.JdbcClientDetailsService">
    <beans:constructor-arg ref="dataSource" />
</beans:bean>

<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans">
    <beans:constructor-arg>
        <beans:list>
            <beans:bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" />
            <beans:bean class="org.springframework.security.access.vote.RoleVoter" />
            <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter" />
        </beans:list>
    </beans:constructor-arg>
</beans:bean>   

<oauth:authorization-server  client-details-service-ref="clientDetails" token-services-ref="tokenServices">
    <oauth:refresh-token />
    <oauth:client-credentials/>
    <oauth:custom-grant token-granter-ref="randomTokenGrant" />
</oauth:authorization-server>


AuthenticationManager
是一个非常简单的界面。我认为任何人都不需要帮助实现这一点。授权服务器配置DSL有明确的点,您可以在其中插入一个
AuthenticationManager
(例如Java中的
AuthorizationServerEndpointsConfigure
,如下所示:).

查看
@ExceptionHandler
注释
@ExceptionHandler
将不起作用,因为错误是从筛选器而不是从
DispatcherServlet
中抛出的。实现一个
AuthenticationFailureHandler
,并将其注册到过滤器中。或者对筛选器进行子类化并覆盖
未成功身份验证
。实际上,如果这些异常与密码授予相关,则不应从筛选器中抛出这些异常。捕获它们的最佳方法是向令牌授予者提供您自己的
AuthenticationManager
。@DaveSyer如何扩展AuthenticationManager(扩展什么类)的示例?另外,如何在bean配置中连接它?感谢Hanks@Dave Syer的回复。authenticationManager的默认实现是什么?希望查看源代码以确保遵循正确的设计模式和逻辑。此外,我查看了授权服务器DSL,没有看到任何与authenticationManager相关的属性。此外,我还调整了上面的问题,以包括我的bean片段。您甚至没有在
中启用密码授予。如果你这样做了,你会发现你可以在授权者中设置一个身份验证管理器。是的,我知道,randomTokenGrant自定义授权类型的工作方式与密码授权相同(扩展了它,只添加了一些自定义代码)。我提到密码授予类型的原因是为了简单。好的,这一切都在您的自定义授予器中由您控制。有什么问题吗?