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);
}
}
確保,對於您通過添加類名來它是rememberMeService您使用此定製對PersistentTokenBasedRememberMeServices的bean的配置:
<beans:bean id="rememberMeServices"
class="my.custom.spring.PersistentTokenBasedRememberMeServicesCustom"/>
爲什麼你不是簡單地實現自己的RememberMe實現嗎?這很容易。 – lexicore 2010-04-09 14:58:59
重複? http://chackoverflow.com/questions/2594960/best-practice-to-implement-secure-remember-me – rook 2010-04-09 15:20:32
@lexicore人員實施他們自己的會話可能會給您的網絡應用帶來真正的破壞。不要重新發明風團。閱讀我的帖子上的「重複?」上面的問題。 – rook 2010-04-09 15:21:44