Spring 通过 zuul 访问时在请求之间重置会话范围 bean
Spring session scope bean reset between requests when accessing through zuul
spring 应用程序有一个会话 bean,其中包含某些数据。该数据在 init
请求时从数据库加载到控制器,这始终是客户端在启动时调用的第一个请求。该数据用于同一用户的其他请求。现在,一切正常。然而,在尝试使用 zuul 将应用程序集成到系统中之后(据我所知,在这种情况下,它只是将请求从一个 url 重定向到另一个),它崩溃了。每当在 init
之后调用方法时,会话 bean 的数据就是 null
。
这是来自服务 class 的片段:
@Autowired
TaskCache cache;
@Override
public void initUserSession() {
List<Task> data = loadTasks();
cache.setTasks(data);
LinearFilterStack<Task> fs = createFilterStack(data);
cache.setFilterStack(fs);
System.out.println(cache.hashCode()); //hashcode stays same
System.out.println(cache.getFilterStack() == null) //false
}
@Override
public List<Task> getTasks(Sort sort) {
System.out.println(cache.hashCode()); //hashcode stays same
System.out.println(cache.getFilterStack() == null) //true
LinearFilterStack<Task> fs = cache.getFilterStack();
List<Task> tasks = fs.filter(cache.getTasks()); //Obviously NPE
sortTasks(tasks, sort);
return tasks;
}
@Component
@Scope(value=WebApplicationContext.SCOPE_SESSION, proxyMode=ScopedProxyMode.TARGET_CLASS)
public class TaskCache { ... }
同样,这仅通过 zuul 发生。 IE。如果我使用 localhost:30022/rest/... 它有效,如果我使用 localhost:8080/app/tasks/rest/... (zuul 重定向到 localhost:30022/rest/.. .) 我得到 NPE,因为缓存 bean 在初始化请求后丢失了它的数据。
这可能是由 Zuul 的默认行为导致的,该行为阻止传递与 headers 相关的 cookie。
以下是 Zuul 的默认配置,它不允许在 header 秒以下传递到您的下游 API 服务器。
zuul.sensitiveHeaders= Authorization,Cookie,Set-Cookie
所以请尝试定义以下属性。它将允许您所有与 header 相关的 cookie 传递到您的 API 服务器。
zuul.sensitiveHeaders= Authorization
您可以在 this document
的 "Cookies and Sensitive Headers" 部分找到更多详细信息
spring 应用程序有一个会话 bean,其中包含某些数据。该数据在 init
请求时从数据库加载到控制器,这始终是客户端在启动时调用的第一个请求。该数据用于同一用户的其他请求。现在,一切正常。然而,在尝试使用 zuul 将应用程序集成到系统中之后(据我所知,在这种情况下,它只是将请求从一个 url 重定向到另一个),它崩溃了。每当在 init
之后调用方法时,会话 bean 的数据就是 null
。
这是来自服务 class 的片段:
@Autowired
TaskCache cache;
@Override
public void initUserSession() {
List<Task> data = loadTasks();
cache.setTasks(data);
LinearFilterStack<Task> fs = createFilterStack(data);
cache.setFilterStack(fs);
System.out.println(cache.hashCode()); //hashcode stays same
System.out.println(cache.getFilterStack() == null) //false
}
@Override
public List<Task> getTasks(Sort sort) {
System.out.println(cache.hashCode()); //hashcode stays same
System.out.println(cache.getFilterStack() == null) //true
LinearFilterStack<Task> fs = cache.getFilterStack();
List<Task> tasks = fs.filter(cache.getTasks()); //Obviously NPE
sortTasks(tasks, sort);
return tasks;
}
@Component
@Scope(value=WebApplicationContext.SCOPE_SESSION, proxyMode=ScopedProxyMode.TARGET_CLASS)
public class TaskCache { ... }
同样,这仅通过 zuul 发生。 IE。如果我使用 localhost:30022/rest/... 它有效,如果我使用 localhost:8080/app/tasks/rest/... (zuul 重定向到 localhost:30022/rest/.. .) 我得到 NPE,因为缓存 bean 在初始化请求后丢失了它的数据。
这可能是由 Zuul 的默认行为导致的,该行为阻止传递与 headers 相关的 cookie。
以下是 Zuul 的默认配置,它不允许在 header 秒以下传递到您的下游 API 服务器。
zuul.sensitiveHeaders= Authorization,Cookie,Set-Cookie
所以请尝试定义以下属性。它将允许您所有与 header 相关的 cookie 传递到您的 API 服务器。
zuul.sensitiveHeaders= Authorization
您可以在 this document
的 "Cookies and Sensitive Headers" 部分找到更多详细信息