【问题标题】:Custom cookie name when using Spring Session使用 Spring Session 时的自定义 cookie 名称
【发布时间】:2015-07-09 23:45:05
【问题描述】:

我正在使用 v1.0.1 的 Spring Sessions。我已经使用 XML 配置设置了我的应用程序。我现在需要根据某些属性将 cookie 名称从默认的“SESSION”更改。例如 myApp_SESSION,其中 myApp 将从属性文件中读取。

我注意到 SessionRepositoryFilter 只有一个构造函数,它采用 sessionRepositoryhttpSessionStrategyCookieHttpSessionStrategy 使用默认值。

我当前的 XML 配置如下。

   <bean id="mapSessionRepository" class="org.springframework.session.MapSessionRepository" />
   <bean id="springSessionRepositoryFilter" class="org.springframework.session.web.http.SessionRepositoryFilter">
       <constructor-arg ref="mapSessionRepository" />
   </bean>

是否可以通过将 CookieHttpSessionStrategy 注入 springSessionRepositoryFilter bean 来更改 cookie 名称?

【问题讨论】:

    标签: spring cookies spring-session


    【解决方案1】:

    你是对的。可以将具有自定义 cookie 名称的 CookieHttpSessionStrategy 注入 SessionRepositoryFilter。

    <bean id="sessionRepositoryFilter"             
          class="org.springframework.session.web.http.SessionRepositoryFilter">
      <constructor-arg ref="sessionRepository"/>
      <property name="httpSessionStrategy">
        <bean class="org.springframework.session.web.http.CookieHttpSessionStrategy">
          <property name="cookieName" value="myCookieName" />
        </bean>
      </property>
    </bean>
    

    【讨论】:

    • 感谢马克的回复。
    猜你喜欢
    • 2017-01-15
    • 2013-05-20
    • 2018-09-17
    • 1970-01-01
    • 2019-09-13
    • 2016-07-25
    • 2016-02-05
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多