30
30
import org .springframework .core .convert .converter .Converter ;
31
31
import org .springframework .core .log .LogMessage ;
32
32
import org .springframework .lang .NonNull ;
33
+ import org .springframework .security .authentication .AbstractAuthenticationToken ;
33
34
import org .springframework .security .authentication .AuthenticationManager ;
34
35
import org .springframework .security .authentication .AuthenticationManagerResolver ;
35
36
import org .springframework .security .core .Authentication ;
36
37
import org .springframework .security .core .AuthenticationException ;
37
38
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
39
+ import org .springframework .security .oauth2 .jwt .Jwt ;
38
40
import org .springframework .security .oauth2 .jwt .JwtDecoder ;
39
41
import org .springframework .security .oauth2 .jwt .JwtDecoders ;
40
42
import org .springframework .security .oauth2 .server .resource .InvalidBearerTokenException ;
@@ -93,7 +95,40 @@ public static JwtIssuerAuthenticationManagerResolver fromTrustedIssuers(Collecti
93
95
public static JwtIssuerAuthenticationManagerResolver fromTrustedIssuers (Predicate <String > trustedIssuers ) {
94
96
Assert .notNull (trustedIssuers , "trustedIssuers cannot be null" );
95
97
return new JwtIssuerAuthenticationManagerResolver (
96
- new TrustedIssuerJwtAuthenticationManagerResolver (trustedIssuers ));
98
+ new TrustedIssuerJwtAuthenticationManagerResolver (null , trustedIssuers ));
99
+ }
100
+
101
+ /**
102
+ * Construct a {@link JwtIssuerAuthenticationManagerResolver} using the provided
103
+ * parameters
104
+ * @param trustedIssuers an array of trusted issuers
105
+ * @since 6.2
106
+ */
107
+ public static JwtIssuerAuthenticationManagerResolver fromTrustedIssuers (Converter <Jwt , ? extends AbstractAuthenticationToken > jwtAuthenticationConverter , String ... trustedIssuers ) {
108
+ return fromTrustedIssuers (jwtAuthenticationConverter , Set .of (trustedIssuers ));
109
+ }
110
+
111
+ /**
112
+ * Construct a {@link JwtIssuerAuthenticationManagerResolver} using the provided
113
+ * parameters
114
+ * @param trustedIssuers a collection of trusted issuers
115
+ * @since 6.2
116
+ */
117
+ public static JwtIssuerAuthenticationManagerResolver fromTrustedIssuers (Converter <Jwt , ? extends AbstractAuthenticationToken > jwtAuthenticationConverter , Collection <String > trustedIssuers ) {
118
+ Assert .notEmpty (trustedIssuers , "trustedIssuers cannot be empty" );
119
+ return fromTrustedIssuers (jwtAuthenticationConverter , Set .copyOf (trustedIssuers )::contains );
120
+ }
121
+
122
+ /**
123
+ * Construct a {@link JwtIssuerAuthenticationManagerResolver} using the provided
124
+ * parameters
125
+ * @param trustedIssuers a predicate to validate issuers
126
+ * @since 6.2
127
+ */
128
+ public static JwtIssuerAuthenticationManagerResolver fromTrustedIssuers (Converter <Jwt , ? extends AbstractAuthenticationToken > jwtAuthenticationConverter , Predicate <String > trustedIssuers ) {
129
+ Assert .notNull (trustedIssuers , "trustedIssuers cannot be null" );
130
+ return new JwtIssuerAuthenticationManagerResolver (
131
+ new TrustedIssuerJwtAuthenticationManagerResolver (jwtAuthenticationConverter , trustedIssuers ));
97
132
}
98
133
99
134
/**
@@ -117,6 +152,7 @@ public static JwtIssuerAuthenticationManagerResolver fromTrustedIssuers(Predicat
117
152
* {@link AuthenticationManager} by the issuer
118
153
*/
119
154
public JwtIssuerAuthenticationManagerResolver (
155
+
120
156
AuthenticationManagerResolver <String > issuerAuthenticationManagerResolver ) {
121
157
Assert .notNull (issuerAuthenticationManagerResolver , "issuerAuthenticationManagerResolver cannot be null" );
122
158
this .authenticationManager = new ResolvingAuthenticationManager (issuerAuthenticationManagerResolver );
@@ -197,7 +233,14 @@ static class TrustedIssuerJwtAuthenticationManagerResolver implements Authentica
197
233
198
234
private final Predicate <String > trustedIssuer ;
199
235
236
+ private final Converter <Jwt , ? extends AbstractAuthenticationToken > jwtAuthenticationConverter ;
237
+
200
238
TrustedIssuerJwtAuthenticationManagerResolver (Predicate <String > trustedIssuer ) {
239
+ this (null , trustedIssuer );
240
+ }
241
+
242
+ TrustedIssuerJwtAuthenticationManagerResolver (Converter <Jwt , ? extends AbstractAuthenticationToken > jwtAuthenticationConverter , Predicate <String > trustedIssuer ) {
243
+ this .jwtAuthenticationConverter = jwtAuthenticationConverter ;
201
244
this .trustedIssuer = trustedIssuer ;
202
245
}
203
246
@@ -208,7 +251,11 @@ public AuthenticationManager resolve(String issuer) {
208
251
(k ) -> {
209
252
this .logger .debug ("Constructing AuthenticationManager" );
210
253
JwtDecoder jwtDecoder = JwtDecoders .fromIssuerLocation (issuer );
211
- return new JwtAuthenticationProvider (jwtDecoder )::authenticate ;
254
+ JwtAuthenticationProvider provider = new JwtAuthenticationProvider (jwtDecoder );
255
+ if (jwtAuthenticationConverter != null ) {
256
+ provider .setJwtAuthenticationConverter (jwtAuthenticationConverter );
257
+ }
258
+ return provider ::authenticate ;
212
259
});
213
260
this .logger .debug (LogMessage .format ("Resolved AuthenticationManager for issuer '%s'" , issuer ));
214
261
return authenticationManager ;
0 commit comments