Skip to content

Commit 298a156

Browse files
committed
Move FACTOR_X509 to PreAuthenticationAuthenticationProvider
1 parent 3a97861 commit 298a156

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/X509Configurer.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security.config.annotation.web.configurers;
1818

19+
import java.util.List;
20+
1921
import jakarta.servlet.http.HttpServletRequest;
2022
import org.jspecify.annotations.Nullable;
2123

@@ -28,6 +30,7 @@
2830
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2931
import org.springframework.security.core.Authentication;
3032
import org.springframework.security.core.AuthenticationException;
33+
import org.springframework.security.core.authority.AuthorityUtils;
3134
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3235
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
3336
import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
@@ -182,7 +185,8 @@ public X509Configurer<H> subjectPrincipalRegex(String subjectPrincipalRegex) {
182185
public void init(H http) {
183186
PreAuthenticatedAuthenticationProvider authenticationProvider = new PreAuthenticatedAuthenticationProvider();
184187
authenticationProvider.setPreAuthenticatedUserDetailsService(getAuthenticationUserDetailsService(http));
185-
http.authenticationProvider(new AuthorityGrantingAuthenticationProvider(authenticationProvider))
188+
authenticationProvider.setGrantedAuthoritySupplier(() -> AuthorityUtils.createAuthorityList("FACTOR_X509"));
189+
http.authenticationProvider(authenticationProvider)
186190
.setSharedObject(AuthenticationEntryPoint.class, new Http403ForbiddenEntryPoint());
187191
ExceptionHandlingConfigurer<H> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
188192
if (exceptions != null) {
@@ -235,28 +239,4 @@ private <C> C getSharedOrBean(H http, Class<C> type) {
235239
return context.getBeanProvider(type).getIfUnique();
236240
}
237241

238-
private static final class AuthorityGrantingAuthenticationProvider implements AuthenticationProvider {
239-
240-
private final AuthenticationProvider delegate;
241-
242-
private AuthorityGrantingAuthenticationProvider(AuthenticationProvider delegate) {
243-
this.delegate = delegate;
244-
}
245-
246-
@Override
247-
public @Nullable Authentication authenticate(Authentication authentication) throws AuthenticationException {
248-
Authentication result = this.delegate.authenticate(authentication);
249-
if (result == null) {
250-
return result;
251-
}
252-
return result.toBuilder().authorities((a) -> a.add(new SimpleGrantedAuthority("FACTOR_X509"))).build();
253-
}
254-
255-
@Override
256-
public boolean supports(Class<?> authentication) {
257-
return true;
258-
}
259-
260-
}
261-
262242
}

web/src/main/java/org/springframework/security/web/authentication/preauth/PreAuthenticatedAuthenticationProvider.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
package org.springframework.security.web.authentication.preauth;
1818

19+
import java.util.Collection;
20+
import java.util.LinkedHashSet;
21+
import java.util.function.Supplier;
22+
1923
import org.apache.commons.logging.Log;
2024
import org.apache.commons.logging.LogFactory;
2125
import org.jspecify.annotations.Nullable;
@@ -28,6 +32,7 @@
2832
import org.springframework.security.authentication.BadCredentialsException;
2933
import org.springframework.security.core.Authentication;
3034
import org.springframework.security.core.AuthenticationException;
35+
import org.springframework.security.core.GrantedAuthority;
3136
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
3237
import org.springframework.security.core.userdetails.UserDetails;
3338
import org.springframework.security.core.userdetails.UserDetailsChecker;
@@ -57,6 +62,8 @@ public class PreAuthenticatedAuthenticationProvider implements AuthenticationPro
5762

5863
private UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker();
5964

65+
private Supplier<Collection<GrantedAuthority>> grantedAuthoritySupplier;
66+
6067
private boolean throwExceptionWhenTokenRejected;
6168

6269
private int order = -1; // default: same as non-ordered
@@ -98,8 +105,10 @@ public void afterPropertiesSet() {
98105
UserDetails userDetails = this.preAuthenticatedUserDetailsService
99106
.loadUserDetails((PreAuthenticatedAuthenticationToken) authentication);
100107
this.userDetailsChecker.check(userDetails);
108+
Collection<GrantedAuthority> authorities = new LinkedHashSet<>(userDetails.getAuthorities());
109+
authorities.addAll(this.grantedAuthoritySupplier.get());
101110
PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(userDetails,
102-
authentication.getCredentials(), userDetails.getAuthorities());
111+
authentication.getCredentials(), authorities);
103112
result.setDetails(authentication.getDetails());
104113
return result;
105114
}
@@ -142,6 +151,14 @@ public void setUserDetailsChecker(UserDetailsChecker userDetailsChecker) {
142151
this.userDetailsChecker = userDetailsChecker;
143152
}
144153

154+
/**
155+
* Sets authorities that this provider should grant once authentication completes
156+
* @param grantedAuthoritySupplier the supplier that grants authorities
157+
*/
158+
public void setGrantedAuthoritySupplier(Supplier<Collection<GrantedAuthority>> grantedAuthoritySupplier) {
159+
this.grantedAuthoritySupplier = grantedAuthoritySupplier;
160+
}
161+
145162
@Override
146163
public int getOrder() {
147164
return this.order;

0 commit comments

Comments
 (0)