2016-03-16 23 views
0

我正在Spring Web應用程序中使用Set-Cookie頭進行手動響應的過濾器。它還以編程方式在Spring的SecurityContext中設置標識構造之前的身份驗證令牌,在過濾器處理時沒有會話存在。由於各種奇怪的體系結構原因,它不能被驅動掉web.xml文件中的< cookie-config >屬性。我可以輕鬆地檢索會話的ID,但其他信息一直難以理解。從HttpSession中檢索SessionCookieConfig爲空

我最初的方法是從過濾器中獲取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] 

我一直在試圖找出這已經有一段時間了,我認爲我可以從這裏得到一些建議。

謝謝!

回答

0

試試這個:

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