SpringSession核心原理

SpringSession核心原理,第1张

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到了过期时间也会自动延期。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5694165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)