20
20
import java .time .Clock ;
21
21
import java .time .Duration ;
22
22
import java .time .Instant ;
23
- import java .util .ArrayList ;
24
23
import java .util .Collection ;
25
24
import java .util .HashSet ;
26
25
import java .util .List ;
44
43
import org .springframework .security .core .AuthenticationException ;
45
44
import org .springframework .security .core .AuthenticationResult ;
46
45
import org .springframework .security .core .GrantedAuthority ;
46
+ import org .springframework .security .core .authority .AuthorityUtils ;
47
47
import org .springframework .security .core .authority .ExpirableGrantedAuthority ;
48
48
import org .springframework .security .core .authority .SimpleGrantedAuthority ;
49
49
import org .springframework .security .core .context .SecurityContextHolder ;
@@ -118,11 +118,9 @@ public void configure(B builder) throws Exception {
118
118
119
119
interface AuthoritiesGranter {
120
120
121
- AuthenticationResult grantAuthorities ( AuthenticationResult authentication );
121
+ Collection < GrantedAuthority > grantableAuthorities ( AuthorizationRequest request );
122
122
123
- default Collection <String > grantableAuthorities () {
124
- return List .of ();
125
- }
123
+ Collection <GrantedAuthority > grantableAuthorities (Authentication result );
126
124
127
125
}
128
126
@@ -135,12 +133,17 @@ static final class PreAuthenticatedAuthoritiesGranter implements AuthoritiesGran
135
133
}
136
134
137
135
@ Override
138
- public AuthenticationResult grantAuthorities (AuthenticationResult authentication ) {
136
+ public Collection <GrantedAuthority > grantableAuthorities (AuthorizationRequest request ) {
137
+ return List .of ();
138
+ }
139
+
140
+ @ Override
141
+ public Collection <GrantedAuthority > grantableAuthorities (Authentication result ) {
139
142
Authentication current = this .strategy .getContext ().getAuthentication ();
140
143
if (current == null || !current .isAuthenticated ()) {
141
- return authentication ;
144
+ return List . of () ;
142
145
}
143
- return authentication . withGrantedAuthorities (( a ) -> a . addAll (current .getAuthorities () ));
146
+ return new HashSet <> (current .getAuthorities ());
144
147
}
145
148
146
149
}
@@ -153,26 +156,22 @@ static final class CompositeAuthoritiesGranter implements AuthoritiesGranter {
153
156
this .authoritiesGranters = List .of (authorities );
154
157
}
155
158
156
- CompositeAuthoritiesGranter (Collection <AuthoritiesGranter > authorities ) {
157
- this .authoritiesGranters = new ArrayList <>(authorities );
158
- }
159
-
160
159
@ Override
161
- public Collection <String > grantableAuthorities () {
162
- Collection <String > grantable = new ArrayList <>();
160
+ public Collection <GrantedAuthority > grantableAuthorities (AuthorizationRequest request ) {
161
+ Collection <GrantedAuthority > authorities = new HashSet <>();
163
162
for (AuthoritiesGranter granter : this .authoritiesGranters ) {
164
- grantable .addAll (granter .grantableAuthorities ());
163
+ authorities .addAll (granter .grantableAuthorities (request ));
165
164
}
166
- return grantable ;
165
+ return authorities ;
167
166
}
168
167
169
168
@ Override
170
- public AuthenticationResult grantAuthorities ( AuthenticationResult authentication ) {
171
- AuthenticationResult granted = authentication ;
169
+ public Collection < GrantedAuthority > grantableAuthorities ( Authentication result ) {
170
+ Collection < GrantedAuthority > authorities = new HashSet <>() ;
172
171
for (AuthoritiesGranter granter : this .authoritiesGranters ) {
173
- granted = granter .grantAuthorities ( granted );
172
+ authorities . addAll ( granter .grantableAuthorities ( result ) );
174
173
}
175
- return granted ;
174
+ return authorities ;
176
175
}
177
176
178
177
}
@@ -197,12 +196,15 @@ static final class SimpleAuthoritiesGranter implements AuthoritiesGranter {
197
196
}
198
197
199
198
@ Override
200
- public Collection <String > grantableAuthorities () {
201
- return this .authorities ;
199
+ public Collection <GrantedAuthority > grantableAuthorities (AuthorizationRequest request ) {
200
+ Collection <GrantedAuthority > grantable = AuthorityUtils .createAuthorityList (this .authorities );
201
+ Collection <GrantedAuthority > requested = request .getAuthorities ();
202
+ grantable .retainAll (requested );
203
+ return grantable ;
202
204
}
203
205
204
206
@ Override
205
- public AuthenticationResult grantAuthorities ( AuthenticationResult authentication ) {
207
+ public Collection < GrantedAuthority > grantableAuthorities ( Authentication result ) {
206
208
Collection <GrantedAuthority > toGrant = new HashSet <>();
207
209
for (String authority : this .authorities ) {
208
210
if (this .grantingTime == null ) {
@@ -213,9 +215,8 @@ public AuthenticationResult grantAuthorities(AuthenticationResult authentication
213
215
toGrant .add (new ExpirableGrantedAuthority (authority , expiresAt ));
214
216
}
215
217
}
216
- Collection <GrantedAuthority > current = new HashSet <>(authentication .getAuthorities ());
217
- toGrant .addAll (current );
218
- return authentication .withGrantedAuthorities (toGrant );
218
+ toGrant .removeAll (result .getAuthorities ());
219
+ return toGrant ;
219
220
}
220
221
221
222
void setClock (Clock clock ) {
@@ -240,7 +241,8 @@ static final class AuthoritiesGranterAuthenticationManager implements Authentica
240
241
public Authentication authenticate (Authentication authentication ) throws AuthenticationException {
241
242
Authentication result = this .authenticationManager .authenticate (authentication );
242
243
Assert .isInstanceOf (AuthenticationResult .class , result , "must be of type AuthenticationResult" );
243
- return this .authoritiesGranter .grantAuthorities ((AuthenticationResult ) result );
244
+ Collection <GrantedAuthority > authorities = this .authoritiesGranter .grantableAuthorities (result );
245
+ return ((AuthenticationResult ) result ).withGrantedAuthorities ((a ) -> a .addAll (authorities ));
244
246
}
245
247
246
248
}
@@ -258,14 +260,8 @@ static final class SimpleAuthorizationEntryPoint implements AuthorizationEntryPo
258
260
}
259
261
260
262
@ Override
261
- public boolean authorizes (AuthorizationRequest authorizationRequest ) {
262
- Collection <String > grantable = this .authoritiesGranter .grantableAuthorities ();
263
- for (GrantedAuthority needed : authorizationRequest .getAuthorities ()) {
264
- if (grantable .contains (needed .getAuthority ())) {
265
- return true ;
266
- }
267
- }
268
- return false ;
263
+ public Collection <GrantedAuthority > grantableAuthorities (AuthorizationRequest request ) {
264
+ return this .authoritiesGranter .grantableAuthorities (request );
269
265
}
270
266
271
267
@ Override
0 commit comments