最近,IBM Security AppScan 发现了加密会话 (ssl) cookie 中缺少安全属性的问题。报告如下:
这个应用程序是 Java 代码,我添加了一个过滤器来设置所有 cookie 的安全,代码:
public class BasicFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
Cookie[] cookies = req.getCookies();
HttpServletResponse resp = (HttpServletResponse) servletResponse;
if( cookies != null && cookies.length > 0) {
for (int i = 0; i < cookies.length; i++) {
cookies[i].setSecure(true);
cookies[i].setHttpOnly(true);
resp.addCookie(cookies[i]);
}
}
filterChain.doFilter(req,resp);
}
@Override
public void destroy() {
}
}
它的工作原理是所有 cookie 都像这样响应两次,它会尝试一遍又一遍地登录(使用SSO 登录):
感谢您的热心帮助,我该如何启用安全和解决 cookie 问题,希望你们能给我一些想法来解决这个问题。谢谢!