栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringSession核心原理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringSession核心原理

SpringSession核心原理 1. 配置SpringSession
  • 引入依赖
		
            org.springframework.session
            spring-session-data-redis
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
  • 配置application.properties
spring.session.store-type=redis # Session store type.
  • 在配置类上标记注解@EnableRedisHttpSession
@EnableRedisHttpSession
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class GulimallAuthServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(GulimallAuthServerApplication.class, args);
    }
}
  • 编写配置类
@Configuration
public class SessionConfig {

    
    @Bean
    public cookieSerializer cookieSerializer() {
        DefaultcookieSerializer cookieSerializer = new DefaultcookieSerializer();
        cookieSerializer.setDomainName("xxx.com");
        cookieSerializer.setcookieName("YOUR_SESSION_NAME");

        return cookieSerializer;
    }

    
    @Bean
    public RedisSerializer springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}
 
2. SpringSession核心原理 

在要使用SpringSession的服务的主启动类上根据不同的nosql要标记不同的注解

这里使用Redis,所以标记了@EnableRedisHttpSession注解

@EnableRedisHttpSession
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@documented
@import(RedisHttpSessionConfiguration.class)// 导入配置
@Configuration
public @interface EnableRedisHttpSession {

@EnableRedisHttpSession中导入了RedisHttpSessionConfiguration这个配置类

在RedisHttpSessionConfiguration这个配置类中,为IOC容器中添加了一个组件: RedisOperationsSessionRepository 用于使用redis操作session,也就是session的增删改查封装类。

public class RedisHttpSessionConfiguration extends SpringHttpSessionConfiguration
      implements BeanClassLoaderAware, EmbeddedValueResolverAware, importAware,
      SchedulingConfigurer {

RedisHttpSessionConfiguration继承了SpringHttpSessionConfiguration,

而SpringHttpSessionConfiguration中为容器添加了另一个重要的组件:SessionRepositoryFilter

SessionRepositoryFilter是session存储的过滤器。SessionRepositoryFilter继承了OncePerRequestFilter,而OncePerRequestFilter实现了Filter接口,所以SessionRepositoryFilter是一个servlet的过滤器,所有的请求过来都需要经过filter。

SessionRepositoryFilter中重写了最核心的方法: doFilterInternal,这个方法在SessionRepositoryFilter的父类中的doFilter中被引用,也就是说是OncePerRequestFilter重写了Fliter接口的doFilter,doFilterInternal在doFilter中被调用实现了核心的功能。

doFilterInternal方法:

@Override
protected void doFilterInternal(HttpServletRequest request,
      HttpServletResponse response, FilterChain filterChain)
      throws ServletException, IOException {
   request.setAttribute(SESSION_REPOSITORY_ATTR, this.sessionRepository);

    // 将原生的request用一个包装类SessionRepositoryRequestWrapper包装起来
   SessionRepositoryRequestWrapper wrappedRequest = new SessionRepositoryRequestWrapper(
         request, response, this.servletContext);
    // 将原生的response用一个包装类包SessionRepositoryResponseWrapper装起来
   SessionRepositoryResponseWrapper wrappedResponse = new SessionRepositoryResponseWrapper(
         wrappedRequest, response);

   try {
      //最后在filter链中放行的是包装过后的request和response
      filterChain.doFilter(wrappedRequest, wrappedResponse);
   }
   finally {
      wrappedRequest.commitSession();
   }
}

SpringSession主要是使用了装饰者模式,将原生的request和response进行了封装,原来我们获取session对象是用request.getSession()获取,而现在是使用wrapperRequest.getSession从RedisOperationsSessionRepository中获取Session,这样我们对session的增删改查就是在Redis中进行的了。

// session是使用sessionRepository的createSession()方法进行创建的
S session = SessionRepositoryFilter.this.sessionRepository.createSession();

最后,在Redis中储存的Session到了过期时间也会自动延期。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号