|
@@ -16,6 +16,8 @@
|
|
|
package org.springframework.security.ui.logout;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
|
|
|
import javax.servlet.FilterChain;
|
|
|
import javax.servlet.ServletException;
|
|
@@ -23,11 +25,11 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
import org.springframework.security.Authentication;
|
|
|
-import org.springframework.security.ui.SpringSecurityFilter;
|
|
|
+import org.springframework.security.context.SecurityContextHolder;
|
|
|
import org.springframework.security.ui.FilterChainOrder;
|
|
|
-import org.springframework.security.util.RedirectUtils;
|
|
|
+import org.springframework.security.ui.LogoutSuccessHandler;
|
|
|
+import org.springframework.security.ui.SpringSecurityFilter;
|
|
|
import org.springframework.security.util.UrlUtils;
|
|
|
-import org.springframework.security.context.SecurityContextHolder;
|
|
|
import org.springframework.util.Assert;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
@@ -37,11 +39,10 @@ import org.springframework.util.StringUtils;
|
|
|
* Polls a series of {@link LogoutHandler}s. The handlers should be specified in the order they are required.
|
|
|
* Generally you will want to call logout handlers <code>TokenBasedRememberMeServices</code> and
|
|
|
* <code>SecurityContextLogoutHandler</code> (in that order).
|
|
|
- * </p>
|
|
|
* <p>
|
|
|
- * After logout, the URL specified by {@link #logoutSuccessUrl} will be shown.
|
|
|
- * </p>
|
|
|
- *
|
|
|
+ * After logout, a redirect will be performed to the URL determined by either the configured
|
|
|
+ * <tt>LogoutSuccessHandler</tt> or the <tt>logoutSuccessUrl</tt>, depending on which constructor was used.
|
|
|
+ *
|
|
|
* @author Ben Alex
|
|
|
* @version $Id$
|
|
|
*/
|
|
@@ -50,17 +51,32 @@ public class LogoutFilter extends SpringSecurityFilter {
|
|
|
//~ Instance fields ================================================================================================
|
|
|
|
|
|
private String filterProcessesUrl = "/j_spring_security_logout";
|
|
|
- private String logoutSuccessUrl;
|
|
|
- private LogoutHandler[] handlers;
|
|
|
- private boolean useRelativeContext;
|
|
|
+ private List<LogoutHandler> handlers; private LogoutSuccessHandler logoutSuccessHandler;
|
|
|
|
|
|
//~ Constructors ===================================================================================================
|
|
|
|
|
|
- public LogoutFilter(String logoutSuccessUrl, LogoutHandler[] handlers) {
|
|
|
+ /**
|
|
|
+ * Constructor which takes a <tt>LogoutSuccessHandler</tt> instance to determine the target destination
|
|
|
+ * after logging out. The list of <tt>LogoutHandler</tt>s are intended to perform the actual logout functionality
|
|
|
+ * (such as clearing the security context, invalidating the session, etc.).
|
|
|
+ */
|
|
|
+ public LogoutFilter(LogoutSuccessHandler logoutSuccessHandler, LogoutHandler... handlers) {
|
|
|
Assert.notEmpty(handlers, "LogoutHandlers are required");
|
|
|
- this.logoutSuccessUrl = logoutSuccessUrl;
|
|
|
- Assert.isTrue(UrlUtils.isValidRedirectUrl(logoutSuccessUrl), logoutSuccessUrl + " isn't a valid redirect URL");
|
|
|
- this.handlers = handlers;
|
|
|
+ this.handlers = Arrays.asList(handlers);
|
|
|
+ Assert.notNull(logoutSuccessHandler, "logoutSuccessHandler cannot be null");
|
|
|
+ this.logoutSuccessHandler = logoutSuccessHandler;
|
|
|
+ }
|
|
|
+
|
|
|
+ public LogoutFilter(String logoutSuccessUrl, LogoutHandler... handlers) {
|
|
|
+ Assert.notEmpty(handlers, "LogoutHandlers are required");
|
|
|
+ this.handlers = Arrays.asList(handlers);
|
|
|
+ Assert.isTrue(!StringUtils.hasLength(logoutSuccessUrl) ||
|
|
|
+ UrlUtils.isValidRedirectUrl(logoutSuccessUrl), logoutSuccessUrl + " isn't a valid redirect URL");
|
|
|
+ SimpleUrlLogoutSuccessHandler urlLogoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
|
|
|
+ if (StringUtils.hasText(logoutSuccessUrl)) {
|
|
|
+ urlLogoutSuccessHandler.setDefaultTargetUrl(logoutSuccessUrl);
|
|
|
+ }
|
|
|
+ logoutSuccessHandler = urlLogoutSuccessHandler;
|
|
|
}
|
|
|
|
|
|
//~ Methods ========================================================================================================
|
|
@@ -72,16 +88,14 @@ public class LogoutFilter extends SpringSecurityFilter {
|
|
|
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
- logger.debug("Logging out user '" + auth + "' and redirecting to logout page");
|
|
|
+ logger.debug("Logging out user '" + auth + "' and transferring to logout destination");
|
|
|
}
|
|
|
|
|
|
- for (int i = 0; i < handlers.length; i++) {
|
|
|
- handlers[i].logout(request, response, auth);
|
|
|
+ for (LogoutHandler handler : handlers) {
|
|
|
+ handler.logout(request, response, auth);
|
|
|
}
|
|
|
|
|
|
- String targetUrl = determineTargetUrl(request, response);
|
|
|
-
|
|
|
- sendRedirect(request, response, targetUrl);
|
|
|
+ logoutSuccessHandler.onLogoutSuccess(request, response, auth);
|
|
|
|
|
|
return;
|
|
|
}
|
|
@@ -120,65 +134,16 @@ public class LogoutFilter extends SpringSecurityFilter {
|
|
|
return uri.endsWith(request.getContextPath() + filterProcessesUrl);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Returns the target URL to redirect to after logout.
|
|
|
- * <p>
|
|
|
- * By default it will check for a <tt>logoutSuccessUrl</tt> parameter in
|
|
|
- * the request and use this. If that isn't present it will use the configured <tt>logoutSuccessUrl</tt>. If this
|
|
|
- * hasn't been set it will check the Referer header and use the URL from there.
|
|
|
- *
|
|
|
- */
|
|
|
- protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {
|
|
|
- String targetUrl = request.getParameter("logoutSuccessUrl");
|
|
|
-
|
|
|
- if(!StringUtils.hasLength(targetUrl)) {
|
|
|
- targetUrl = getLogoutSuccessUrl();
|
|
|
- }
|
|
|
-
|
|
|
- if (!StringUtils.hasLength(targetUrl)) {
|
|
|
- targetUrl = request.getHeader("Referer");
|
|
|
- }
|
|
|
-
|
|
|
- if (!StringUtils.hasLength(targetUrl)) {
|
|
|
- targetUrl = "/";
|
|
|
- }
|
|
|
-
|
|
|
- return targetUrl;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Allow subclasses to modify the redirection message.
|
|
|
- *
|
|
|
- * @param request the request
|
|
|
- * @param response the response
|
|
|
- * @param url the URL to redirect to
|
|
|
- *
|
|
|
- * @throws IOException in the event of any failure
|
|
|
- */
|
|
|
- protected void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url)
|
|
|
- throws IOException {
|
|
|
-
|
|
|
- RedirectUtils.sendRedirect(request, response, url, useRelativeContext);
|
|
|
- }
|
|
|
-
|
|
|
public void setFilterProcessesUrl(String filterProcessesUrl) {
|
|
|
- Assert.hasText(filterProcessesUrl, "FilterProcessesUrl required");
|
|
|
- Assert.isTrue(UrlUtils.isValidRedirectUrl(filterProcessesUrl), filterProcessesUrl + " isn't a valid redirect URL");
|
|
|
+ Assert.isTrue(UrlUtils.isValidRedirectUrl(filterProcessesUrl), filterProcessesUrl + " isn't a valid value for" +
|
|
|
+ " 'filterProcessesUrl'");
|
|
|
this.filterProcessesUrl = filterProcessesUrl;
|
|
|
}
|
|
|
|
|
|
- protected String getLogoutSuccessUrl() {
|
|
|
- return logoutSuccessUrl;
|
|
|
- }
|
|
|
-
|
|
|
protected String getFilterProcessesUrl() {
|
|
|
return filterProcessesUrl;
|
|
|
}
|
|
|
|
|
|
- public void setUseRelativeContext(boolean useRelativeContext) {
|
|
|
- this.useRelativeContext = useRelativeContext;
|
|
|
- }
|
|
|
-
|
|
|
public int getOrder() {
|
|
|
return FilterChainOrder.LOGOUT_FILTER;
|
|
|
}
|