标签: session

  • 当在SpringBoot使用Redis的Session共享功能时,@EnableScheduling始终生效的问题。

    我在调试项目的redis共享时,遇到了这种情况:

    当我需要使用Redis来管理SpringBoot的Session时,会加入如下依赖:

    		<dependency>
    			<groupId>org.springframework.session</groupId>
    			<artifactId>spring-session-data-redis</artifactId>
    		</dependency>

    这个依赖会让启动类中的@EnableScheduling设定无效,就是说,此时无论有没有@EnableScheduling,系统中的所有定时器均会生效。

    唉~具体原因,我猜是因为这个功能需要用到定时器,所以会默认开启了呗~折腾了我半天,真浪费时间~

  • SpringBoot利用Redis对session进行共享

    1、引入pom依赖包

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    </dependency>

    2、配置application.application

    # spring session使用存储类型
    spring.session.store-type=redis

    #配置redis集群

    spring.redis.cluster.nodes=192.168.1.55:7000,192.168.1.53:7003

    3、启动类中增加@EnableRedisHttpSession

    4、使用
    request.getSession().setAttribute(“username”, “admin”);
    String userName = (String) request.getSession().getAttribute(“username”);