|
17 | 17 | package org.springframework.security.config.annotation.web.configurers;
|
18 | 18 |
|
19 | 19 | import jakarta.servlet.http.HttpServletRequest;
|
20 |
| -import org.jspecify.annotations.Nullable; |
21 | 20 |
|
22 | 21 | import org.springframework.context.ApplicationContext;
|
23 | 22 | import org.springframework.security.authentication.AuthenticationDetailsSource;
|
24 | 23 | import org.springframework.security.authentication.AuthenticationManager;
|
25 |
| -import org.springframework.security.authentication.AuthenticationProvider; |
26 | 24 | import org.springframework.security.config.Customizer;
|
27 | 25 | import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
|
28 | 26 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
29 | 27 | import org.springframework.security.core.Authentication;
|
30 |
| -import org.springframework.security.core.AuthenticationException; |
31 |
| -import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 28 | +import org.springframework.security.core.authority.AuthorityUtils; |
32 | 29 | import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
|
33 | 30 | import org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper;
|
34 | 31 | import org.springframework.security.core.userdetails.UserDetailsService;
|
@@ -182,7 +179,8 @@ public X509Configurer<H> subjectPrincipalRegex(String subjectPrincipalRegex) {
|
182 | 179 | public void init(H http) {
|
183 | 180 | PreAuthenticatedAuthenticationProvider authenticationProvider = new PreAuthenticatedAuthenticationProvider();
|
184 | 181 | authenticationProvider.setPreAuthenticatedUserDetailsService(getAuthenticationUserDetailsService(http));
|
185 |
| - http.authenticationProvider(new AuthorityGrantingAuthenticationProvider(authenticationProvider)) |
| 182 | + authenticationProvider.setGrantedAuthoritySupplier(() -> AuthorityUtils.createAuthorityList("FACTOR_X509")); |
| 183 | + http.authenticationProvider(authenticationProvider) |
186 | 184 | .setSharedObject(AuthenticationEntryPoint.class, new Http403ForbiddenEntryPoint());
|
187 | 185 | ExceptionHandlingConfigurer<H> exceptions = http.getConfigurer(ExceptionHandlingConfigurer.class);
|
188 | 186 | if (exceptions != null) {
|
@@ -234,31 +232,4 @@ private <C> C getSharedOrBean(H http, Class<C> type) {
|
234 | 232 | return context.getBeanProvider(type).getIfUnique();
|
235 | 233 | }
|
236 | 234 |
|
237 |
| - private static final class AuthorityGrantingAuthenticationProvider implements AuthenticationProvider { |
238 |
| - |
239 |
| - private final AuthenticationProvider delegate; |
240 |
| - |
241 |
| - private AuthorityGrantingAuthenticationProvider(AuthenticationProvider delegate) { |
242 |
| - this.delegate = delegate; |
243 |
| - } |
244 |
| - |
245 |
| - @Override |
246 |
| - public @Nullable Authentication authenticate(Authentication authentication) throws AuthenticationException { |
247 |
| - Authentication result = this.delegate.authenticate(authentication); |
248 |
| - if (result == null) { |
249 |
| - return result; |
250 |
| - } |
251 |
| - return result |
252 |
| - .toBuilder() |
253 |
| - .authorities((a) -> a.add(new SimpleGrantedAuthority("FACTOR_X509"))) |
254 |
| - .build(); |
255 |
| - } |
256 |
| - |
257 |
| - @Override |
258 |
| - public boolean supports(Class<?> authentication) { |
259 |
| - return true; |
260 |
| - } |
261 |
| - |
262 |
| - } |
263 |
| - |
264 | 235 | }
|
0 commit comments