Skip to content

Commit fc802e1

Browse files
dongmyojzheaux
authored andcommitted
Remove Servlet 2.5 and 3.0 Support for Remember Me and CSRF
Fixes: gh-6263, Fixes: gh-6262
1 parent 0d2af41 commit fc802e1

File tree

5 files changed

+15
-227
lines changed

5 files changed

+15
-227
lines changed

web/src/main/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServices.java

+3-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.security.web.authentication.rememberme;
1717

1818
import java.io.UnsupportedEncodingException;
19-
import java.lang.reflect.Method;
2019
import java.util.Base64;
2120
import java.net.URLDecoder;
2221
import java.net.URLEncoder;
@@ -46,7 +45,6 @@
4645
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
4746
import org.springframework.security.web.authentication.logout.LogoutHandler;
4847
import org.springframework.util.Assert;
49-
import org.springframework.util.ReflectionUtils;
5048
import org.springframework.util.StringUtils;
5149

5250
/**
@@ -86,16 +84,13 @@ public abstract class AbstractRememberMeServices implements RememberMeServices,
8684
private String key;
8785
private int tokenValiditySeconds = TWO_WEEKS_S;
8886
private Boolean useSecureCookie = null;
89-
private Method setHttpOnlyMethod;
9087
private GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();
9188

9289
protected AbstractRememberMeServices(String key, UserDetailsService userDetailsService) {
9390
Assert.hasLength(key, "key cannot be empty or null");
9491
Assert.notNull(userDetailsService, "UserDetailsService cannot be null");
9592
this.key = key;
9693
this.userDetailsService = userDetailsService;
97-
this.setHttpOnlyMethod = ReflectionUtils.findMethod(Cookie.class, "setHttpOnly",
98-
boolean.class);
9994
}
10095

10196
@Override
@@ -396,8 +391,8 @@ protected void cancelCookie(HttpServletRequest request, HttpServletResponse resp
396391
*
397392
* By default a secure cookie will be used if the connection is secure. You can set
398393
* the {@code useSecureCookie} property to {@code false} to override this. If you set
399-
* it to {@code true}, the cookie will always be flagged as secure. If Servlet 3.0 is
400-
* used, the cookie will be marked as HttpOnly.
394+
* it to {@code true}, the cookie will always be flagged as secure. By default the cookie
395+
* will be marked as HttpOnly.
401396
*
402397
* @param tokens the tokens which will be encoded to make the cookie value.
403398
* @param maxAge the value passed to {@link Cookie#setMaxAge(int)}
@@ -424,12 +419,7 @@ protected void setCookie(String[] tokens, int maxAge, HttpServletRequest request
424419
cookie.setSecure(useSecureCookie);
425420
}
426421

427-
if (setHttpOnlyMethod != null) {
428-
ReflectionUtils.invokeMethod(setHttpOnlyMethod, cookie, Boolean.TRUE);
429-
}
430-
else if (logger.isDebugEnabled()) {
431-
logger.debug("Note: Cookie will not be marked as HttpOnly because you are not using Servlet 3.0 (Cookie#setHttpOnly(boolean) was not found).");
432-
}
422+
cookie.setHttpOnly(true);
433423

434424
response.addCookie(cookie);
435425
}

web/src/main/java/org/springframework/security/web/csrf/CookieCsrfTokenRepository.java

+4-20
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
package org.springframework.security.web.csrf;
1818

19-
import java.lang.reflect.Method;
2019
import java.util.UUID;
2120

2221
import javax.servlet.http.Cookie;
2322
import javax.servlet.http.HttpServletRequest;
2423
import javax.servlet.http.HttpServletResponse;
2524

2625
import org.springframework.util.Assert;
27-
import org.springframework.util.ReflectionUtils;
2826
import org.springframework.util.StringUtils;
2927
import org.springframework.web.util.WebUtils;
3028

@@ -49,19 +47,13 @@ public final class CookieCsrfTokenRepository implements CsrfTokenRepository {
4947

5048
private String cookieName = DEFAULT_CSRF_COOKIE_NAME;
5149

52-
private final Method setHttpOnlyMethod;
53-
54-
private boolean cookieHttpOnly;
50+
private boolean cookieHttpOnly = true;
5551

5652
private String cookiePath;
5753

5854
private String cookieDomain;
5955

6056
public CookieCsrfTokenRepository() {
61-
this.setHttpOnlyMethod = ReflectionUtils.findMethod(Cookie.class, "setHttpOnly", boolean.class);
62-
if (this.setHttpOnlyMethod != null) {
63-
this.cookieHttpOnly = true;
64-
}
6557
}
6658

6759
@Override
@@ -87,9 +79,7 @@ public void saveToken(CsrfToken token, HttpServletRequest request,
8779
else {
8880
cookie.setMaxAge(-1);
8981
}
90-
if (cookieHttpOnly && setHttpOnlyMethod != null) {
91-
ReflectionUtils.invokeMethod(setHttpOnlyMethod, cookie, Boolean.TRUE);
92-
}
82+
cookie.setHttpOnly(cookieHttpOnly);
9383
if (this.cookieDomain != null && !this.cookieDomain.isEmpty()) {
9484
cookie.setDomain(this.cookieDomain);
9585
}
@@ -145,17 +135,11 @@ public void setCookieName(String cookieName) {
145135

146136
/**
147137
* Sets the HttpOnly attribute on the cookie containing the CSRF token.
148-
* The cookie will only be marked as HttpOnly if both <code>cookieHttpOnly</code> is <code>true</code> and the underlying version of Servlet is 3.0 or greater.
149-
* Defaults to <code>true</code> if the underlying version of Servlet is 3.0 or greater.
150-
* NOTE: The {@link Cookie#setHttpOnly(boolean)} was introduced in Servlet 3.0.
138+
* Defaults to <code>true</code>.
151139
*
152-
* @param cookieHttpOnly <code>true</code> sets the HttpOnly attribute, <code>false</code> does not set it (depending on Servlet version)
153-
* @throws IllegalArgumentException if <code>cookieHttpOnly</code> is <code>true</code> and the underlying version of Servlet is less than 3.0
140+
* @param cookieHttpOnly <code>true</code> sets the HttpOnly attribute, <code>false</code> does not set it
154141
*/
155142
public void setCookieHttpOnly(boolean cookieHttpOnly) {
156-
if (cookieHttpOnly && setHttpOnlyMethod == null) {
157-
throw new IllegalArgumentException("Cookie will not be marked as HttpOnly because you are using a version of Servlet less than 3.0. NOTE: The Cookie#setHttpOnly(boolean) was introduced in Servlet 3.0.");
158-
}
159143
this.cookieHttpOnly = cookieHttpOnly;
160144
}
161145

