我有一个使用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">
最佳答案
您是否在RoleBasedAuthenticationSuccessHandler中调用super之前或之后设置cookie?
super.onAuthenticationSuccess(request,response,authentication);
您必须在调用super之前设置cookie,因为超类中的逻辑将发送重定向,因此阻止您更新HttpServletResponse的内容.
原文地址:https://www.jb51.cc/spring/431667.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。