|
@@ -33,6 +33,8 @@ import org.springframework.security.core.Authentication;
|
|
|
import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.core.CredentialsContainer;
|
|
|
import org.springframework.security.core.SpringSecurityMessageSource;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
|
|
import org.springframework.util.Assert;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
@@ -92,6 +94,9 @@ public class ProviderManager implements AuthenticationManager, MessageSourceAwar
|
|
|
|
|
|
private static final Log logger = LogFactory.getLog(ProviderManager.class);
|
|
|
|
|
|
+ private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
|
|
+ .getContextHolderStrategy();
|
|
|
+
|
|
|
private AuthenticationEventPublisher eventPublisher = new NullEventPublisher();
|
|
|
|
|
|
private List<AuthenticationProvider> providers = Collections.emptyList();
|
|
@@ -209,6 +214,7 @@ public class ProviderManager implements AuthenticationManager, MessageSourceAwar
|
|
|
lastException = ex;
|
|
|
}
|
|
|
}
|
|
|
+ result = applyPreviousAuthentication(result);
|
|
|
if (result == null && this.parent != null) {
|
|
|
// Allow the parent to try.
|
|
|
try {
|
|
@@ -265,6 +271,20 @@ public class ProviderManager implements AuthenticationManager, MessageSourceAwar
|
|
|
throw lastException;
|
|
|
}
|
|
|
|
|
|
+ private @Nullable Authentication applyPreviousAuthentication(@Nullable Authentication result) {
|
|
|
+ if (result == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Authentication current = this.securityContextHolderStrategy.getContext().getAuthentication();
|
|
|
+ if (current == null) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ if (!current.isAuthenticated()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ return result.toBuilder().apply(current).build();
|
|
|
+ }
|
|
|
+
|
|
|
@SuppressWarnings("deprecation")
|
|
|
private void prepareException(AuthenticationException ex, Authentication auth) {
|
|
|
ex.setAuthenticationRequest(auth);
|
|
@@ -287,6 +307,11 @@ public class ProviderManager implements AuthenticationManager, MessageSourceAwar
|
|
|
return this.providers;
|
|
|
}
|
|
|
|
|
|
+ public void setSecurityContextHolderStrategy(SecurityContextHolderStrategy securityContextHolderStrategy) {
|
|
|
+ Assert.notNull(securityContextHolderStrategy, "securityContextHolderStrategy cannot be null");
|
|
|
+ this.securityContextHolderStrategy = securityContextHolderStrategy;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void setMessageSource(MessageSource messageSource) {
|
|
|
this.messages = new MessageSourceAccessor(messageSource);
|