spring security 如何捕捉AuthenticationProvider接口类中的异常

spring boot 集成了spring security 在做验证码的时候用了AuthenticationProvider,在AuthenticationProvider实现中进行了验证码的校验

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    CaptchaWebAuthenticationDetails authenticationDetails= (CaptchaWebAuthenticationDetails) authentication.getDetails();
    String inputCaptcha =   authenticationDetails.getInputCaptcha();
    String sessionCaptcha   =authenticationDetails.getSessionCaptcha();
    if (!inputCaptcha.equals(sessionCaptcha)){
        throw new CaptchaException("erro");
    }
    return null;
}
但是 throw new CaptchaException("erro");当验证码错误的时候这个异常却无法被全局异常处理捕捉到,浏览器直接抛出了

Whitelabel Error Page,如何才能让这个异常被全局异常处理捕捉到,求解?

CaptchaException 继承了RuntimeException 如果继承AuthenticationException异常则无法捕捉,会继续下面的账号密码验证。


暮色呼如
浏览 2217回答 1
1回答

胡子哥哥

ExceptionTranslationFilter应该是会捕捉到AccessDeniedException和AuthenticationException的,说说我自己项目中如何使用spring security 做验证码校验的,我们是自定义了一个类,它继承UsernamePasswordAuthenticationFilter,然后重写了attemptAuthentication这个方法,我们是在这里面做的验证码校验的。
打开App,查看更多内容
随时随地看视频慕课网APP