spring - java spring session如何自定义cookie键

标签 spring session cookies redis

我使用 spring session HttpSession ,如何自定义 cookie 键,我尝试了这个解决方案: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>

最佳答案

你只需要添加下面的bean来创建自定义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>

关于spring - java spring session如何自定义cookie键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39367493/

相关文章:

session - 如何增加 Symfony 中的 session 超时

javascript - 当我们运行 2 个或多个项目时无法创建新的 session ID

php - 在 PHP session 中存储用户密码是否常见?

javascript - 如何使用 cookie 保存游戏中的分数?

java - 在 Tomcat 中支持没有 Cookie 的 session

java - Spring MVC HandlerInterceptor : Redirect Failed

java - 具有 Mysql JSON 类型的 Spring Data

java - 计数(唯一) View Spring boot REST

php - 使用cURL/php获取页面,但缺少一些东西

java - 无法将 JSP 项目作为动态 Web 项目导入 Eclipse