Skip to content

Commit 0c549ee

Browse files
Use SHA256 by default in Remember Me
Closes gh-11520
1 parent db9d60e commit 0c549ee

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

docs/modules/ROOT/pages/servlet/authentication/rememberme.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ A `key` is shared between this authentication provider and the `TokenBasedRememb
114114
In addition, `TokenBasedRememberMeServices` requires a `UserDetailsService`, from which it can retrieve the username and password for signature comparison purposes and generate the `RememberMeAuthenticationToken` to contain the correct `GrantedAuthority` instances.
115115
`TokenBasedRememberMeServices` also implements Spring Security's `LogoutHandler` interface so that it can be used with `LogoutFilter` to have the cookie cleared automatically.
116116

117-
By default, this implementation uses the MD5 algorithm to encode the token signature.
117+
By default, this implementation uses the SHA-256 algorithm to encode the token signature.
118118
To verify the token signature, the algorithm retrieved from `algorithmName` is parsed and used.
119-
If no `algorithmName` is present, the default matching algorithm will be used, which is MD5.
119+
If no `algorithmName` is present, the default matching algorithm will be used, which is SHA-256.
120120
You can specify different algorithms for signature encoding and for signature matching, this allows users to safely upgrade to a different encoding algorithm while still able to verify old ones if there is no `algorithmName` present.
121121
To do that you can specify your customized `TokenBasedRememberMeServices` as a Bean and use it in the configuration.
122122

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@
9494
*/
9595
public class TokenBasedRememberMeServices extends AbstractRememberMeServices {
9696

97-
private static final RememberMeTokenAlgorithm DEFAULT_MATCHING_ALGORITHM = RememberMeTokenAlgorithm.MD5;
97+
private static final RememberMeTokenAlgorithm DEFAULT_MATCHING_ALGORITHM = RememberMeTokenAlgorithm.SHA256;
9898

99-
private static final RememberMeTokenAlgorithm DEFAULT_ENCODING_ALGORITHM = RememberMeTokenAlgorithm.MD5;
99+
private static final RememberMeTokenAlgorithm DEFAULT_ENCODING_ALGORITHM = RememberMeTokenAlgorithm.SHA256;
100100

101101
private final RememberMeTokenAlgorithm encodingAlgorithm;
102102

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public void loginSuccessWhenDefaultEncodingAlgorithmThenContainsAlgorithmName()
407407
assertThat(cookie.getMaxAge()).isEqualTo(this.services.getTokenValiditySeconds());
408408
assertThat(CodecTestUtils.isBase64(cookie.getValue().getBytes())).isTrue();
409409
assertThat(new Date().before(new Date(determineExpiryTimeFromBased64EncodedToken(cookie.getValue())))).isTrue();
410-
assertThat("MD5").isEqualTo(determineAlgorithmNameFromBase64EncodedToken(cookie.getValue()));
410+
assertThat("SHA256").isEqualTo(determineAlgorithmNameFromBase64EncodedToken(cookie.getValue()));
411411
}
412412

413413
@Test
@@ -459,11 +459,11 @@ public void constructorWhenEncodingAlgorithmNullThenException() {
459459
}
460460

461461
@Test
462-
public void constructorWhenNoEncodingAlgorithmSpecifiedThenMd5() {
462+
public void constructorWhenNoEncodingAlgorithmSpecifiedThenSha256() {
463463
TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices("key", this.uds);
464464
RememberMeTokenAlgorithm encodingAlgorithm = (RememberMeTokenAlgorithm) ReflectionTestUtils
465465
.getField(rememberMeServices, "encodingAlgorithm");
466-
assertThat(encodingAlgorithm).isSameAs(RememberMeTokenAlgorithm.MD5);
466+
assertThat(encodingAlgorithm).isSameAs(RememberMeTokenAlgorithm.SHA256);
467467
}
468468

469469
}

0 commit comments

Comments
 (0)