AuthenticationException异常详解
来源:互联网 发布:淘宝上大牌瑕疵化妆品 编辑:程序博客网 时间:2024/06/06 17:25
这个异常是在登录的时候出现错误时抛出的异常,比如账户锁定,证书失效等,先来看下AuthenticationException常用的的子类:
UsernameNotFoundException 用户找不到
BadCredentialsException 坏的凭据
AccountStatusException 用户状态异常它包含如下子类
AccountExpiredException 账户过期
LockedException账户锁定
DisabledException 账户不可用
CredentialsExpiredException 证书过期
常见的异常就这几个,还有很多不再一一赘述,仅仅为了展示,对后续的阅读没有什么影响
用户登录验证的过滤器是UsernamePasswordAuthenticationFilter,它继承自AbstractAuthenticationProcessingFilter。
今天研究的是看Spring如何处理AuthenticationException这个异常的,异常的处理一般都是doFilter中处理的,所以首先看AbstractAuthenticationProcessingFilter的doFilter中的代码
从这段代码中我们看到Spring将异常捕获后交给了unsuccessfulAuthentication这个方法来处理(InternalAuthenticationServiceException也是AuthenticationException异常的子类,这是一个内部认证服务异常)unsuccessfulAuthentication又交给了failureHandler(AuthenticationFailureHandler)来处理,然后追踪failureHandler
发现最终是由SimpleUrlAuthenticationFailureHandler这个类的onAuthenticationFailure方法来处理的,打开这个类,有两个核心方法
从onAuthenticationFailure中我们可以看出,如果没有设置defaultFailureUrl就发出401错误。(SC_UNAUTHORIZED是一个常量值是401),如果设置了defaultFailureUrl,调用saveException方法后跳转到defaultFailureUrl页面。
forwardToDestination是是否是服务器跳转,默认使用重定向即客户端跳转。
saveException字面意思是保存异常信息,其实就是把异常信息写入request或者Session中。
如果使用服务器跳转则写入request,客户端跳转则写入Session。
而key是WebAttributes.AUTHENTICATION_EXCEPTION打开WebAttributes找到这个常量
发现其值为SPRING_SECURITY_LAST_EXCEPTION,这样的话我们就可以在页面上通过el表达式来获取到这个异常了注意:saveException保存的是Session对象所以直接使用 ${SPRING_SECURITY_LAST_EXCEPTION}是获取不到异常信息的,需要使用${SPRING_SECURITY_LAST_EXCEPTION.message}
- AuthenticationException异常详解
- Spring Security教程外篇(1)---- AuthenticationException异常详解
- Spring Security教程外篇(1)---- AuthenticationException异常详解
- Spring Security教程外篇(1)---- AuthenticationException异常详解
- Spring Security教程(1)---- AuthenticationException异常详解
- Spring Security教程外篇(1)---- AuthenticationException异常详解
- Shiro AuthenticationException 身份认证异常
- AuthenticationException
- spring security抛出AuthenticationException异常的原因
- spring security抛出AuthenticationException异常的原因
- AuthenticationException 常见子类
- 异常详解
- 异常详解
- Shiro:org.apache.shiro.authc.AuthenticationException
- 其他错误:org.apache.shiro.authc.AuthenticationException
- Shiro:org.apache.shiro.authc.AuthenticationException
- java异常处理详解
- java常见异常详解
- D. My pretty girl Noora(Round422)
- Android自定义View——从零开始实现圆形进度条
- 把一个数据库中的表数据导入另一个数据库中特定的表
- Java项目中Class:xxx
- Andrew Ng's deeplearning Course1Week1 Nerual Networks and Deep Learning(神经网络和深度学习)
- AuthenticationException异常详解
- 辨析C与C++中的&符号的区别
- SpringMVC Model、ModelMap和ModelAndView的区别和用法
- 【面向JS--函数】
- NetBeans用maven构建HTTP服务器 1
- aspectj实现aop的两种操作
- python爬虫实战笔记---以轮子哥为起点Scrapy爬取知乎用户信息
- 「原创」马云又嗨啦,云栖音乐节与李健同台飙歌,把健哥都带跑调了
- AES加密解密