|
16 | 16 |
|
17 | 17 | package org.springframework.security.web.authentication.preauth; |
18 | 18 |
|
| 19 | +import java.util.Collection; |
| 20 | +import java.util.LinkedHashSet; |
| 21 | +import java.util.function.Supplier; |
| 22 | + |
19 | 23 | import org.apache.commons.logging.Log; |
20 | 24 | import org.apache.commons.logging.LogFactory; |
21 | 25 | import org.jspecify.annotations.Nullable; |
|
28 | 32 | import org.springframework.security.authentication.BadCredentialsException; |
29 | 33 | import org.springframework.security.core.Authentication; |
30 | 34 | import org.springframework.security.core.AuthenticationException; |
| 35 | +import org.springframework.security.core.GrantedAuthority; |
31 | 36 | import org.springframework.security.core.userdetails.AuthenticationUserDetailsService; |
32 | 37 | import org.springframework.security.core.userdetails.UserDetails; |
33 | 38 | import org.springframework.security.core.userdetails.UserDetailsChecker; |
@@ -57,6 +62,8 @@ public class PreAuthenticatedAuthenticationProvider implements AuthenticationPro |
57 | 62 |
|
58 | 63 | private UserDetailsChecker userDetailsChecker = new AccountStatusUserDetailsChecker(); |
59 | 64 |
|
| 65 | + private Supplier<Collection<GrantedAuthority>> grantedAuthoritySupplier; |
| 66 | + |
60 | 67 | private boolean throwExceptionWhenTokenRejected; |
61 | 68 |
|
62 | 69 | private int order = -1; // default: same as non-ordered |
@@ -98,8 +105,10 @@ public void afterPropertiesSet() { |
98 | 105 | UserDetails userDetails = this.preAuthenticatedUserDetailsService |
99 | 106 | .loadUserDetails((PreAuthenticatedAuthenticationToken) authentication); |
100 | 107 | this.userDetailsChecker.check(userDetails); |
| 108 | + Collection<GrantedAuthority> authorities = new LinkedHashSet<>(userDetails.getAuthorities()); |
| 109 | + authorities.addAll(this.grantedAuthoritySupplier.get()); |
101 | 110 | PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(userDetails, |
102 | | - authentication.getCredentials(), userDetails.getAuthorities()); |
| 111 | + authentication.getCredentials(), authorities); |
103 | 112 | result.setDetails(authentication.getDetails()); |
104 | 113 | return result; |
105 | 114 | } |
@@ -142,6 +151,14 @@ public void setUserDetailsChecker(UserDetailsChecker userDetailsChecker) { |
142 | 151 | this.userDetailsChecker = userDetailsChecker; |
143 | 152 | } |
144 | 153 |
|
| 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 | + |
145 | 162 | @Override |
146 | 163 | public int getOrder() { |
147 | 164 | return this.order; |
|
0 commit comments