【问题标题】:Spring HttpSession with Redis not saving SESSION cookie value带有 Redis 的 Spring HttpSession 不保存 SESSION cookie 值
【发布时间】:2017-04-02 10:13:01
【问题描述】:

我们正在使用 Spring Cloud 构建一个基于微服务的系统,我们使用 Zuul 作为边缘服务器和一些后端微服务。在其中一个后端服务中,我们使用 Redis 来管理 HttpSession 并存储一些与用户相关的对象。

直接访问微服务时一切正常,但通过 Zuul 访问时出现问题,因为支持微服务总是获得一个新的HttpSession ID。我尝试使用 ZuulFilter 将带有 JSESSIONIDSESSION 的 cookie 转发到微服务,但微服务最终没有使用 cookie 值在 Redis 中创建 spring:sessions。

谁能帮助确定如何让 Spring Session 使用 Cookie 值,而不是 HttpSession.ID

我们正在使用:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Camden.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

【问题讨论】:

  • 你找到答案了吗

标签: spring cookies redis netflix-zuul httpsession


【解决方案1】:

我尝试了类似的方法。你可以看看这里:https://github.com/khauser/microservices4vaadin

需要对 DefaultCookieSerializer 进行更改:

@Bean
public HttpSessionStrategy httpSessionStrategy() {
  CookieHttpSessionStrategy cookieHttpSessionStrategy = new CookieHttpSessionStrategy();
  DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
  cookieSerializer.setCookieName("JSESSIONID");
  cookieSerializer.setCookiePath("/");
  cookieSerializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
  cookieHttpSessionStrategy.setCookieSerializer(cookieSerializer);
  return cookieHttpSessionStrategy;
}

【讨论】:

    【解决方案2】:

    只需在 application.properties 中使用它即可解决您的问题

    zuul.sensitive-headers=
    

    【讨论】:

      猜你喜欢
      • 2019-02-16
      • 2017-11-02
      • 2018-08-28
      • 2021-01-25
      • 2021-12-10
      • 2016-12-09
      • 2019-06-04
      • 2018-09-21
      • 1970-01-01
      相关资源
      最近更新 更多