使用 Spring 会话时的自定义 cookie 名称

Custom cookie name when using Spring Session

我正在使用 Spring Sessions 的 v1.0.1。我已经使用 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 名称?

你是对的。可以将带有自定义 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>