Spring Session原理及源码分析

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】

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<HttpSessionListener> httpSessionListeners = new ArrayList<HttpSessionListener>();

      private ServletContext servletContext;

      @Bean
      public SessionEventHttpSessionListenerAdapter sessionEventHttpSessionListenerAdapter() {
          return new SessionEventHttpSessionListenerAdapter(this.httpSessionListeners);
      }

      @Bean
      public <S extends ExpiringSession> SessionRepositoryFilter<? extends ExpiringSession> springSessionRepositoryFilter(
            SessionRepository<S> sessionRepository) {
          SessionRepositoryFilter<S> sessionRepositoryFilter = new SessionRepositoryFilter<S>(
                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<HttpSessionListener> listeners) {
          this.httpSessionListeners = listeners;
      }
    }

最底下有四个可以配置的Spring Session方式。看名字可以看出他们的作用。
这个配置往Spring容器中配置两个bean。
SessionEventHttpSessionListenerAdapter session事件的监听器适配器
SessionRepositoryFilter session 持久化的过滤器,这是Spring Session的核心。

SessionRepositoryFilter 可以支持两种策略,

  1. CookieHttpSessionStrategy是基于Cookie的默认实现。
  2. 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的实现转换。


来源:[]()

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Spring Session原理及源码分析

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