1
+ package org .springdoc .demo .auth ;
2
+
3
+ import java .util .UUID ;
4
+
5
+ import com .nimbusds .jose .jwk .JWKSet ;
6
+ import com .nimbusds .jose .jwk .RSAKey ;
7
+ import com .nimbusds .jose .jwk .source .JWKSource ;
8
+ import com .nimbusds .jose .proc .SecurityContext ;
9
+
10
+ import org .springframework .context .annotation .Bean ;
11
+ import org .springframework .context .annotation .Configuration ;
12
+ import org .springframework .core .Ordered ;
13
+ import org .springframework .core .annotation .Order ;
14
+ import org .springframework .jdbc .core .JdbcTemplate ;
15
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
16
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
17
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
18
+ import org .springframework .security .config .Customizer ;
19
+ import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
20
+ import org .springframework .security .config .annotation .web .configurers .oauth2 .server .resource .OAuth2ResourceServerConfigurer ;
21
+ import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
22
+ import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
23
+ import org .springframework .security .oauth2 .core .oidc .OidcScopes ;
24
+ import org .springframework .security .oauth2 .jwt .JwtDecoder ;
25
+ import org .springframework .security .oauth2 .server .authorization .JdbcOAuth2AuthorizationConsentService ;
26
+ import org .springframework .security .oauth2 .server .authorization .JdbcOAuth2AuthorizationService ;
27
+ import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationConsentService ;
28
+ import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationService ;
29
+ import org .springframework .security .oauth2 .server .authorization .client .JdbcRegisteredClientRepository ;
30
+ import org .springframework .security .oauth2 .server .authorization .client .RegisteredClient ;
31
+ import org .springframework .security .oauth2 .server .authorization .client .RegisteredClientRepository ;
32
+ import org .springframework .security .oauth2 .server .authorization .config .annotation .web .configuration .OAuth2AuthorizationServerConfiguration ;
33
+ import org .springframework .security .oauth2 .server .authorization .config .annotation .web .configurers .OAuth2AuthorizationServerConfigurer ;
34
+ import org .springframework .security .oauth2 .server .authorization .settings .AuthorizationServerSettings ;
35
+ import org .springframework .security .web .SecurityFilterChain ;
36
+ import org .springframework .security .web .authentication .LoginUrlAuthenticationEntryPoint ;
37
+ import org .springframework .web .cors .CorsConfiguration ;
38
+
39
+
40
+ @ Configuration (proxyBeanMethods = false )
41
+ public class SecurityConfig {
42
+
43
+ @ Bean
44
+ @ Order (Ordered .HIGHEST_PRECEDENCE )
45
+ public SecurityFilterChain authorizationServerSecurityFilterChain (HttpSecurity http ) throws Exception {
46
+ OAuth2AuthorizationServerConfiguration .applyDefaultSecurity (http );
47
+ http .getConfigurer (OAuth2AuthorizationServerConfigurer .class )
48
+ .oidc (Customizer .withDefaults ());
49
+ http .cors ().configurationSource (request -> new CorsConfiguration ().applyPermitDefaultValues ())
50
+ .and ()
51
+ .exceptionHandling (exceptions ->
52
+ exceptions .authenticationEntryPoint (new LoginUrlAuthenticationEntryPoint ("/login" ))
53
+ )
54
+ .oauth2ResourceServer (OAuth2ResourceServerConfigurer ::jwt );
55
+ return http .build ();
56
+ }
57
+
58
+ @ Bean
59
+ public RegisteredClientRepository registeredClientRepository (JdbcTemplate jdbcTemplate ) {
60
+ RegisteredClient registeredClient = RegisteredClient .withId (UUID .randomUUID ().toString ())
61
+ .clientId ("newClient" )
62
+ .clientSecret ("{noop}newClientSecret" )
63
+ .clientAuthenticationMethod (ClientAuthenticationMethod .CLIENT_SECRET_POST )
64
+ .authorizationGrantType (AuthorizationGrantType .AUTHORIZATION_CODE )
65
+ .authorizationGrantType (AuthorizationGrantType .REFRESH_TOKEN )
66
+ .authorizationGrantType (AuthorizationGrantType .CLIENT_CREDENTIALS )
67
+ .redirectUri ("http://127.0.0.1:8081/resource-server/swagger-ui/oauth2-redirect.html" )
68
+ .redirectUri ("http://127.0.0.1:8082/resource-server/webjars/swagger-ui/oauth2-redirect.html" )
69
+ .redirectUri ("http://158.101.191.70:8095/resource-server/swagger-ui/oauth2-redirect.html" )
70
+ .redirectUri ("http://158.101.191.70:8096/resource-server/webjars/swagger-ui/oauth2-redirect.html" )
71
+ .scope (OidcScopes .OPENID )
72
+ .scope (OidcScopes .PROFILE )
73
+ .scope ("springdoc.read" )
74
+ .scope ("springdoc.write" )
75
+ .build ();
76
+
77
+ // Save registered client in db as if in-memory
78
+ JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository (jdbcTemplate );
79
+ registeredClientRepository .save (registeredClient );
80
+
81
+ return registeredClientRepository ;
82
+ }
83
+
84
+ @ Bean
85
+ public OAuth2AuthorizationService authorizationService (JdbcTemplate jdbcTemplate , RegisteredClientRepository registeredClientRepository ) {
86
+ return new JdbcOAuth2AuthorizationService (jdbcTemplate , registeredClientRepository );
87
+ }
88
+
89
+ @ Bean
90
+ public OAuth2AuthorizationConsentService authorizationConsentService (JdbcTemplate jdbcTemplate , RegisteredClientRepository registeredClientRepository ) {
91
+ return new JdbcOAuth2AuthorizationConsentService (jdbcTemplate , registeredClientRepository );
92
+ }
93
+
94
+ @ Bean
95
+ public JWKSource <SecurityContext > jwkSource () {
96
+ RSAKey rsaKey = Jwks .generateRsa ();
97
+ JWKSet jwkSet = new JWKSet (rsaKey );
98
+ return (jwkSelector , securityContext ) -> jwkSelector .select (jwkSet );
99
+ }
100
+
101
+ @ Bean
102
+ public JwtDecoder jwtDecoder (JWKSource <SecurityContext > jwkSource ) {
103
+ return OAuth2AuthorizationServerConfiguration .jwtDecoder (jwkSource );
104
+ }
105
+
106
+ @ Bean
107
+ public AuthorizationServerSettings authorizationServerSettings () {
108
+ return AuthorizationServerSettings .builder ().build ();
109
+ }
110
+
111
+ @ Bean
112
+ public EmbeddedDatabase embeddedDatabase () {
113
+ return new EmbeddedDatabaseBuilder ()
114
+ .generateUniqueName (true )
115
+ .setType (EmbeddedDatabaseType .H2 )
116
+ .setScriptEncoding ("UTF-8" )
117
+ .addScript ("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql" )
118
+ .addScript ("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql" )
119
+ .addScript ("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql" )
120
+ .build ();
121
+ }
122
+
123
+ }
0 commit comments