【问题标题】:java spring session how to custom cookie keyjava spring session如何自定义cookie键
【发布时间】:2017-01-15 00:03:42
【问题描述】:

我使用spring session HttpSession,我如何自定义cookie key,我尝试了这个解决方案:Custom cookie name when using Spring Session。但它不起作用,名称仍然是SESSION。

我的配置如下:

<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<context:property-placeholder location="classpath:/env/env_test.properties"/> 
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
     p:port="${spring.redis.port}" p:hostName="${spring.redis.host}"/>
<bean id="mapSessionRepository" class="org.springframework.session.MapSessionRepository" />
<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="_session_id" />
    </bean>
  </property>
</bean>

【问题讨论】:

    标签: spring session cookies redis


    【解决方案1】:

    您只需要在下面添加bean 即可创建custom cookie

        <bean class ="org.springframework.session.web.http.DefaultCookieSerializer">
                <property name="cookieName" value="JSESIONID"></property>       
        </bean>
    
    JESSIONID - Custom Cookie Name
    
        Please remove below configuration fr`enter code here`om xml file.
    
        <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="_session_id" />
            </bean>
          </property>
        </bean>
    

    【讨论】:

      【解决方案2】:

      你必须创建一个带有类的bean

      org.springframework.session.web.http.DefaultCookieSerializer
      

      因此,在该 bean 内部,您定义自定义属性,然后将该 bean 声明为以下属性:

      org.springframework.session.web.http.CookieHttpSessionStrategy
      

      举例:

      <bean id="yourCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
              <property name="cookieName" value="yourCustomName"/>
              <property name="cookiePath" value="yourCustomPath"/>
              ...
      </bean>
      <bean id="cookieHttpSessionStrategy" class="org.springframework.session.web.http.CookieHttpSessionStrategy">
              <property name="cookieSerializer" ref="yourCookieSerializer"></property>
      </bean>
      

      【讨论】:

        猜你喜欢
        • 2015-07-09
        • 2016-07-25
        • 1970-01-01
        • 2013-05-20
        • 2018-10-27
        • 2019-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多