Skip to content

Commit 21119ea

Browse files
Max BatischevMax Batischev
authored andcommitted
Add support remember-me cookie customization
Closes spring-projectsgh-14990
1 parent 3acd2c6 commit 21119ea

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import java.net.URLEncoder;
2222
import java.nio.charset.StandardCharsets;
2323
import java.util.Base64;
24+
import java.util.function.Consumer;
2425

2526
import jakarta.servlet.http.Cookie;
2627
import jakarta.servlet.http.HttpServletRequest;
@@ -97,6 +98,9 @@ public abstract class AbstractRememberMeServices
9798

9899
private GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();
99100

101+
private Consumer<Cookie> cookieCustomizer = (cookie) -> {
102+
};
103+
100104
protected AbstractRememberMeServices(String key, UserDetailsService userDetailsService) {
101105
Assert.hasLength(key, "key cannot be empty or null");
102106
Assert.notNull(userDetailsService, "UserDetailsService cannot be null");
@@ -373,6 +377,9 @@ protected void setCookie(String[] tokens, int maxAge, HttpServletRequest request
373377
}
374378
cookie.setSecure((this.useSecureCookie != null) ? this.useSecureCookie : request.isSecure());
375379
cookie.setHttpOnly(true);
380+
381+
this.cookieCustomizer.accept(cookie);
382+
376383
response.addCookie(cookie);
377384
}
378385

@@ -492,4 +499,14 @@ public void setMessageSource(MessageSource messageSource) {
492499
this.messages = new MessageSourceAccessor(messageSource);
493500
}
494501

502+
/**
503+
* Sets the {@link Consumer}, allowing customization of cookie.
504+
* @param cookieCustomizer customize for cookie
505+
* @since 6.4
506+
*/
507+
public void setCookieCustomizer(Consumer<Cookie> cookieCustomizer) {
508+
Assert.notNull(cookieCustomizer, "cookieCustomizer cannot be null");
509+
this.cookieCustomizer = cookieCustomizer;
510+
}
511+
495512
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -402,6 +402,17 @@ public void setMessageSourceWhenNotNullThenCanGet() {
402402
verify(source).getMessage(eq(code), any(), any());
403403
}
404404

405+
@Test
406+
public void setCookieCustomAttribute() {
407+
MockHttpServletRequest request = new MockHttpServletRequest();
408+
MockHttpServletResponse response = new MockHttpServletResponse();
409+
MockRememberMeServices services = new MockRememberMeServices(this.uds);
410+
services.setCookieCustomizer((cookie) -> cookie.setAttribute("attr1", "value1"));
411+
services.setCookie(new String[] { "mycookie" }, 1000, request, response);
412+
Cookie cookie = response.getCookie(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
413+
assertThat(cookie.getAttribute("attr1")).isEqualTo("value1");
414+
}
415+
405416
private Cookie[] createLoginCookie(String cookieToken) {
406417
MockRememberMeServices services = new MockRememberMeServices(this.uds);
407418
Cookie cookie = new Cookie(AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY,

0 commit comments

Comments
 (0)