Spring Security源码分析八:Spring Security 退出
为什么80%的码农都做不了架构师?>>>
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
退出原理
- 清除
Cookie
- 清除当前用户的
remember-me
记录 - 使当前
session
失效 - 清空当前的
SecurityContext
- 重定向到登录界面
Spring Security
的退出请求(默认为/logout
)由LogoutFilter过滤器拦截处理。
退出的实现
- 主页中添加退出链接
<a href="/signOut">退出</a>
- 配置MerryyouSecurityConfig
.......and().logout().logoutUrl("/signOut")//自定义退出的地址.logoutSuccessUrl("/register")//退出之后跳转到注册页面.deleteCookies("JSESSIONID")//删除当前的JSESSIONID.and()
......
效果如下
源码分析
LogoutFilter#doFilter
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;//#1.匹配到/logout请求if (requiresLogout(request, response)) {Authentication auth = SecurityContextHolder.getContext().getAuthentication();if (logger.isDebugEnabled()) {logger.debug("Logging out user '" + auth+ "' and transferring to logout destination");}//#2.处理1-4步this.handler.logout(request, response, auth);//#3.重定向到注册界面logoutSuccessHandler.onLogoutSuccess(request, response, auth);return;}chain.doFilter(request, response);}
- 匹配当前拦截的请求
- 处理 清空
Cookie
、remember-me
、session
和SecurityContext
- 重定向到登录界面
handler
CookieClearingLogoutHandler
清空Cookie
PersistentTokenBasedRememberMeServices
清空remember-me
SecurityContextLogoutHandler
使当前session
无效,清空当前的SecurityContext
CookieClearingLogoutHandler#logout
public void logout(HttpServletRequest request, HttpServletResponse response,Authentication authentication) {for (String cookieName : cookiesToClear) {//# 1.Cookie置为nullCookie cookie = new Cookie(cookieName, null);String cookiePath = request.getContextPath();if (!StringUtils.hasLength(cookiePath)) {cookiePath = "/";}cookie.setPath(cookiePath);cookie.setMaxAge(0);response.addCookie(cookie);}}
Cookie
置为null
PersistentTokenBasedRememberMeServices#logout
public void logout(HttpServletRequest request, HttpServletResponse response,Authentication authentication) {super.logout(request, response, authentication);if (authentication != null) {//#1.清空persistent_logins表中记录tokenRepository.removeUserTokens(authentication.getName());}}
- 清空persistent_logins表中记录
SecurityContextLogoutHandler#logout
public void logout(HttpServletRequest request, HttpServletResponse response,Authentication authentication) {Assert.notNull(request, "HttpServletRequest required");if (invalidateHttpSession) {HttpSession session = request.getSession(false);if (session != null) {logger.debug("Invalidating session: " + session.getId());//#1.使当前session失效session.invalidate();}}if (clearAuthentication) {SecurityContext context = SecurityContextHolder.getContext();//#2.清空当前的`SecurityContext`context.setAuthentication(null);}SecurityContextHolder.clearContext();}
- 使当前session失效
- 清空当前的
SecurityContext
AbstractAuthenticationTargetUrlRequestHandler#handle
protected void handle(HttpServletRequest request, HttpServletResponse response,Authentication authentication) throws IOException, ServletException {//#1.获取配置的跳转地址String targetUrl = determineTargetUrl(request, response);if (response.isCommitted()) {logger.debug("Response has already been committed. Unable to redirect to "+ targetUrl);return;}//#2.跳转请求redirectStrategy.sendRedirect(request, response, targetUrl);}
- 获取配置的跳转地址
- 跳转请求
代码下载
从我的 github 中下载,https://github.com/longfeizheng/logback
转载于:https://my.oschina.net/merryyou/blog/1609246
Spring Security源码分析八:Spring Security 退出相关推荐
- Spring Security 源码分析:Spring Security 授权过程
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring I ...
- Spring Ioc 源码分析(一)--Spring Ioc容器的加载
1.目标:熟练使用spring,并分析其源码,了解其中的思想.这篇主要介绍spring ioc 容器的加载 2.前提条件:会使用debug 3.源码分析方法:Intellj idea debug 模式 ...
- Spring Cloud源码分析(二)Ribbon(续)
因文章长度限制,故分为两篇.上一篇:<Spring Cloud源码分析(二)Ribbon> 负载均衡策略 通过上一篇对Ribbon的源码解读,我们已经对Ribbon实现的负载均衡器以及其中 ...
- Spring AOP 源码分析 - 拦截器链的执行过程
1.简介 本篇文章是 AOP 源码分析系列文章的最后一篇文章,在前面的两篇文章中,我分别介绍了 Spring AOP 是如何为目标 bean 筛选合适的通知器,以及如何创建代理对象的过程.现在我们的得 ...
- Spring AOP 源码分析 - 创建代理对象
1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...
- Spring AOP 源码分析 - 筛选合适的通知器
1.简介 从本篇文章开始,我将会对 Spring AOP 部分的源码进行分析.本文是 Spring AOP 源码分析系列文章的第二篇,本文主要分析 Spring AOP 是如何为目标 bean 筛选出 ...
- spring AOP源码分析(一)
spring AOP源码分析(一) 对于springAOP的源码分析,我打算分三部分来讲解:1.配置文件的解析,解析为BeanDefination和其他信息然后注册到BeanFactory中:2.为目 ...
- spring源码分析第六天------spring经典面试问题
spring源码分析第六天------spring经典面试问题 1.Spring5 新特性及应用举例 2.Spring 经典的面试问题 a.什么是 Spring 框架?Spring 框架有哪些主要模块 ...
- 一步一步手绘Spring AOP运行时序图(Spring AOP 源码分析)
相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...
最新文章
- dhcp只能分配与路由器相同网段么_dhcp工作原理
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database
- 【华为云技术分享】STM32L476移植华为LiteOS系列教程---Kconfig 6
- 字符串处理 BestCoder Round #43 1001 pog loves szh I
- JVM学习笔记四_垃圾收集器与内存分配策略
- php想做一个无刷新弹窗,php+ajax实现无刷新的新闻留言系统
- 2022新阅读:nmn有用吗?nmn注意事项有哪些?
- VirtualBox+CentOS6.5安装增强功能包 - Building the main Guest Additions module [失败]
- zend抽象语法树AST流程解析
- 颜值是第一生产力 - Windows Terminal
- 生命是一个苦旅,我们都在路上
- 算法之算法的时间复杂度
- 怎么制作九宫格切图?这两个方法非常简单
- Signal and SIGIO
- 点击按钮随机更换页面背景颜色
- 冥土追魂(暴力枚举)
- linux查询进程号是否存在启动脚本,Shell实现判断进程是否存在并重新启动脚本分享...
- 使用electron-buil将vue打包成exe并生成安装包
- 契约锁电子签章在食品行业全产业链的应用场景
- xjoi 1542 玩玩拉格朗日四平方和定理