自定义DaoAuthenticationProvider没有向自定义AuthenticationFailureHandler

时间:2015-07-08 12:51:48

标签: java spring spring-security

在我的应用程序中,我使用Spring Security,并尝试自定义它......

我有一个职业DaoAuthenticationProvider

@Component("authenticationProvider")
public class LimitLoginAuthenticationProvider extends DaoAuthenticationProvider {
    ...
    @Override
    public Authentication authenticate(Authentication pAuthentication)
            throws AuthenticationException {
        if (StringUtils.isBlank(pAuthentication.getName())) {
            throw new UsernameNotFoundException("Login is required");
        }
        if (StringUtils.isBlank(pAuthentication.getCredentials().toString())) {
            throw new AuthenticationCredentialsNotFoundException(
                    "Password is required");
        }
        ...
    }
}

和一个客户AuthenticationFailureHandler

@Component("authenticationFailureHandler")
public class MyAuthenticationFailureHandler implements
        AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest pRequest,
            HttpServletResponse pResponse,
            AuthenticationException pAuthenticationException)
            throws IOException, ServletException {
        pAuthenticationException.getMessage(); // -> Bad credentials
    }
}

我的问题是,当我提交表单没有登录登录但没有密码时,我总是收到消息"凭据错误&# 34; (来自BadCredentialsException)而不是我的自定义消息。为什么?

PS:例外情况是我的自定义DaoAuthenticationProvider

1 个答案:

答案 0 :(得分:0)

就像之前说的@ tobad357一样,您必须在authenticate()的{​​{1}}方法中将LimitLoginAuthenticationProvider.class设置为false。