我有一个使用Spring Security的Web项目,我试图在处理身份验证成功的方法中保存cookie.但是,当我查看浏览器的cookie时,只显示JSESSIONID,当我在Spring重定向到的servlet上查看request.getCookies()时,会发生相同的情况.
我试图将cookie保存在应用程序的一个servlet中,并且cookie保存正确,因此Spring Security可能会清除响应.你有什么主意吗?
一种解决方法是将其保存在Session中,然后获取它并将cookie保存在登录重定向到的servlet上.另一个是使用像this这样的javascript保存cookie.但我不喜欢这些解决方案.提前致谢
这是相关代码:
public class RoleBasedAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler implements
AuthenticationSuccessHandler {
...
// save a cookie with the selected language
MaparameterMap = request.getParameterMap();
if (parameterMap.containsKey("language")) {
saveCookie("language",parameterMap.get("language")[0],response);
}
}
public static void saveCookie(String cookieName,String value,HttpServletResponse response) {
Cookie cookie = new Cookie(cookieName,value);
//maxAge is one month: 30*24*60*60
cookie.setMaxAge(2592000);
cookie.setDomain("projectName");
cookie.setPath("/");
response.addCookie(cookie);
}
}
ecurity:http auto-config="false" ...>
ecurity:form-login login-page="/login.do" authentication-success-handler-ref="redirectRoleStrategy" .../>
...
ecurity:http>
ecurity.RoleBasedAuthenticationSuccessHandler">
最佳答案