|
@@ -34,6 +34,7 @@ import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
import org.springframework.security.web.authentication.RememberMeServices;
|
|
import org.springframework.security.web.authentication.RememberMeServices;
|
|
import org.springframework.security.web.context.NullSecurityContextRepository;
|
|
import org.springframework.security.web.context.NullSecurityContextRepository;
|
|
@@ -67,6 +68,9 @@ import org.springframework.web.filter.GenericFilterBean;
|
|
*/
|
|
*/
|
|
public class RememberMeAuthenticationFilter extends GenericFilterBean implements ApplicationEventPublisherAware {
|
|
public class RememberMeAuthenticationFilter extends GenericFilterBean implements ApplicationEventPublisherAware {
|
|
|
|
|
|
|
|
+ private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
|
|
|
+ .getContextHolderStrategy();
|
|
|
|
+
|
|
private ApplicationEventPublisher eventPublisher;
|
|
private ApplicationEventPublisher eventPublisher;
|
|
|
|
|
|
private AuthenticationSuccessHandler successHandler;
|
|
private AuthenticationSuccessHandler successHandler;
|
|
@@ -99,10 +103,10 @@ public class RememberMeAuthenticationFilter extends GenericFilterBean implements
|
|
|
|
|
|
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
|
private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
|
throws IOException, ServletException {
|
|
throws IOException, ServletException {
|
|
- if (SecurityContextHolder.getContext().getAuthentication() != null) {
|
|
|
|
|
|
+ if (this.securityContextHolderStrategy.getContext().getAuthentication() != null) {
|
|
this.logger.debug(LogMessage
|
|
this.logger.debug(LogMessage
|
|
.of(() -> "SecurityContextHolder not populated with remember-me token, as it already contained: '"
|
|
.of(() -> "SecurityContextHolder not populated with remember-me token, as it already contained: '"
|
|
- + SecurityContextHolder.getContext().getAuthentication() + "'"));
|
|
|
|
|
|
+ + this.securityContextHolderStrategy.getContext().getAuthentication() + "'"));
|
|
chain.doFilter(request, response);
|
|
chain.doFilter(request, response);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -112,16 +116,16 @@ public class RememberMeAuthenticationFilter extends GenericFilterBean implements
|
|
try {
|
|
try {
|
|
rememberMeAuth = this.authenticationManager.authenticate(rememberMeAuth);
|
|
rememberMeAuth = this.authenticationManager.authenticate(rememberMeAuth);
|
|
// Store to SecurityContextHolder
|
|
// Store to SecurityContextHolder
|
|
- SecurityContext context = SecurityContextHolder.createEmptyContext();
|
|
|
|
|
|
+ SecurityContext context = this.securityContextHolderStrategy.createEmptyContext();
|
|
context.setAuthentication(rememberMeAuth);
|
|
context.setAuthentication(rememberMeAuth);
|
|
- SecurityContextHolder.setContext(context);
|
|
|
|
|
|
+ this.securityContextHolderStrategy.setContext(context);
|
|
onSuccessfulAuthentication(request, response, rememberMeAuth);
|
|
onSuccessfulAuthentication(request, response, rememberMeAuth);
|
|
this.logger.debug(LogMessage.of(() -> "SecurityContextHolder populated with remember-me token: '"
|
|
this.logger.debug(LogMessage.of(() -> "SecurityContextHolder populated with remember-me token: '"
|
|
- + SecurityContextHolder.getContext().getAuthentication() + "'"));
|
|
|
|
|
|
+ + this.securityContextHolderStrategy.getContext().getAuthentication() + "'"));
|
|
this.securityContextRepository.saveContext(context, request, response);
|
|
this.securityContextRepository.saveContext(context, request, response);
|
|
if (this.eventPublisher != null) {
|
|
if (this.eventPublisher != null) {
|
|
this.eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
|
|
this.eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
|
|
- SecurityContextHolder.getContext().getAuthentication(), this.getClass()));
|
|
|
|
|
|
+ this.securityContextHolderStrategy.getContext().getAuthentication(), this.getClass()));
|
|
}
|
|
}
|
|
if (this.successHandler != null) {
|
|
if (this.successHandler != null) {
|
|
this.successHandler.onAuthenticationSuccess(request, response, rememberMeAuth);
|
|
this.successHandler.onAuthenticationSuccess(request, response, rememberMeAuth);
|
|
@@ -196,4 +200,15 @@ public class RememberMeAuthenticationFilter extends GenericFilterBean implements
|
|
this.securityContextRepository = securityContextRepository;
|
|
this.securityContextRepository = securityContextRepository;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link SecurityContextHolderStrategy} to use. The default action is to use
|
|
|
|
+ * the {@link SecurityContextHolderStrategy} stored in {@link SecurityContextHolder}.
|
|
|
|
+ *
|
|
|
|
+ * @since 5.8
|
|
|
|
+ */
|
|
|
|
+ public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
|
|
|
|
+ Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
|
|
|
|
+ this.securityContextHolderStrategy = securityContextHolderStrategy;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|