web/src/test/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServicesServlet3Tests.java

-87
This file was deleted.

web/src/test/java/org/springframework/security/web/authentication/rememberme/AbstractRememberMeServicesTests.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package org.springframework.security.web.authentication.rememberme;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.powermock.api.mockito.PowerMockito.spy;
20-
import static org.powermock.api.mockito.PowerMockito.when;
2119

2220
import javax.servlet.http.Cookie;
2321
import javax.servlet.http.HttpServletRequest;
@@ -41,7 +39,6 @@
4139
import org.springframework.security.core.userdetails.UserDetails;
4240
import org.springframework.security.core.userdetails.UserDetailsService;
4341
import org.springframework.security.core.userdetails.UsernameNotFoundException;
44-
import org.springframework.test.util.ReflectionTestUtils;
4542
import org.springframework.util.ReflectionUtils;
4643
import org.springframework.util.StringUtils;
4744

@@ -369,17 +366,16 @@ protected String encodeCookie(String[] cookieTokens) {
369366
}
370367

371368
@Test
372-
public void setHttpOnlyIgnoredForServlet25() throws Exception {
373-
spy(ReflectionUtils.class);
374-
when(ReflectionUtils.findMethod(Cookie.class, "setHttpOnly",
375-
boolean.class)).thenReturn(null);
369+
public void setCookieSetsIsHttpOnlyFlagByDefault() throws Exception {
370+
MockHttpServletRequest request = new MockHttpServletRequest();
371+
MockHttpServletResponse response = new MockHttpServletResponse();
372+
request.setContextPath("contextpath");
376373

377374
MockRememberMeServices services = new MockRememberMeServices(uds);
378-
assertThat(ReflectionTestUtils.getField(services, "setHttpOnlyMethod")).isNull();
379-
380-
services = new MockRememberMeServices("key",
381-
new MockUserDetailsService(joe, false));
382-
assertThat(ReflectionTestUtils.getField(services, "setHttpOnlyMethod")).isNull();
375+
services.setCookie(new String[] { "mycookie" }, 1000, request, response);
376+
Cookie cookie = response.getCookie(
377+
AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
378+
assertThat(cookie.isHttpOnly()).isTrue();
383379
}
384380

385381
// SEC-2791

web/src/test/java/org/springframework/security/web/csrf/CookieCsrfTokenRepositoryServlet3Tests.java

-95
This file was deleted.

0 commit comments

Comments
 (0)