Spring-security:与COOKIE无法正常工作的session-config

时间:2017-02-16 11:30:33

标签: java spring spring-mvc tomcat spring-security

我正在开发一个Spring-MVC应用程序,我希望即使在Apache Tomcat重新启动后也要保持用户登录。我遇到了两个策略,一个是<session-config><tracking-mode>COOKIE,另一个link是设置Tomcat来存储HTTP Session。我们有postgres,并没有太多关于如何使用PostgreSQL的信息。

我尝试了第一次配置,虽然我没有错误,每当我重启tomcat时,都会抛出用户登录。这是解决这个问题的正确方法吗?有没有办法用Redis做到这一点?谢谢。

security-applicationContext.xml:

  <security:http create-session="ifRequired" use-expressions="true" auto-config="false" disable-url-rewriting="true">
        <security:form-login login-page="/login" username-parameter="j_username" password-parameter="j_password"
                             login-processing-url="/j_spring_security_check" default-target-url="/canvaslisting"
                             always-use-default-target="true" authentication-failure-url="/denied"/>
        <security:remember-me key="_spring_security_remember_me" user-service-ref="userDetailsService"
                              token-validity-seconds="1209600" data-source-ref="dataSource"/>
        <security:logout delete-cookies="JSESSIONID" invalidate-session="true" logout-url="/j_spring_security_logout"/>
   <!--<security:intercept-url pattern="/**" requires-channel="https"/>-->
        <security:port-mappings>
            <security:port-mapping http="80" https="443"/>
        </security:port-mappings>
        <security:logout logout-url="/logout" logout-success-url="/" success-handler-ref="myLogoutHandler"/>
        <security:session-management session-fixation-protection="migrateSession">
// we want multi-browser logins. 
            <security:concurrency-control session-registry-ref="sessionReg" max-sessions="5" expired-url="/login"/>
        </security:session-management>
    </security:http>

    <beans:bean id="sessionReg" class="org.springframework.security.core.session.SessionRegistryImpl"/>

    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider user-service-ref="LoginServiceImpl">
            <security:password-encoder ref="encoder"/>
        </security:authentication-provider>
    </security:authentication-manager>

web.xml:

 <session-config>
        <session-timeout>0</session-timeout>
        <tracking-mode>COOKIE</tracking-mode>
    </session-config>

1 个答案:

答案 0 :(得分:0)

您的链接问题已经描述了如何将会话持久保存到JDBC数据库 该示例使用MySql,使用Postgres的唯一区别是更改url和驱动程序类,就像使用任何JDBC连接一样。

<Manager className="org.apache.catalina.session.PersistentManager"
         maxIdleBackup="10">
  <Store className="org.apache.catalina.session.JDBCStore"
         connectionURL="jdbc:postgresql://localhost/mytomcat?user=root"
         driverName="org.postgresql.Driver"
         sessionAppCol="app_name"
         sessionDataCol="session_data"
         sessionIdCol="session_id"
         sessionLastAccessedCol="last_access"
         sessionMaxInactiveCol="max_inactive"
         sessionTable="tomcat_sessions"
         sessionValidCol="valid_session" />
</Manager>

如果您想使用redis,则可以使用Pivotal's RedisStore session manager

<Manager className="org.apache.catalina.session.PersistentManager">
    <Store className="com.gopivotal.manager.redis.RedisStore" 
             uri="redis://username:password@localhost:6370/0" />
</Manager>