【问题标题】:SessionCookieConfig empty when retrieved from HttpSession从 HttpSession 检索时 SessionCookieConfig 为空
【发布时间】:2016-10-01 00:33:45
【问题描述】:

我正在开发 Spring Web 应用程序中的过滤器,该过滤器需要手动使用 Set-Cookie 标头进行响应。它还在头部构造之前以编程方式在 Spring 的 SecurityContext 中设置身份验证令牌,在处理过滤器时不存在会话。由于各种奇怪的架构原因,它不能从 web.xml 文件中的 属性中移除。我可以轻松检索会话的 ID,但其他信息对我来说一直难以捉摸。

我最初的方法是从过滤器中获取 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]

我一直在尝试解决这个问题,并认为我可以从这里获得一些建议。

谢谢!

【问题讨论】:

    标签: spring session cookies


    【解决方案1】:

    试试这个:

    SessionCookieConfig sessionConfig =  request.getServletContext().getSessionCookieConfig();
    

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 2011-01-26
      • 2012-06-27
      • 2016-12-31
      • 2012-09-22
      相关资源
      最近更新 更多