首页 新闻 会员 周边

spring security 如何捕捉 实现AuthenticationProvider接口类中 抛出的异常?

0
悬赏园豆:50 [已解决问题] 解决于 2018-06-21 16:38

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异常则无法捕捉,会继续下面的账号密码验证。
iisRun的主页 iisRun | 初学一级 | 园豆:147
提问于:2018-06-18 17:35
< >
分享
最佳答案
0

ExceptionTranslationFilter应该是会捕捉到AccessDeniedException和AuthenticationException的,说说我自己项目中如何使用spring security 做验证码校验的,我们是自定义了一个类,它继承UsernamePasswordAuthenticationFilter,然后重写了attemptAuthentication这个方法,我们是在这里面做的验证码校验的。

收获园豆:50
让我发会呆 | 老鸟四级 |园豆:2929 | 2018-06-19 09:34

谢谢你的回答,刚接触spring security还有很多不懂的,我现在也采用了你说的这个方式,至于我提的这个问题,我调试到结果是在ProviderManager中已经被catch掉了,然后赋值给了lastException,之后后面的一些条件没有符合 就没有在处理这个异常了。

iisRun | 园豆:147 (初学一级) | 2018-06-21 16:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册