1
1
/*
2
- * Copyright 2020 the original author or authors.
2
+ * Copyright 2020-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
22
22
import javax .servlet .http .HttpServletRequest ;
23
23
24
- import org .springframework .beans .factory .annotation .Value ;
25
24
import org .springframework .context .annotation .Bean ;
25
+ import org .springframework .context .annotation .Configuration ;
26
26
import org .springframework .security .authentication .AuthenticationManager ;
27
27
import org .springframework .security .authentication .AuthenticationManagerResolver ;
28
+ import org .springframework .security .authentication .ProviderManager ;
28
29
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
29
- import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
30
- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
31
30
import org .springframework .security .oauth2 .jwt .JwtDecoder ;
32
- import org .springframework .security .oauth2 .jwt .NimbusJwtDecoder ;
33
31
import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationProvider ;
34
32
import org .springframework .security .oauth2 .server .resource .authentication .JwtBearerTokenAuthenticationConverter ;
35
33
import org .springframework .security .oauth2 .server .resource .authentication .OpaqueTokenAuthenticationProvider ;
36
- import org .springframework .security .oauth2 .server .resource .introspection .NimbusOpaqueTokenIntrospector ;
37
34
import org .springframework .security .oauth2 .server .resource .introspection .OpaqueTokenIntrospector ;
35
+ import org .springframework .security .web .SecurityFilterChain ;
38
36
39
37
/**
40
38
* OAuth Resource Security configuration.
41
39
*
42
40
* @author Josh Cummings
43
41
*/
44
- @ EnableWebSecurity
45
- public class OAuth2ResourceServerSecurityConfiguration extends WebSecurityConfigurerAdapter {
42
+ @ Configuration
43
+ public class OAuth2ResourceServerSecurityConfiguration {
46
44
47
- @ Value ("${tenantOne.jwk-set-uri}" )
48
- String jwkSetUri ;
49
-
50
- @ Value ("${tenantTwo.introspection-uri}" )
51
- String introspectionUri ;
52
-
53
- @ Value ("${tenantTwo.introspection-client-id}" )
54
- String introspectionClientId ;
55
-
56
- @ Value ("${tenantTwo.introspection-client-secret}" )
57
- String introspectionClientSecret ;
58
-
59
- @ Override
60
- protected void configure (HttpSecurity http ) throws Exception {
45
+ @ Bean
46
+ SecurityFilterChain apiSecurity (HttpSecurity http ,
47
+ AuthenticationManagerResolver <HttpServletRequest > authenticationManagerResolver ) throws Exception {
61
48
// @formatter:off
62
49
http
63
50
.authorizeRequests ((requests ) -> requests
64
- .mvcMatchers ("/**/message/**" ).hasAuthority ("SCOPE_message:read" )
65
- .anyRequest ().authenticated ()
51
+ .mvcMatchers ("/**/message/**" ).hasAuthority ("SCOPE_message:read" )
52
+ .anyRequest ().authenticated ()
66
53
)
67
54
.oauth2ResourceServer ((resourceServer ) -> resourceServer
68
- .authenticationManagerResolver (multitenantAuthenticationManager () )
55
+ .authenticationManagerResolver (authenticationManagerResolver )
69
56
);
70
57
// @formatter:on
58
+
59
+ return http .build ();
71
60
}
72
61
73
62
@ Bean
74
- AuthenticationManagerResolver <HttpServletRequest > multitenantAuthenticationManager () {
63
+ AuthenticationManagerResolver <HttpServletRequest > multitenantAuthenticationManager (JwtDecoder jwtDecoder ,
64
+ OpaqueTokenIntrospector opaqueTokenIntrospector ) {
75
65
Map <String , AuthenticationManager > authenticationManagers = new HashMap <>();
76
- authenticationManagers .put ("tenantOne" , jwt ());
77
- authenticationManagers .put ("tenantTwo" , opaque ());
66
+ authenticationManagers .put ("tenantOne" , jwt (jwtDecoder ));
67
+ authenticationManagers .put ("tenantTwo" , opaque (opaqueTokenIntrospector ));
78
68
return (request ) -> {
79
69
String [] pathParts = request .getRequestURI ().split ("/" );
80
70
String tenantId = (pathParts .length > 0 ) ? pathParts [1 ] : null ;
@@ -86,17 +76,14 @@ AuthenticationManagerResolver<HttpServletRequest> multitenantAuthenticationManag
86
76
};
87
77
}
88
78
89
- AuthenticationManager jwt () {
90
- JwtDecoder jwtDecoder = NimbusJwtDecoder .withJwkSetUri (this .jwkSetUri ).build ();
79
+ AuthenticationManager jwt (JwtDecoder jwtDecoder ) {
91
80
JwtAuthenticationProvider authenticationProvider = new JwtAuthenticationProvider (jwtDecoder );
92
81
authenticationProvider .setJwtAuthenticationConverter (new JwtBearerTokenAuthenticationConverter ());
93
- return authenticationProvider :: authenticate ;
82
+ return new ProviderManager ( authenticationProvider ) ;
94
83
}
95
84
96
- AuthenticationManager opaque () {
97
- OpaqueTokenIntrospector introspectionClient = new NimbusOpaqueTokenIntrospector (this .introspectionUri ,
98
- this .introspectionClientId , this .introspectionClientSecret );
99
- return new OpaqueTokenAuthenticationProvider (introspectionClient )::authenticate ;
85
+ AuthenticationManager opaque (OpaqueTokenIntrospector introspectionClient ) {
86
+ return new ProviderManager (new OpaqueTokenAuthenticationProvider (introspectionClient ));
100
87
}
101
88
102
89
}
0 commit comments