我正在使用 Spring Security 的 RememberMe 服务来保持用户身份验证。
我想找到一种简单的方法将 RememberMe cookie 设置为会话 cookie,而不是设置固定的过期时间。对于我的应用程序,cookie 应持续存在,直到用户关闭浏览器。
关于如何最好地实施这一点有什么建议吗?是否担心这是潜在的安全问题?
这样做的主要原因是,使用基于 cookie 的令牌,负载均衡器后面的任何服务器都可以为受保护的请求提供服务,而无需依赖存储在 HttpSession 中的用户身份验证。事实上,我已经明确告诉 Spring Security 永远不要使用命名空间创建会话。此外,我们正在使用 Amazon 的 Elastic Load Balancing,因此不支持粘性会话。
注意:虽然我知道截至 4 月 8 日,亚马逊现在支持粘性会话,但由于其他一些原因,我仍然不想使用它们。也就是说,一台服务器的过早终止仍然会导致与其关联的所有用户的会话丢失。http://aws.amazon.com/about-aws/whats-new/2010/04/08/support-for-session-stickiness-in-elastic-load-balancing/ http://aws.amazon.com/about-aws/whats-new/2010/04/08/support-for-session-stickiness-in-elastic-load-balancing/
Spring Security 3 不提供如何生成 cookie 的配置。您必须覆盖默认行为:
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
/** Cookie expires on session. */
public class PersistentTokenBasedRememberMeServicesCustom extends
PersistentTokenBasedRememberMeServices {
/** only needed because super throws exception. */
public PersistentTokenBasedRememberMeServicesCustom() throws Exception {
super();
}
/** Copy of code of inherited class + setting cookieExpiration, */
@Override
protected void setCookie(String[] tokens, int maxAge,
HttpServletRequest request, HttpServletResponse response) {
String cookieValue = encodeCookie(tokens);
Cookie cookie = new Cookie(getCookieName(), cookieValue);
//cookie.setMaxAge(maxAge);
cookie.setPath("/");
cookie.setSecure(false); // no getter available in super, so always false
response.addCookie(cookie);
}
}
确保您使用此定制的基于持久令牌的记住我服务因为你是记住我服务通过将类名添加到它的 bean 配置中:
<beans:bean id="rememberMeServices"
class="my.custom.spring.PersistentTokenBasedRememberMeServicesCustom"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)