• Spring Session


    开工开工,

    准备条件:

    1. 本地Redis,官网:http://redis.io/,windows下

    https://github.com/ServiceStack/redis-windows

    https://github.com/MSOpenTech/redis/releases msi版本

    2. Maven下的项目

    开始:

    1. 依赖配置pom.xml

    <dependency> 
    <groupId>org.springframework.session</groupId> 
    <artifactId>spring-session</artifactId> 
    <version>1.0.1.RELEASE</version> 
    </dependency> 
    <dependency> 
    <groupId>com.orange.redis-embedded</groupId> 
    <artifactId>embedded-redis</artifactId> 
    <version>0.6</version> 
    </dependency> 
    <dependency> 
    <groupId>org.springframework.session</groupId> 
    <artifactId>spring-session-data-redis</artifactId> 
    <version>1.0.1.RELEASE</version> 
    </dependency>

    2. web.xml配置

    <filter>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>springSessionRepositoryFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    3.SpringMVC.xml配置


    <!-- spring session redis 配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"/>

    <!--Redis 单机配置 -->
    <!-- 
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="127.0.0.1" />
    <property name="port" value="6379" /> -->
    <!--<property name="password" value="${redis.password}" />-->
    <!--<property name="timeout" value="${redis.timeout}" />-->
    <!--<property name="poolConfig" ref="jedisPoolConfig" /> -->
    <!--<property name="usePool" value="true" /> -->
    <!-- </bean> -->

    <!-- Redis 集群配置 -->
    <!-- 
    <bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
    <constructor-arg name="master" value="mymaster" />
    <constructor-arg name="sentinelHostAndPorts">
    <set>
    <value>xxx.xx.x.xxx:xxxxx</value>
    <value>xxx.xx.x.xxx:xxxxx</value>
    <value>xxx.xx.x.xxx:xxxxx</value>
    </set>
    </constructor-arg>
    </bean>
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <constructor-arg ref="sentinelConfig" />
    </bean> -->

    <!-- 
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
    <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean> -->

    <!-- 将session放入redis 360分钟-->
    <!--
    <bean id="redisHttpSessionConfiguration"
    class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> 
    <property name="maxInactiveIntervalInSeconds" value="21600" />
    </bean> -->

    就着完了,注意看集群配置,因为我是本地没有测试到集群。

    这是第一种配置方式,第二种配置,上面的所有配置都不需要,除了pom的依赖文件。

    第二种配置,添加两个类文件,什么都不用做了,ok.

    @EnableRedisHttpSession 
    public class SessionConfig {

    @Bean
    public JedisConnectionFactory connectionFactory() {
    JedisConnectionFactory connection = new JedisConnectionFactory(); 
    connection.setPort(6379); 
    connection.setHostName("127.0.0.1"); 
    return connection; 
    }
    }

    public class SessionInitializer extends AbstractHttpSessionApplicationInitializer 
    {
    public SessionInitializer() 
    {
    super(SessionConfig.class);
    }
    }

    测试session,你打开你的网站,用浏览器,

    打开 redis-cli.exe,执行 keys *,你就可以看到当前所有的keys值,

    再打开一个浏览器,再执行 keys *,你会发现多了两个key,一个是记住过期的事件,

    你可以打开 浏览器,F12,执行js new Date(value),把你的value贴进去,就知道是什么时候过期了。

    参考引用:

    使用spring-session把http session放到redis里面

    Session集群 http://qtdebug.com/spring-security/5.%20Spring-Security-%E9%9B%86%E7%BE%A4.html

    Redis配置:http://aiilive.blog.51cto.com/1925756/1627455

    如何在Redis中查看Session数据?


    (1)Http Session数据在Redis中是以Hash结构存储的。 
    (2)可以看到,还有一个key="spring:session:expirations:1431577740000"的数据,是以Set结构保存的。这个值记录了所有session数据应该被删除的时间(即最新的一个session数据过期的时间)。 

    127.0.0.1:6379> keys *
    1) "spring:session:expirations:1431577740000"
    2) "spring:session:sessions:e2cef3ae-c8ea-4346-ba6b-9b3b26eee578"
    127.0.0.1:6379> type spring:session:sessions:e2cef3ae-c8ea-4346-ba6b-9b3b26eee578
    hash
    127.0.0.1:6379> type spring:session:expirations:1431577740000
    set
    
    127.0.0.1:6379> keys *
    1) "spring:session:expirations:1431527520000"
    2) "spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b"
    3) "spring:session:sessions:11a69da6-138b-42bc-9916-60ae78aa55aa"
    4) "spring:session:sessions:0a51e2c2-4a3b-4986-a754-d886d8a5d42d"
    5) "spring:session:expirations:1431527460000"
    
    127.0.0.1:6379> hkeys spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b
    1) "maxInactiveInterval"
    2) "creationTime"
    3) "lastAccessedTime"
    4) "sessionAttr:attr1"
    
    127.0.0.1:6379> hget spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b sessionAttr:attr1
    "xacxedx00x05srx00x11java.lang.Integerx12xe2xa0xa4xf7x81x878x02x00x01Ix00x05valuexrx00x10java.lang.Numberx86xacx95x1dx0bx94xe0x8bx02x00x00xpx00x00x00x03"
    
    127.0.0.1:6379> hget spring:session:sessions:59f3987c-d1e4-44b3-a83a-32079942888b creationTime
    "xacxedx00x05srx00x0ejava.lang.Long;x8bxe4x90xccx8f#xdfx02x00x01Jx00x05valuexrx00x10java.lang.Numberx86xacx95x1dx0bx94xe0x8bx02x00x00xpx00x00x01MMx94(xec"
  • 相关阅读:
    菜鸟打印模板
    centos8安装nginx1.9.1
    centos8安装tomcat8.5
    构建微信公众号服务实例和小程序服务实例
    wxMpMenuService.menuGet()和wxMpMenuService.getSelfMenuInfo()返回值对比
    spring cloud 实现基于Nacos权重的负载均衡
    go开发环境搭建
    国内公共的DNS服务&Centos7 配置公共DNS服务
    Redis
    Spring Boot 集成 PageHelper
  • 原文地址:https://www.cnblogs.com/wangchaoyuana/p/7497381.html
Copyright © 2020-2023  润新知