20
20
21
21
import javax .servlet .http .Cookie ;
22
22
23
- import org .apache .commons .codec .binary .Base64 ;
24
- import org .apache .commons .codec .digest .DigestUtils ;
25
23
import org .junit .jupiter .api .BeforeEach ;
26
24
import org .junit .jupiter .api .Test ;
27
25
34
32
import org .springframework .security .core .userdetails .UserDetails ;
35
33
import org .springframework .security .core .userdetails .UserDetailsService ;
36
34
import org .springframework .security .core .userdetails .UsernameNotFoundException ;
35
+ import org .springframework .security .test .web .CodecTestUtils ;
37
36
import org .springframework .util .StringUtils ;
38
37
39
38
import static org .assertj .core .api .Assertions .assertThat ;
@@ -77,7 +76,7 @@ void udsWillReturnNull() {
77
76
}
78
77
79
78
private long determineExpiryTimeFromBased64EncodedToken (String validToken ) {
80
- String cookieAsPlainText = new String ( Base64 .decodeBase64 (validToken . getBytes ()) );
79
+ String cookieAsPlainText = CodecTestUtils .decodeBase64 (validToken );
81
80
String [] cookieTokens = StringUtils .delimitedListToStringArray (cookieAsPlainText , ":" );
82
81
if (cookieTokens .length == 3 ) {
83
82
try {
@@ -93,9 +92,9 @@ private String generateCorrectCookieContentForToken(long expiryTime, String user
93
92
// format is:
94
93
// username + ":" + expiryTime + ":" + Md5Hex(username + ":" + expiryTime + ":" +
95
94
// password + ":" + key)
96
- String signatureValue = DigestUtils .md5Hex (username + ":" + expiryTime + ":" + password + ":" + key );
95
+ String signatureValue = CodecTestUtils .md5Hex (username + ":" + expiryTime + ":" + password + ":" + key );
97
96
String tokenValue = username + ":" + expiryTime + ":" + signatureValue ;
98
- return new String ( Base64 .encodeBase64 (tokenValue . getBytes ()) );
97
+ return CodecTestUtils .encodeBase64 (tokenValue );
99
98
}
100
99
101
100
@ Test
@@ -135,7 +134,7 @@ public void autoLoginReturnsNullForExpiredCookieAndClearsCookie() {
135
134
@ Test
136
135
public void autoLoginReturnsNullAndClearsCookieIfMissingThreeTokensInCookieValue () {
137
136
Cookie cookie = new Cookie (AbstractRememberMeServices .SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY ,
138
- new String ( Base64 .encodeBase64 ("x" . getBytes ()) ));
137
+ CodecTestUtils .encodeBase64 ("x" ));
139
138
MockHttpServletRequest request = new MockHttpServletRequest ();
140
139
request .setCookies (cookie );
141
140
MockHttpServletResponse response = new MockHttpServletResponse ();
@@ -176,7 +175,7 @@ public void autoLoginClearsCookieIfSignatureBlocksDoesNotMatchExpectedValue() {
176
175
@ Test
177
176
public void autoLoginClearsCookieIfTokenDoesNotContainANumberInCookieValue () {
178
177
Cookie cookie = new Cookie (AbstractRememberMeServices .SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY ,
179
- new String ( Base64 .encodeBase64 ("username:NOT_A_NUMBER:signature" . getBytes ()) ));
178
+ CodecTestUtils .encodeBase64 ("username:NOT_A_NUMBER:signature" ));
180
179
MockHttpServletRequest request = new MockHttpServletRequest ();
181
180
request .setCookies (cookie );
182
181
MockHttpServletResponse response = new MockHttpServletResponse ();
@@ -276,7 +275,7 @@ public void loginSuccessNormalWithNonUserDetailsBasedPrincipalSetsExpectedCookie
276
275
assertThat (Long .parseLong (expiryTime ) > expectedExpiryTime - 10000 ).isTrue ();
277
276
assertThat (cookie ).isNotNull ();
278
277
assertThat (cookie .getMaxAge ()).isEqualTo (this .services .getTokenValiditySeconds ());
279
- assertThat (Base64 . isArrayByteBase64 (cookie .getValue ().getBytes ())).isTrue ();
278
+ assertThat (CodecTestUtils . isBase64 (cookie .getValue ().getBytes ())).isTrue ();
280
279
assertThat (new Date ().before (new Date (determineExpiryTimeFromBased64EncodedToken (cookie .getValue ())))).isTrue ();
281
280
}
282
281
@@ -290,7 +289,7 @@ public void loginSuccessNormalWithUserDetailsBasedPrincipalSetsExpectedCookie()
290
289
Cookie cookie = response .getCookie (AbstractRememberMeServices .SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY );
291
290
assertThat (cookie ).isNotNull ();
292
291
assertThat (cookie .getMaxAge ()).isEqualTo (this .services .getTokenValiditySeconds ());
293
- assertThat (Base64 . isArrayByteBase64 (cookie .getValue ().getBytes ())).isTrue ();
292
+ assertThat (CodecTestUtils . isBase64 (cookie .getValue ().getBytes ())).isTrue ();
294
293
assertThat (new Date ().before (new Date (determineExpiryTimeFromBased64EncodedToken (cookie .getValue ())))).isTrue ();
295
294
}
296
295
@@ -316,7 +315,7 @@ public void negativeValidityPeriodIsSetOnCookieButExpiryTimeRemainsAtTwoWeeks()
316
315
assertThat (determineExpiryTimeFromBased64EncodedToken (cookie .getValue ())
317
316
- System .currentTimeMillis () > AbstractRememberMeServices .TWO_WEEKS_S - 50 ).isTrue ();
318
317
assertThat (cookie .getMaxAge ()).isEqualTo (-1 );
319
- assertThat (Base64 . isArrayByteBase64 (cookie .getValue ().getBytes ())).isTrue ();
318
+ assertThat (CodecTestUtils . isBase64 (cookie .getValue ().getBytes ())).isTrue ();
320
319
}
321
320
322
321
}
0 commit comments