Spring Session在不改变原有使用方式的前提下可以管理session。

从注解@EnableSpringHttpSession入手:

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)

@Target({ java.lang.annotation.ElementType.TYPE })

@Documented

@Import(SpringHttpSessionConfiguration.class)

@Configuration

public @interface EnableSpringHttpSession {

}

发现导入了配置类

SpringHttpSessionConfiguration

@Configuration

public class SpringHttpSessionConfiguration {

private CookieHttpSessionStrategy defaultHttpSessionStrategy = new CookieHttpSessionStrategy();

private HttpSessionStrategy httpSessionStrategy = this.defaultHttpSessionStrategy;

private List httpSessionListeners = new ArrayList();

private ServletContext servletContext;

@Bean

public SessionEventHttpSessionListenerAdapter sessionEventHttpSessionListenerAdapter() {

return new SessionEventHttpSessionListenerAdapter(this.httpSessionListeners);

}

@Bean

public SessionRepositoryFilter extends ExpiringSession> springSessionRepositoryFilter(

SessionRepository sessionRepository) {

SessionRepositoryFilter sessionRepositoryFilter = new SessionRepositoryFilter(

sessionRepository);

sessionRepositoryFilter.setServletContext(this.servletContext);

if (this.httpSessionStrategy instanceof MultiHttpSessionStrategy) {

sessionRepositoryFilter.setHttpSessionStrategy(

(MultiHttpSessionStrategy) this.httpSessionStrategy);

}

else {

sessionRepositoryFilter.setHttpSessionStrategy(this.httpSessionStrategy);

}

return sessionRepositoryFilter;

}

@Autowired(required = false)

public void setServletContext(ServletContext servletContext) {

this.servletContext = servletContext;

}

@Autowired(required = false)

public void setCookieSerializer(CookieSerializer cookieSerializer) {

this.defaultHttpSessionStrategy.setCookieSerializer(cookieSerializer);

}

@Autowired(required = false)

public void setHttpSessionStrategy(HttpSessionStrategy httpSessionStrategy) {

this.httpSessionStrategy = httpSessionStrategy;

}

@Autowired(required = false)

public void setHttpSessionListeners(List listeners) {

this.httpSessionListeners = listeners;

}

}

最底下有四个可以配置的Spring Session方式。看名字可以看出他们的作用。

这个配置往Spring容器中配置两个bean。

SessionEventHttpSessionListenerAdapter session事件的监听器适配器

SessionRepositoryFilter session 持久化的过滤器,这是Spring Session的核心。

SessionRepositoryFilter 可以支持两种策略,

CookieHttpSessionStrategy是基于Cookie的默认实现。

MultiHttpSessionStrategy可以支持多个用户在一个浏览器上。

SessionRepositoryFilter 过滤核心代码

@Override

protected void doFilterInternal(HttpServletRequest request,

HttpServletResponse response, FilterChain filterChain)

throws ServletException, IOException {

request.setAttribute(SESSION_REPOSITORY_ATTR, this.sessionRepository);

SessionRepositoryRequestWrapper wrappedRequest = new SessionRepositoryRequestWrapper(

request, response, this.servletContext);

SessionRepositoryResponseWrapper wrappedResponse = new SessionRepositoryResponseWrapper(

wrappedRequest, response);

HttpServletRequest strategyRequest = this.httpSessionStrategy

.wrapRequest(wrappedRequest, wrappedResponse);

HttpServletResponse strategyResponse = this.httpSessionStrategy

.wrapResponse(wrappedRequest, wrappedResponse);

try {

filterChain.doFilter(strategyRequest, strategyResponse);

}

finally {

wrappedRequest.commitSession();

}

HttpServletRequest strategyRequest = this.httpSessionStrategy

.wrapRequest(wrappedRequest, wrappedResponse);

HttpServletResponse strategyResponse = this.httpSessionStrategy

.wrapResponse(wrappedRequest, wrappedResponse);

}

SessionRepositoryRequestWrapper及

SessionRepositoryResponseWrapper是将

HttpServletRequest、HttpServletResponse包装成的可以提供session持久化功能的类。

HttpServletRequest strategyRequest = this.httpSessionStrategy

.wrapRequest(wrappedRequest, wrappedResponse);

HttpServletResponse strategyResponse = this.httpSessionStrategy

.wrapResponse(wrappedRequest, wrappedResponse);

利用多态完成request及response的实现转换。

来源:[]()

