Spring-security源码-Filter之LogoutFilter(十三)
1.Spring-Security系列导航2.spring-security使用-登录(一)3.spring-security使用-自定义数据源(二)4.spring-security使用-更友好的方式扩展登录AuthenticationProvider(三)5.spring-security使用-获得当前用户信息(四)6.spring-security使用-同一个账号只允许登录一次(五)7.spring-security使用-session共享(六)8.spring-security使用-安全防护HttpFirewall(七)9.spring-security使用-权限控制(八)10.spring-security源码-初始化(九)11.spring-security源码-如何初始化SecurityFilterChain到Servlet12.spring-security源码-FilterChainProxy13.spring-security源码-Filter之WebAsyncManagerIntegrationFilter(十)14.Spring-security源码-Filter之SecurityContextPersistenceFilter(十一)15.Spring-security源码-Filter之HeaderWriterFilter(十二)
16.Spring-security源码-Filter之LogoutFilter(十三)
17.Spring-security源码-Filter之UsernamePasswordAuthenticationFilter(十四)18.Spring-security源码-Filter之ConcurrentSessionFilter(十五)19.Spring-security源码-Filter之SessionManagementFilter(十六)20.Spring-security源码-Filter之RememberMeAuthenticationFilter(十七)21.Spring-security源码-Filter之ExceptionTranslationFilter(十八)22.Spring-security源码-Filter之FilterSecurityInterceptor(十九)23.Spring-security源码-注解权限原理(二十)24.Spring-security源码-注解权限原理之MethodSecurityInterceptor(二十一)25.Spring-Security基于源码扩展-一套系统多套登录逻辑(二十二)26.Spring-Security基于源码扩展-自定义登录(二十三)27.Spring-Security基于源码扩展-自定义认证失败返回(二十四)28.Spring-Security基于源码扩展-自定义授权注解(二十五)负责处理登出相关逻辑,默认url映射是/logout
org.springframework.security.config.annotation.web.configurers.LogoutConfigurer 初始化
默认初始化处https://www.cnblogs.com/LQBlog/p/15508248.html#autoid-12-0-0
private void applyDefaultConfiguration(HttpSecurity http) throws Exception { //http本质也是build 这里都是配置默认的config configure add CsrfConfigurer http.csrf(); //默认增加一个WebAsyncManagerIntegrationFilter http.addFilter(new WebAsyncManagerIntegrationFilter()); //configures add ExceptionHandlingConfigurer http.exceptionHandling(); //configures add HeadersConfigurer http.headers(); //configures add SessionManagementConfigurer http.sessionManagement(); //configure add SecurityContextConfigurer http.securityContext(); //configure add RequestCacheConfigurer http.requestCache(); ///configure add AnonymousConfigurer http.anonymous(); ///configure add ServletApiConfigurer http.servletApi(); //configure DefaultLoginPageConfigurer http.apply(new DefaultLoginPageConfigurer<>()); //configure LogoutConfigurer http.logout(); }
通过http.logout().addLogoutHandler() 可以自定义handler
LogoutFilter
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { //匹配是否能够处理 默认是/logout if (requiresLogout(request, response)) { //从SecurityContextHolder 获得Authentication信息 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (this.logger.isDebugEnabled()) { this.logger.debug(LogMessage.format("Logging out [%s]", auth)); } /** * 调用CompositeLogoutHandler 他也实现了LogoutHandler 他只是一个统一的管理器 * 内部循环调用LogoutHandler * 默认有三种 * PersistentTokenBasedRememberMeServices <1> * SecurityContextLogoutHandler <2> * LogoutSuccessEventPublishingLogoutHandler<3> */ this.handler.logout(request, response, auth); //处理登出成功的SimpleUrlLogoutSuccessHandler 比如重定向到登录页 this.logoutSuccessHandler.onLogoutSuccess(request, response, auth); return; } chain.doFilter(request, response); }
<1>
org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices#logout
@Override public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { //清除cookile super.logout(request, response, authentication); if (authentication != null) { //删除token this.tokenRepository.removeUserTokens(authentication.getName()); } }
<2>
org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler#logout
@Override public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { Assert.notNull(request, "HttpServletRequest required"); if (this.invalidateHttpSession) { HttpSession session = request.getSession(false); if (session != null) { //清空session session.invalidate(); if (this.logger.isDebugEnabled()) { this.logger.debug(LogMessage.format("Invalidated session %s", session.getId())); } } } if (this.clearAuthentication) { //清空 SecurityContext context = SecurityContextHolder.getContext(); context.setAuthentication(null); } //清空 SecurityContextHolder.clearContext(); }
<3>
发布一个spring的事件我们可以监听这个事件 知道某个用户登出了 参考:https://www.cnblogs.com/LQBlog/p/13878553.html#_label5
org.springframework.security.web.authentication.logout.LogoutSuccessEventPublishingLogoutHandler#logout
@Override public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { if (this.eventPublisher == null) { return; } if (authentication == null) { return; } this.eventPublisher.publishEvent(new LogoutSuccessEvent(authentication)); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2020-11-10 spring源码阅读(二)-IOC之ClassPathXmlApplicationContext