Skip to content

Commit 27e17d5

Browse files
committed
Fix CookieClearingLogoutHandler cookie secure flag:
It is better to mark cookie secure flag with request.isSecure() to ensure cookie identity is same
1 parent 2015f39 commit 27e17d5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

web/src/main/java/org/springframework/security/web/authentication/logout/CookieClearingLogoutHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* - A given list of Cookies
3333
*
3434
* @author Luke Taylor
35+
* @author Onur Kagan Ozcan
3536
* @since 3.1
3637
*/
3738
public final class CookieClearingLogoutHandler implements LogoutHandler {
@@ -46,6 +47,7 @@ public CookieClearingLogoutHandler(String... cookiesToClear) {
4647
String cookiePath = request.getContextPath() + "/";
4748
cookie.setPath(cookiePath);
4849
cookie.setMaxAge(0);
50+
cookie.setSecure(request.isSecure());
4951
return cookie;
5052
};
5153
cookieList.add(f);

web/src/test/java/org/springframework/security/web/authentication/logout/CookieClearingLogoutHandlerTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @author Luke Taylor
30+
* @author Onur Kagan Ozcan
3031
*/
3132
public class CookieClearingLogoutHandlerTests {
3233

@@ -61,6 +62,18 @@ public void configuredCookiesAreCleared() {
6162
}
6263
}
6364

65+
@Test
66+
public void configuredCookieIsSecure() {
67+
MockHttpServletResponse response = new MockHttpServletResponse();
68+
MockHttpServletRequest request = new MockHttpServletRequest();
69+
request.setSecure(true);
70+
request.setContextPath("/app");
71+
CookieClearingLogoutHandler handler = new CookieClearingLogoutHandler("my_cookie");
72+
handler.logout(request, response, mock(Authentication.class));
73+
assertThat(response.getCookies()).hasSize(1);
74+
assertThat(response.getCookies()[0].getSecure()).isTrue();
75+
}
76+
6477
@Test
6578
public void passedInCookiesAreCleared() {
6679
MockHttpServletResponse response = new MockHttpServletResponse();

0 commit comments

Comments
 (0)