java中session源码_Spring Session原理及源码分析相关推荐

  1. java 中线程池的种类,原理以及源码解析(1)

    java 中的线程池创建都是Executors 类中提供的方法,并且方法返回线程池对象. Executors 源码: // // Source code recreated from a .class ...

  2. 【Java面试题】21 Java中的异常处理机制的简单原理和应用。

    [Java面试题]21 Java中的异常处理机制的简单原理和应用. 参考文章: (1)[Java面试题]21 Java中的异常处理机制的简单原理和应用. (2)https://www.cnblogs. ...

  3. java校验框架源码解析_Spring Boot原理剖析和源码分析

    Spring Boot原理剖析和源码分析 依赖管理 问题一:为什么导入dependency时不需要指定版本? spring-boot-starter-parent依赖 org.springframew ...

  4. 动图 + 源码,演示 Java 中常用数据结构执行过程及原理

    最近在整理数据结构方面的知识, 系统化看了下Java中常用数据结构, 突发奇想用动画来绘制数据流转过程. 主要基于jdk8, 可能会有些特性与jdk7之前不相同, 例如LinkedList Linke ...

  5. 数据结构中缀表达式转后缀表达式与后缀表达式的求值实训报告_动图+源码,演示 Java 中常用数据结构执行过程及原理...

    程序员的成长之路互联网/程序员/成长/职场 关注 阅读本文大概需要 3.7 分钟. 作者:大道方圆cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识, ...

  6. 动图+源码,演示 Java 中常用数据结构执行过程及原理

    程序员的成长之路 互联网/程序员/成长/职场 关注 阅读本文大概需要 3.7 分钟. 作者:大道方圆 cnblogs.com/xdecode/p/9321848.html 最近在整理数据结构方面的知识 ...

  7. 【Java集合学习系列】HashMap实现原理及源码分析

    HashMap特性 hashMap是基于哈希表的Map接口的非同步实现,继承自AbstractMap接口,实现了Map接口(HashTable跟HashMap很像,HashTable中的方法是线程安全 ...

  8. Java中字符串拼接的几种方式(源码分析)

    字符串拼接是我们在Java代码中比较经常要做的事情,就是把多个字符串拼接到一起. 我们都知道,String是Java中一个不可变的类,所以他一旦被实例化就无法被修改. 不可变类的实例一旦创建,其成员变 ...

  9. java跟setattribute,java 中的request.setAttribute和session.setAttribute的区别

    1,request.setAttribute("curruser", curruser)这个方法是将curruser这个对象保存在request作用域中,然后在转发进入的页面就可以 ...

  10. 深入理解Java中的反射机制和使用原理!详细解析invoke方法的执行和使用

    反射的概念 反射:Refelection,反射是Java的特征之一,允许运行中的Java程序获取自身信息,并可以操作类或者对象的内部属性 通过反射,可以在运行时获得程序或者程序中的每一个类型的成员活成 ...

最新文章

  1. Docker创建Gitea(git服务)
  2. python在windows和linux_python在windows和linux下获得本机本地ip地址方法小结
  3. 搜索引擎工作的基础流程与原理
  4. 自动设置图片的序号_word表格技巧:如何在表格中填充序号并能自动更新
  5. 虚拟化:企业信息化建设中坚力量
  6. linux基础命令学习(四)用户与群组
  7. 在Tomcat上跑东西时遇到的对我这样新手来说很难找到的问题
  8. 网页简单上传图片 imgareaselect插件
  9. 壹佰文章总结| 关于ASP.NETCore的分享之路
  10. C语言中sizeof详解——面试C/C++
  11. 第一次作业源于计科一班的王相博
  12. c语言找出递增子数组的长度,编程之美2.16 数组中最长递增子序列的长度
  13. python stdout,在Python中的Stdout编码
  14. 双模sa_七句话讲清NSA单模与SA+NSA双模手机的真实区别
  15. 一位Java程序员写给女友的情书
  16. MonoMac 1.0正式发布
  17. 定时器cron表达式
  18. 服务器硬盘一般怎样备份,GHOST对硬盘系统分区(一般是C盘)的备份和恢复图文教程,对操作系统的备份和恢复...
  19. matlab怎么取差分,差分进化算法原理与matlab实现
  20. html5 获取本机号码,如何获取本机手机号码

热门文章

  1. matlab中 s 函数简记
  2. Controller中请求数据的方式
  3. Python中几个有趣的函数
  4. Python爬取抖音app视频
  5. 这个教程价值有点高,利用Python制作全自动化营销软件!
  6. 死锁(python 版)
  7. asp.net JSONHelper JSON帮助类
  8. asp.net 2.0 下的表单验证Cookieless属性
  9. jQuery框架学习第十一天:实战jQuery表单验证及jQuery自动完成提示插件
  10. asp.net学习之SqlDataSource 2 select的四种参数赋予形式的解释