Spring Security系列之退出(十三)

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

内容简介:Spring Security的退出请求(默认为效果如下Cookie置为null
Spring Security系列之退出(十三)

退出原理

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

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

退出的实现

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

效果如下

Spring Security系列之退出(十三)

源码分析

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. 处理 清空Cookie、remember-me、session和SecurityContext
  3. 重定向到登录界面

handler

Spring Security系列之退出(十三)
  1. CookieClearingLogoutHandler 清空Cookie
  2. PersistentTokenBasedRememberMeServices 清空 remember-me
  3. SecurityContextLogoutHandler 使当前 session 无效,清空当前的 SecurityContext

CookieClearingLogoutHandler#logout

Cookie置为null

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);
		}
	}
复制代码

PersistentTokenBasedRememberMeServices#logout

清空persistent_logins表中记录

public void logout(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) {
		super.logout(request, response, authentication);

		if (authentication != null) {
			//#1.清空persistent_logins表中记录
			tokenRepository.removeUserTokens(authentication.getName());
		}
	}
复制代码

SecurityContextLogoutHandler#logout

  1. 使当前session失效
  2. 清空当前的SecurityContext
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();
	}
复制代码

AbstractAuthenticationTargetUrlRequestHandler#handle

  1. 获取配置的跳转地址
  2. 跳转请求
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);
}
复制代码

以上所述就是小编给大家介绍的《Spring Security系列之退出(十三)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

反应式设计模式

反应式设计模式

Roland Kuhn、Brian Hanafee、Jamie Allen / 何品、邱嘉和、王石冲、林炜翔审校 / 清华大学出版社 / 2019-1-1 / 98.00 元

《反应式设计模式》介绍反应式应用程序设计的原则、模式和经典实践,讲述如何用断路器模式将运行缓慢的组件与其他组件隔开、如何用事务序列(Saga)模式实现多阶段事务以及如何通过分片模式来划分数据集,分析如何保持源代码的可读性以及系统的可测试性(即使在存在许多潜在交互和失败点的情况下)。 主要内容 ? “反应式宣言”指南 ? 流量控制、有界一致性、容错等模式 ? 得之不易的关于“什么行不通”的经验 ? ......一起来看看 《反应式设计模式》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具