【问题标题】:spring-session-data-redis not workingspring-session-data-redis 不工作
【发布时间】:2016-05-31 11:34:22
【问题描述】:

我试过官方参考Spring Session提供的例子。通过http://localhost:8080/login 登录后,似乎会话数据仍然存储在内存中,并且没有redis 交互(通过redis-cli monitor 命令观察到)。只有 JSESSIONID 存储在 cookie 中

以下设置:

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

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

spring.xml:

<context:annotation-config/>
<beans:bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<beans:bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
            p:hostName="192.168.1.230"
            p:port="6379"
/>

<security:authentication-manager>
    <security:authentication-provider>
        <security:user-service>
            <security:user name="root" password="123456" authorities="ROLE_ADMIN"/>
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>
<security:global-method-security secured-annotations="enabled"/>
<security:http auto-config="true">
    <security:intercept-url pattern="/ping" access="hasRole('ROLE_ADMIN')"/>
    <security:form-login default-target-url="/ping"/>
    <security:csrf disabled="true"/>
</security:http>

<mvc:annotation-driven/>

<context:component-scan base-package="io.hbprotoss.demo.controller"/>

【问题讨论】:

    标签: spring spring-mvc spring-security spring-session


    【解决方案1】:

    您需要声明会话存储库过滤器,如下所示:

    <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>
    

    由于您使用的是 Spring Security,请确保在 安全过滤器(以及可能想要访问会话的任何其他过滤器)之前声明会话存储库过滤器,否则您可能会得到两个会话一个由 servlet 容器提供,一个由 spring session 提供。

    更多细节可以在这里找到 https://docs.spring.io/spring-session/docs/current/reference/html5/#xml-servlet-container-initialization

    【讨论】:

    • 我不太明白filter-name的用法。它是否影响过滤器类提供的使用哪个功能?因为我现在有两个过滤器具有相同的过滤器类但不同的过滤器名称。
    • 这就是DelegatingFilterProxy 过滤器的作用(查看其文档)。您实际上拥有真正的过滤器作为弹簧豆。 springSessionRepositoryFilter 是因为 &lt;beans:bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/&gt; 这一行而创建的
    • 知道了。感谢您的帮助:)
    猜你喜欢
    • 2021-09-13
    • 2021-01-06
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多