|
@@ -16,24 +16,30 @@
|
|
package org.springframework.security.web.authentication.logout;
|
|
package org.springframework.security.web.authentication.logout;
|
|
|
|
|
|
|
|
|
|
-import org.springframework.security.core.Authentication;
|
|
|
|
-import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
-import org.springframework.util.Assert;
|
|
|
|
-
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
|
+import org.springframework.security.core.context.SecurityContext;
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Performs a logout by modifying the {@link org.springframework.security.core.context.SecurityContextHolder}.
|
|
* Performs a logout by modifying the {@link org.springframework.security.core.context.SecurityContextHolder}.
|
|
* <p>
|
|
* <p>
|
|
* Will also invalidate the {@link HttpSession} if {@link #isInvalidateHttpSession()} is <code>true</code> and the
|
|
* Will also invalidate the {@link HttpSession} if {@link #isInvalidateHttpSession()} is <code>true</code> and the
|
|
* session is not <code>null</code>.
|
|
* session is not <code>null</code>.
|
|
|
|
+ * <p>
|
|
|
|
+ * Will also remove the {@link Authentication} from the current {@link SecurityContext} if {@link #clearAuthentication}
|
|
|
|
+ * is set to true (default).
|
|
*
|
|
*
|
|
* @author Ben Alex
|
|
* @author Ben Alex
|
|
|
|
+ * @author Rob Winch
|
|
*/
|
|
*/
|
|
public class SecurityContextLogoutHandler implements LogoutHandler {
|
|
public class SecurityContextLogoutHandler implements LogoutHandler {
|
|
private boolean invalidateHttpSession = true;
|
|
private boolean invalidateHttpSession = true;
|
|
|
|
+ private boolean clearAuthentication = true;
|
|
|
|
|
|
//~ Methods ========================================================================================================
|
|
//~ Methods ========================================================================================================
|
|
|
|
|
|
@@ -53,6 +59,11 @@ public class SecurityContextLogoutHandler implements LogoutHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if(clearAuthentication) {
|
|
|
|
+ SecurityContext context = SecurityContextHolder.getContext();
|
|
|
|
+ context.setAuthentication(null);
|
|
|
|
+ }
|
|
|
|
+
|
|
SecurityContextHolder.clearContext();
|
|
SecurityContextHolder.clearContext();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -70,4 +81,14 @@ public class SecurityContextLogoutHandler implements LogoutHandler {
|
|
this.invalidateHttpSession = invalidateHttpSession;
|
|
this.invalidateHttpSession = invalidateHttpSession;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * If true, removes the {@link Authentication} from the {@link SecurityContext} to prevent issues with concurrent
|
|
|
|
+ * requests.
|
|
|
|
+ *
|
|
|
|
+ * @param clearAuthentication true if you wish to clear the {@link Authentication} from the {@link SecurityContext}
|
|
|
|
+ * (default) or false if the {@link Authentication} should not be removed.
|
|
|
|
+ */
|
|
|
|
+ public void setClearAuthentication(boolean clearAuthentication) {
|
|
|
|
+ this.clearAuthentication = clearAuthentication;
|
|
|
|
+ }
|
|
}
|
|
}
|