mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-09 11:53:30 +00:00
SEC-1217: AbstractRememberMeServices should set 'secure' attribute on remember-me cookie if in secure context. Added "useSecureCookie" configuration property and corresponding use-secure-cookie attribute in namespace.
This commit is contained in:
parent
b2c2b93545
commit
2039200617
@ -32,6 +32,7 @@ class RememberMeBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
static final String ATT_TOKEN_REPOSITORY = "token-repository-ref";
|
static final String ATT_TOKEN_REPOSITORY = "token-repository-ref";
|
||||||
static final String ATT_USER_SERVICE_REF = "user-service-ref";
|
static final String ATT_USER_SERVICE_REF = "user-service-ref";
|
||||||
static final String ATT_TOKEN_VALIDITY = "token-validity-seconds";
|
static final String ATT_TOKEN_VALIDITY = "token-validity-seconds";
|
||||||
|
static final String ATT_SECURE_COOKIE = "use-secure-cookie";
|
||||||
|
|
||||||
protected final Log logger = LogFactory.getLog(getClass());
|
protected final Log logger = LogFactory.getLog(getClass());
|
||||||
private String servicesName;
|
private String servicesName;
|
||||||
@ -94,6 +95,10 @@ class RememberMeBeanDefinitionParser implements BeanDefinitionParser {
|
|||||||
services.getPropertyValues().addPropertyValue("userDetailsService", new RuntimeBeanReference(userServiceRef));
|
services.getPropertyValues().addPropertyValue("userDetailsService", new RuntimeBeanReference(userServiceRef));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ("true".equals(element.getAttribute(ATT_SECURE_COOKIE))) {
|
||||||
|
services.getPropertyValues().addPropertyValue("useSecureCookie", true);
|
||||||
|
}
|
||||||
|
|
||||||
if (tokenValiditySet) {
|
if (tokenValiditySet) {
|
||||||
Integer tokenValidity = new Integer(tokenValiditySeconds);
|
Integer tokenValidity = new Integer(tokenValiditySeconds);
|
||||||
if (tokenValidity.intValue() < 0 && isPersistent) {
|
if (tokenValidity.intValue() < 0 && isPersistent) {
|
||||||
|
@ -454,6 +454,10 @@ remember-me.attlist &=
|
|||||||
## Exports the internally defined RememberMeServices as a bean alias, allowing it to be used by other beans in the application context.
|
## Exports the internally defined RememberMeServices as a bean alias, allowing it to be used by other beans in the application context.
|
||||||
attribute services-alias {xsd:token}?
|
attribute services-alias {xsd:token}?
|
||||||
|
|
||||||
|
remember-me.attlist &=
|
||||||
|
## Determines whether the "secure" flag will be set on the remember-me cookie. If set to true, the cookie will only be submitted over HTTPS. Defaults to false.
|
||||||
|
attribute use-secure-cookie {boolean}?
|
||||||
|
|
||||||
remember-me.attlist &=
|
remember-me.attlist &=
|
||||||
## The period (in seconds) for which the remember-me cookie should be valid.
|
## The period (in seconds) for which the remember-me cookie should be valid.
|
||||||
attribute token-validity-seconds {xsd:integer}?
|
attribute token-validity-seconds {xsd:integer}?
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -544,6 +544,7 @@ public class HttpSecurityBeanDefinitionParserTests {
|
|||||||
RememberMeServices rememberMeServices = getRememberMeServices();
|
RememberMeServices rememberMeServices = getRememberMeServices();
|
||||||
|
|
||||||
assertTrue(rememberMeServices instanceof PersistentTokenBasedRememberMeServices);
|
assertTrue(rememberMeServices instanceof PersistentTokenBasedRememberMeServices);
|
||||||
|
assertFalse((Boolean)FieldUtils.getFieldValue(getRememberMeServices(), "useSecureCookie"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -587,8 +588,7 @@ public class HttpSecurityBeanDefinitionParserTests {
|
|||||||
"<http auto-config='true'>" +
|
"<http auto-config='true'>" +
|
||||||
" <remember-me key='ourkey' token-validity-seconds='10000' />" +
|
" <remember-me key='ourkey' token-validity-seconds='10000' />" +
|
||||||
"</http>" + AUTH_PROVIDER_XML);
|
"</http>" + AUTH_PROVIDER_XML);
|
||||||
assertEquals(10000, FieldUtils.getFieldValue(getRememberMeServices(),
|
assertEquals(10000, FieldUtils.getFieldValue(getRememberMeServices(), "tokenValiditySeconds"));
|
||||||
"tokenValiditySeconds"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -597,8 +597,16 @@ public class HttpSecurityBeanDefinitionParserTests {
|
|||||||
"<http auto-config='true'>" +
|
"<http auto-config='true'>" +
|
||||||
" <remember-me key='ourkey' token-validity-seconds='-1' />" +
|
" <remember-me key='ourkey' token-validity-seconds='-1' />" +
|
||||||
"</http>" + AUTH_PROVIDER_XML);
|
"</http>" + AUTH_PROVIDER_XML);
|
||||||
assertEquals(-1, FieldUtils.getFieldValue(getRememberMeServices(),
|
assertEquals(-1, FieldUtils.getFieldValue(getRememberMeServices(), "tokenValiditySeconds"));
|
||||||
"tokenValiditySeconds"));
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rememberMeSecureCookieAttributeIsSetCorrectly() throws Exception {
|
||||||
|
setContext(
|
||||||
|
"<http auto-config='true'>" +
|
||||||
|
" <remember-me key='ourkey' use-secure-cookie='true' />" +
|
||||||
|
"</http>" + AUTH_PROVIDER_XML);
|
||||||
|
assertTrue((Boolean)FieldUtils.getFieldValue(getRememberMeServices(), "useSecureCookie"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=BeanDefinitionParsingException.class)
|
@Test(expected=BeanDefinitionParsingException.class)
|
||||||
|
@ -55,6 +55,7 @@ public abstract class AbstractRememberMeServices implements RememberMeServices,
|
|||||||
private boolean alwaysRemember;
|
private boolean alwaysRemember;
|
||||||
private String key;
|
private String key;
|
||||||
private int tokenValiditySeconds = TWO_WEEKS_S;
|
private int tokenValiditySeconds = TWO_WEEKS_S;
|
||||||
|
private boolean useSecureCookie = false;
|
||||||
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
Assert.hasLength(key);
|
Assert.hasLength(key);
|
||||||
@ -308,6 +309,7 @@ public abstract class AbstractRememberMeServices implements RememberMeServices,
|
|||||||
Cookie cookie = new Cookie(cookieName, cookieValue);
|
Cookie cookie = new Cookie(cookieName, cookieValue);
|
||||||
cookie.setMaxAge(maxAge);
|
cookie.setMaxAge(maxAge);
|
||||||
cookie.setPath(StringUtils.hasLength(request.getContextPath()) ? request.getContextPath() : "/");
|
cookie.setPath(StringUtils.hasLength(request.getContextPath()) ? request.getContextPath() : "/");
|
||||||
|
cookie.setSecure(useSecureCookie);
|
||||||
response.addCookie(cookie);
|
response.addCookie(cookie);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,6 +376,10 @@ public abstract class AbstractRememberMeServices implements RememberMeServices,
|
|||||||
return tokenValiditySeconds;
|
return tokenValiditySeconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUseSecureCookie(boolean useSecureCookie) {
|
||||||
|
this.useSecureCookie = useSecureCookie;
|
||||||
|
}
|
||||||
|
|
||||||
protected AuthenticationDetailsSource getAuthenticationDetailsSource() {
|
protected AuthenticationDetailsSource getAuthenticationDetailsSource() {
|
||||||
return authenticationDetailsSource;
|
return authenticationDetailsSource;
|
||||||
}
|
}
|
||||||
|
@ -226,7 +226,24 @@ public class AbstractRememberMeServicesTests {
|
|||||||
assertEquals("mycookie", cookie.getValue());
|
assertEquals("mycookie", cookie.getValue());
|
||||||
assertEquals("mycookiename", cookie.getName());
|
assertEquals("mycookiename", cookie.getName());
|
||||||
assertEquals("contextpath", cookie.getPath());
|
assertEquals("contextpath", cookie.getPath());
|
||||||
|
assertFalse(cookie.getSecure());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setCookieSetsSecureFlagIfConfigured() throws Exception {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
request.setContextPath("contextpath");
|
||||||
|
|
||||||
|
MockRememberMeServices services = new MockRememberMeServices() {
|
||||||
|
protected String encodeCookie(String[] cookieTokens) {
|
||||||
|
return cookieTokens[0];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
services.setUseSecureCookie(true);
|
||||||
|
services.setCookie(new String[] {"mycookie"}, 1000, request, response);
|
||||||
|
Cookie cookie = response.getCookie(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
|
||||||
|
assertTrue(cookie.getSecure());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Cookie[] createLoginCookie(String cookieToken) {
|
private Cookie[] createLoginCookie(String cookieToken) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user