-
Notifications
You must be signed in to change notification settings - Fork 6.1k
CookieClearingLogoutHandler enhancement #6116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2002-2016 the original author or authors. | ||
* Copyright 2002-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -24,28 +24,49 @@ | |
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A logout handler which clears a defined list of cookies, using the context path as the | ||
* cookie path. | ||
* A logout handler which clears either | ||
* - A defined list of cookie names, using the context path as the cookie path | ||
* OR | ||
* - A given list of Cookies | ||
* | ||
* @author Luke Taylor | ||
* @since 3.1 | ||
*/ | ||
public final class CookieClearingLogoutHandler implements LogoutHandler { | ||
private final List<String> cookiesToClear; | ||
private final List<Object> cookiesToClear; | ||
|
||
public CookieClearingLogoutHandler(String... cookiesToClear) { | ||
Assert.notNull(cookiesToClear, "List of cookies cannot be null"); | ||
this.cookiesToClear = Arrays.asList(cookiesToClear); | ||
this.cookiesToClear = Arrays.asList((Object[]) cookiesToClear); | ||
} | ||
|
||
/** | ||
* @since 5.X | ||
|
||
* @param cookiesToClear - One or more Cookie objects that must have maxAge of 0 | ||
*/ | ||
public CookieClearingLogoutHandler(Cookie... cookiesToClear) { | ||
Assert.notNull(cookiesToClear, "List of cookies cannot be null"); | ||
List<Object> cookieList = new ArrayList<Object>(); | ||
for (Cookie cookie : cookiesToClear) { | ||
Assert.isTrue(cookie.getMaxAge() == 0, "Cookie maxAge must be 0"); | ||
cookieList.add(cookie); | ||
} | ||
this.cookiesToClear = cookieList; | ||
} | ||
|
||
public void logout(HttpServletRequest request, HttpServletResponse response, | ||
Authentication authentication) { | ||
for (String cookieName : cookiesToClear) { | ||
Cookie cookie = new Cookie(cookieName, null); | ||
String cookiePath = request.getContextPath() + "/"; | ||
cookie.setPath(cookiePath); | ||
cookie.setMaxAge(0); | ||
response.addCookie(cookie); | ||
for (Object cookie : cookiesToClear) { | ||
Cookie realCookie = null; | ||
if (cookie instanceof String) { | ||
|
||
realCookie = new Cookie((String) cookie, null); | ||
String cookiePath = request.getContextPath() + "/"; | ||
realCookie.setPath(cookiePath); | ||
realCookie.setMaxAge(0); | ||
}else if (cookie instanceof Cookie){ | ||
realCookie = (Cookie) cookie; | ||
} | ||
response.addCookie(realCookie); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's change this to
List<Function<HttpServletRequest,Cookie>>
. In each of the constructors we can convert the arguments into aFunction<HttpServletRequest,Cookie>
.