在springsecurity 中,我们一般可以通过代码:

SecurityContext securityContext = SecurityContextHolder.getContext();

Authentication auth = securityContext.getAuthentication();

获取当前登录人员信息,其实我们可以从SecurityContext 获取 springsecurity 实现的秘密。

就让我从SecurityContextHolder 一步步抽丝剥茧,解读一下SecurityContext  的来源。

这里我们可以通过查看SecurityContextHolder 源码,看看这个SecurityContext  到底 是哪里来的。

 public static void clearContext() {
strategy.clearContext();
} /**
* Obtain the current <code>SecurityContext</code>.
*
* @return the security context (never <code>null</code>)
*/
public static SecurityContext getContext() {
return strategy.getContext();
}
public static void setContext(SecurityContext context) {
strategy.setContext(context);
}

代码很简单,我们可以看到他是通过 strategy 获取 的,那这个strategy 有是什么东西呢?

通过跟踪 源码发现 ,这个 strategy 实际是ThreadLocalSecurityContextHolderStrategy 的一个实例对象。

那这个ThreadLocalSecurityContextHolderStrategy  又是什么呢,我们继续看源码。

final class ThreadLocalSecurityContextHolderStrategy implements SecurityContextHolderStrategy {
//~ Static fields/initializers ===================================================================================== private static final ThreadLocal<SecurityContext> contextHolder = new ThreadLocal<SecurityContext>(); //~ Methods ======================================================================================================== public void clearContext() {
contextHolder.remove();
} public SecurityContext getContext() {
SecurityContext ctx = contextHolder.get(); if (ctx == null) {
ctx = createEmptyContext();
contextHolder.set(ctx);
} return ctx;
} public void setContext(SecurityContext context) {
Assert.notNull(context, "Only non-null SecurityContext instances are permitted");
contextHolder.set(context);
} public SecurityContext createEmptyContext() {
return new SecurityContextImpl();
}
}

不过就是一个放到一个线程变量中。看到这里我们需要知道 什么使用调用了这个setContext 方法。

通过跟踪源码发现:原来调用这个setContext方法的类:

SecurityContextPersistenceFilter

这是一个过滤器。我们知道 springsecurity 是有一组 过滤器 组成的,并且这个过滤器排在这些过滤器的第一个位置。

这个我们终于找到设置这个context的源头了。

通过阅读源码 ,我们看到这样两行代码.

HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
SecurityContext contextBeforeChainExecution = repo.loadContext(holder);

我们看到了这个SecurityContext 的源头了。

我们找到了这个 SecurityContext 是通过这个SecurityContextRepository 类获取的。

我们找到了这个接口的实现 HttpSessionSecurityContextRepository。

public SecurityContextPersistenceFilter() {
this(new HttpSessionSecurityContextRepository());
} public SecurityContextPersistenceFilter(SecurityContextRepository repo) {
this.repo = repo;
}

最终我们找到了HttpSessionSecurityContextRepository代码

 private SecurityContext readSecurityContextFromSession(HttpSession httpSession) {
final boolean debug = logger.isDebugEnabled(); if (httpSession == null) {
if (debug) {
logger.debug("No HttpSession currently exists");
} return null;
} // Session exists, so try to obtain a context from it. Object contextFromSession = httpSession.getAttribute(springSecurityContextKey); if (contextFromSession == null) {
if (debug) {
logger.debug("HttpSession returned null object for SPRING_SECURITY_CONTEXT");
} return null;
} // We now have the security context object from the session.
if (!(contextFromSession instanceof SecurityContext)) {
if (logger.isWarnEnabled()) {
logger.warn(springSecurityContextKey + " did not contain a SecurityContext but contained: '"
+ contextFromSession + "'; are you improperly modifying the HttpSession directly "
+ "(you should always use SecurityContextHolder) or using the HttpSession attribute "
+ "reserved for this class?");
} return null;
} if (debug) {
logger.debug("Obtained a valid SecurityContext from " + springSecurityContextKey + ": '" + contextFromSession + "'");
} // Everything OK. The only non-null return from this method. return (SecurityContext) contextFromSession;
}

这里我们可以看到 实际 这个SecurityContext 实际是从session 中获取的。

Object contextFromSession = httpSession.getAttribute("SPRING_SECURITY_CONTEXT");

从这里我们就知道了这个SecurityContext 来源了。

步骤是:

1.通过 SecurityContextPersistenceFilter 这个过滤器,从session 中获取SecurityContext .

2.并且把这个SecurityContext  放到线程变量中,然后我们在这个请求中就可以直接通过SecurityContextHolder.getContext();

获取SecurityContext 对象了。

解决了获取的问题,我们只需要把登录后的SecurityContext  放到httpsession 中就好了。

下面代码就是登录的代码实现:

UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(userName, pwd);
authRequest.setDetails(new WebAuthenticationDetails(request));
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication auth = authenticationManager.authenticate(authRequest);
securityContext.setAuthentication(auth); HttpSession session = request.getSession();
session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, securityContext);

通过上面的代码,我们就将securityContext 放到 session中了。

