Spring Security源码分析八:Spring Security 退出

栏目: Java · 发布时间: 6年前

内容简介:Spring Security源码分析八:Spring Security 退出

Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。

退出原理

  1. 清除 Cookie
  2. 清除当前用户的 remember-me 记录
  3. 使当前 session 失效
  4. 清空当前的 SecurityContext
  5. 重定向到登录界面

Spring Security 的退出请求(默认为 /logout )由LogoutFilter过滤器拦截处理。

退出的实现

  1. 主页中添加退出链接
<a href="/signOut">退出</a>
  1. 配置 MerryyouSecurityConfig
......
				.and()
                .logout()
                .logoutUrl("/signOut")//自定义退出的地址
                .logoutSuccessUrl("/register")//退出之后跳转到注册页面
                .deleteCookies("JSESSIONID")//删除当前的JSESSIONID
                .and()
......

效果如下

user-gold-cdn.xitu.io/2018/1/18/1…

源码分析

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);
	}
  1. 匹配当前拦截的请求
  2. 处理 清空 Cookieremember-mesessionSecurityContext
  3. 重定向到登录界面

handler

user-gold-cdn.xitu.io/2018/1/18/1…
  1. CookieClearingLogoutHandler 清空 Cookie
  2. PersistentTokenBasedRememberMeServices 清空 remember-me
  3. SecurityContextLogoutHandler 使当前 session 无效,清空当前的 SecurityContext

CookieClearingLogoutHandler#logout

public void logout(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) {
		for (String cookieName : cookiesToClear) {
			//# 1.Cookie置为null
			Cookie cookie = new Cookie(cookieName, null);
			String cookiePath = request.getContextPath();
			if (!StringUtils.hasLength(cookiePath)) {
				cookiePath = "/";
			}
			cookie.setPath(cookiePath);
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}
  1. 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());
		}
	}
  1. 清空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();
	}
  1. 使当前session失效
  2. 清空当前的 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);
	}
  1. 获取配置的跳转地址
  2. 跳转请求

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法设计与分析

算法设计与分析

郑宗汉/郑晓明编 / 清华大学出版社 / 2005-6 / 32.00元

《算法设计与分析》系统地介绍算法设计与分析的概念和方法,共四部分内容,第一部分包括前两章,介绍算法设计与分析的基本概念及必要的数学工具,对算法的时间复杂性的概念及算法的分析方法作了较为详细的叙述。第二部分包括第3~~9章,以算法设计技术为纲,从排序问题和离散集合的操作开始,进而介绍递归技术、分治法、贪婪法、动态规划、回溯法、分支与限界法以及随机算法等算法设计技术及其复杂性。第三部分包括第10章和第......一起来看看 《算法设计与分析》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具