Spring Session Data Redis 配置中遇到的坑

来源:互联网 发布:c 股票自动交易源码 编辑:程序博客网 时间:2024/06/07 11:59
说道Session共享,从网上搜索,有几种实现方式,比较普遍的 Tomcat Session Redis,这种是需要在tomcat容器里增加几个jar包,并修改Tomcat里的content.xml增加一条语句,实现session共享,此方法依赖于 tomcat.
还有一种是 Spring-Session-Data-Redis,此方法对servlet容器都有效,不局限于tomcat,也是比较普遍的一种方法,具体可自行百度了解
今天要说的是在配置SpringSessionDataRedis过程中遇到的坑
基本配置方式也比较简单
第一步:pom.xml中引入依赖
首先要引入依赖,此依赖并不是jar,而是一个空包里面还有对spring-session,redis等几个必要组件的依赖
<!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
<dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.3.1.RELEASE</version>
</dependency>
注意:此地方有一个比较坑爹地方,当打开Maven仓库,可以看到有一个对Spring版本的依赖的要求,对于1.3.1的SpringSession,对Spring的依赖最低4.3.4,如果依赖低于这个版本,则会报错Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.session.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}, 特别注意,引入不同的版本时,可能会引起原有jar包版本不兼容
第二步:Spring-mvc.xml中增加 redisHttpSessionConfiguration
    <!-- Spring Session Redis 共享 http://www.cnblogs.com/andyfengzp/p/6434287.html -->
     <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="1800"/>
    </bean>
注意:由于使用了这里的配置,由redis负责接管Session,原来web.xml里配置的Session超时时间就会失效了
    <session-config>
        <session-timeout>60</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
        </cookie-config>
    </session-config>

第三步:Web.xml中配置filter

    <!-- Spring Session Redis 共享 需要放在filter的第一个  http://www.cnblogs.com/andyfengzp/p/6434287.html -->
    <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>
注意:这个filter要写在比较靠前的第一个的位置

以上这几步配置完成后,基本就可以实现Session的共享了。
但是这里还有一个情况,项目启动后,左右的静态资源都无法访问了,这个是和原来的版本有不一样的地方,
Spring4.3,这里需要注意,所有Spring的xml的配置文件里标签要变为  -4.3.xsd
<!-- 静态文件资源访问配置 --> 要注意这里的静态文件不要放到WEB-INF里,要放到项目内,例如项目下的resources
<mvc:resources mapping="/**" location="/resources/" />这样就可以放行静态文件的访问


还有一个比较大的坑,使用Spring Session实际是Spring加入了一个Filter,其本质是:对每一个请求的request都会被DelegatingFilterProxy进行了一次封装。那么,在Controller里面拿出的request实际上是封装后的request,因为Session要存在Session,所以调用request.getSession()的时候,实际上拿到是Spring封装后的session ,因此对于request实际上拿到是Spring封装后的request。那么可能会导致Request.getInputStream无法获取到流数据,对于使用raw格式,即非Key,Value参数的提交 会被Spring-Session获取,contentTypeapplication/x-www-form-urlencoded时,就会被解析成Paramater,从而导致request.getInputStream的数据再获取时为空了。还有就是Controller里如果用到了request.setCharacterEncoding("GBK"); 设置字符集,这样的请求也无法生效,因为request.setCharacterEncoding只有在request.getSession或者request.getParamater之前使用有效,而由于使用Spring Session加入了filter,所以Controller里的request.setCharacterEncoding这种方式转编码就失效了,解决办法,可以是new String(request.getParameter(key).getBytes(), "GBK"),获取到参数后转码

相关连接
http://blog.csdn.net/lchpersonal521/article/details/52104959