springsecurity 源码解读之 SecurityContext的更多相关文章

  1. springsecurity 源码解读 之 RememberMeAuthenticationFilter

    RememberMeAuthenticationFilter 的作用很简单,就是用于当session 过期后,系统自动通过读取cookie 让系统自动登录. 我们来看看Springsecurity的过 ...

  2. springsecurity 源码解读之 AnonymousAuthenticationFilter

    我们知道springsecutity 是通过一系列的 过滤器实现的,我们可以看看这系列的过滤器到底长成什么样子呢? 一堆过滤器,这个过滤器的设计设计上是 责任链设计模式. 这里我们可以看到有一个 An ...

  3. SDWebImage源码解读之SDWebImageDownloaderOperation

    第七篇 前言 本篇文章主要讲解下载操作的相关知识,SDWebImageDownloaderOperation的主要任务是把一张图片从服务器下载到内存中.下载数据并不难,如何对下载这一系列的任务进行设计 ...

  4. SDWebImage源码解读 之 NSData+ImageContentType

    第一篇 前言 从今天开始,我将开启一段源码解读的旅途了.在这里先暂时不透露具体解读的源码到底是哪些?因为也可能随着解读的进行会更改计划.但能够肯定的是,这一系列之中肯定会有Swift版本的代码. 说说 ...

  5. SDWebImage源码解读 之 UIImage+GIF

    第二篇 前言 本篇是和GIF相关的一个UIImage的分类.主要提供了三个方法: + (UIImage *)sd_animatedGIFNamed:(NSString *)name ----- 根据名 ...

  6. SDWebImage源码解读 之 SDWebImageCompat

    第三篇 前言 本篇主要解读SDWebImage的配置文件.正如compat的定义,该配置文件主要是兼容Apple的其他设备.也许我们真实的开发平台只有一个,但考虑各个平台的兼容性,对于框架有着很重要的 ...

  7. SDWebImage源码解读_之SDWebImageDecoder

    第四篇 前言 首先,我们要弄明白一个问题? 为什么要对UIImage进行解码呢?难道不能直接使用吗? 其实不解码也是可以使用的,假如说我们通过imageNamed:来加载image,系统默认会在主线程 ...

  8. SDWebImage源码解读之SDWebImageCache(上)

    第五篇 前言 本篇主要讲解图片缓存类的知识,虽然只涉及了图片方面的缓存的设计,但思想同样适用于别的方面的设计.在架构上来说,缓存算是存储设计的一部分.我们把各种不同的存储内容按照功能进行切割后,图片缓 ...

  9. SDWebImage源码解读之SDWebImageCache(下)

    第六篇 前言 我们在SDWebImageCache(上)中了解了这个缓存类大概的功能是什么?那么接下来就要看看这些功能是如何实现的? 再次强调,不管是图片的缓存还是其他各种不同形式的缓存,在原理上都极 ...

随机推荐

  1. 简单的知识图谱,neo4j+python

    因为研究方向是知识图谱,就有兴致想要构建一个简单的知识图谱,就在网上查找了一下,参考了neo4j搭建简单的金融知识图谱的思想,就着手从零开始构建. 1.首先就要考虑数据的获得,因为之前没有接触过爬虫之 ...

  2. 长短记忆神经网络LSTM

    转载: https://www.jianshu.com/p/dcec3f07d3b5 https://blog.csdn.net/dream_catcher_10/article/details/48 ...

  3. wampserver_x86_3.0.6 允许外网访问配置教程

    1.打开wamp目录下的apache配置文件中的httpd.conf 用可以看行数的编辑器打开 大概244行: 改为 <Directory /> AllowOverride none Re ...

  4. [java,2018-02-24] svn检出项目名称不正确

    ,今天从svn中检出项目时发现,检出项目的名称与实际的不相同,如下图: 这才想起来,当时创建项目时是随意起了个test的名称作为项目名,后来觉得能用,就在me中直接把项目名称改掉,提交到了svn.再从 ...

  5. redis 10个问题

    (1)什么是redis? Redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) (2)Reids的特点 Redis本质上是一个Key-Value类型的 ...

  6. java中存在三种调用机制

    1:同步调用:一种阻塞式调用,调用方要等待对方执行完毕才返回,它是一种单向调用 2:回调:一种双向调用模式,也就是说,被调用方在接口被调用时也会调用对方的接口: 3:异步调用:一种类似消息或事件的机制 ...

  7. 日志模块logging

    import logging # 设置日志基础样式 logging.basicConfig(level=logging.INFO, format='levelname:%(levelname)s fi ...

  8. 面向连接的tcp 编程

    from socket import * serverSocket=socket(AF_INET,SOCK_STREAM) serverSocket.bind(("",8899)) ...

  9. 2008nian元旦

    我要做---->> 控制财务状况---1记账 2 炒股或基金 3预算 锻炼---每次要达到锻炼的效果 学习计划-- 1报班架构 2 项目管理 3读书 爱好---做饭,收拾整理 职业规划. ...

  10. Python学习笔记---切片 列表 元祖 字典 集合

    列表[1,2,3,2]#[] 元祖(1,2,3,2)#() 字典{1:2,3:2}#{} 集合{1,2,3,2}#{} 1,集合与列表的区别,集合里不能有重复元素 2.字典与集合的区别,都是用花括号表 ...