Custom cookie name when using Spring Session

一世执手 提交于 2019-12-19 11:37:06

问题


I am using v1.0.1 of Spring Sessions. I have got my application setup using XML configurations. I now need to change the cookie name from the default of "SESSION" based on some property. For example to myApp_SESSION where myApp will be read from a property file.

I noticed that the SessionRepositoryFilter has only one constructor that takes a sessionRepository and the httpSessionStrategy with CookieHttpSessionStrategy using default values.

My current XML configuration is as below.

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

Is it possible to change the cookie name by injecting a CookieHttpSessionStrategy to the springSessionRepositoryFilter bean?


回答1:


You are correct. It is possible to inject a CookieHttpSessionStrategy with a custom cookie name into the 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>


来源:https://stackoverflow.com/questions/29964921/custom-cookie-name-when-using-spring-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!