【发布时间】:2016-10-01 00:33:45
【问题描述】:
我正在开发 Spring Web 应用程序中的过滤器,该过滤器需要手动使用 Set-Cookie 标头进行响应。它还在头部构造之前以编程方式在 Spring 的 SecurityContext 中设置身份验证令牌,在处理过滤器时不存在会话。由于各种奇怪的架构原因,它不能从 web.xml 文件中的
我最初的方法是从过滤器中获取 ServletContext。这导致 SessionCookieConfig 类为空。然后我从会话中获取了 ServletContext,但这也返回了一个没有填充属性的类。这是我的代码示例:
public class SomeFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws throws IOException, ServletException {
HttpServletRequest servletRequest = (HttpServletRequest) request;
HttpServletResponse servletResponse = (HttpServletResponse) response;
/** some logic **/
SecurityContextHolder.getContext().setAuthenticationn(getAuthToken())
HttpSession session = servletRequest.getSession();
SessionCookieConfig sessionConfig = session.getServletContext().getSessionCookieConfig();
String headerVal = String.format("%s=%s; Path=/; secure", sessionConfig.getName(), session.getId());
servletResponse.setHeader("Set-Cookie", headerVal);
/** some more logic **/
chain.doFilter(request, response);
}
}
我在理论上认为这会起作用,但这是我调试应用程序时 SessionCookieConfig 的样子:
org.apache.catalina.core.ApplicationSessionCookieConfig
httpOnly = false
secure = false
maxAge = -1
comment = null
domain = null
name = null
path = null
context = StandardEngine[Catalina].StandardHost[localhost].StandardContext[/path]
我一直在尝试解决这个问题,并认为我可以从这里获得一些建议。
谢谢!
【问题讨论】: