redis 集中式session解决

     一、 目前无非就是三种单机Session(基于单机内存,无法部署多台机器)、基于Cookie(安全性差)、基于全局的统一Session管理(redis、mysql)等多种方式 ;

    二、

采用的是redis进行集中式Session管理,核心依赖

            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session</artifactId>
                <version>1.2.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-redis</artifactId>
            </dependency>
三、application.yml 配置


    server:
      port: 8080
    spring:
      redis:
        database: 1
        host: localhost
        pool:
          max-active: 20

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

开启@EnableRedisHttpSession



    server:
      port: 8080
    spring:
      redis:
        database: 1
        host: localhost
        pool:
          max-active: 20

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

开启@EnableRedisHttpSession


通过加上@EnableRedisHttpSession注解,开启redis集中式session管理,所有的session都存放到了redis中


    @SpringBootApplication
    @EnableRedisHttpSession
    public class AppApplication {
        public static void main(String[] args) throws Exception {
            SpringApplication.run(AppApplication.class, args);
        }
    }

    server:
      port: 8080
    spring:
      redis:
        database: 1
        host: localhost
        pool:
          max-active: 20

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

开启@EnableRedisHttpSession


通过源码可知、可以通过设置maxInactiveIntervalInSeconds来设定session的统一过期时间,


    @Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
    @Target({ java.lang.annotation.ElementType.TYPE })
    @Documented
    @Import(RedisHttpSessionConfiguration.class)
    @Configuration
    public @interface EnableRedisHttpSession {
        int maxInactiveIntervalInSeconds() default 1800;

        String redisNamespace() default "";


        RedisFlushMode redisFlushMode() default RedisFlushMode.ON_SAVE;
    }

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

通过redis集中式管理session这种方式在使用上面对客户端是透明的,无需自己操作redis,在使用HttpSession对象的时候直接使用即可


    @RestController
    public class IndexController {
        @GetMapping("/index")
        public ResponseEntity index(HttpSession httpSession) {
            httpSession.setAttribute("user", "helloword");
            return ResponseEntity.ok("ok");
        }

        @GetMapping("/helloword")
        public ResponseEntity hello(HttpSession httpSession) {
            return ResponseEntity.ok(httpSession.getAttribute("user"));
        }
    }





    server:
      port: 8080
    spring:
      redis:
        database: 1
        host: localhost
        pool:
          max-active: 20

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

开启@EnableRedisHttpSession

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值