27
27
import org .springframework .context .annotation .Configuration ;
28
28
import org .springframework .core .Ordered ;
29
29
import org .springframework .core .annotation .Order ;
30
+ import org .springframework .jdbc .core .JdbcTemplate ;
31
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
32
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
33
+ import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
30
34
import org .springframework .security .config .Customizer ;
31
35
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
32
36
import org .springframework .security .config .annotation .web .configuration .OAuth2AuthorizationServerConfiguration ;
33
37
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
34
38
import org .springframework .security .oauth2 .core .ClientAuthenticationMethod ;
35
39
import org .springframework .security .oauth2 .core .oidc .OidcScopes ;
36
40
import org .springframework .security .oauth2 .jwt .JwtDecoder ;
37
- import org .springframework .security .oauth2 .server .authorization .client .InMemoryRegisteredClientRepository ;
41
+ import org .springframework .security .oauth2 .server .authorization .JdbcOAuth2AuthorizationConsentService ;
42
+ import org .springframework .security .oauth2 .server .authorization .JdbcOAuth2AuthorizationService ;
43
+ import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationConsentService ;
44
+ import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationService ;
45
+ import org .springframework .security .oauth2 .server .authorization .client .JdbcRegisteredClientRepository ;
38
46
import org .springframework .security .oauth2 .server .authorization .client .RegisteredClient ;
39
47
import org .springframework .security .oauth2 .server .authorization .client .RegisteredClientRepository ;
40
48
import org .springframework .security .oauth2 .server .authorization .config .ProviderSettings ;
@@ -56,7 +64,7 @@ public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity h
56
64
57
65
// @formatter:off
58
66
@ Bean
59
- public RegisteredClientRepository registeredClientRepository () {
67
+ public RegisteredClientRepository registeredClientRepository (JdbcTemplate jdbcTemplate ) {
60
68
RegisteredClient registeredClient = RegisteredClient .withId (UUID .randomUUID ().toString ())
61
69
.clientId ("messaging-client" )
62
70
.clientSecret ("{noop}secret" )
@@ -71,10 +79,27 @@ public RegisteredClientRepository registeredClientRepository() {
71
79
.scope ("message.write" )
72
80
.clientSettings (clientSettings -> clientSettings .requireUserConsent (true ))
73
81
.build ();
74
- return new InMemoryRegisteredClientRepository (registeredClient );
82
+
83
+ // Save registered client in db as if in-memory
84
+ JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository (jdbcTemplate );
85
+ if (registeredClientRepository .findByClientId (registeredClient .getClientId ()) == null ) {
86
+ registeredClientRepository .save (registeredClient );
87
+ }
88
+
89
+ return registeredClientRepository ;
75
90
}
76
91
// @formatter:on
77
92
93
+ @ Bean
94
+ public OAuth2AuthorizationService authorizationService (JdbcTemplate jdbcTemplate , RegisteredClientRepository registeredClientRepository ) {
95
+ return new JdbcOAuth2AuthorizationService (jdbcTemplate , registeredClientRepository );
96
+ }
97
+
98
+ @ Bean
99
+ public OAuth2AuthorizationConsentService authorizationConsentService (JdbcTemplate jdbcTemplate , RegisteredClientRepository registeredClientRepository ) {
100
+ return new JdbcOAuth2AuthorizationConsentService (jdbcTemplate , registeredClientRepository );
101
+ }
102
+
78
103
@ Bean
79
104
public JWKSource <SecurityContext > jwkSource () {
80
105
RSAKey rsaKey = Jwks .generateRsa ();
@@ -92,4 +117,18 @@ public ProviderSettings providerSettings() {
92
117
return new ProviderSettings ().issuer ("http://auth-server:9000" );
93
118
}
94
119
120
+ @ Bean
121
+ public EmbeddedDatabase embeddedDatabase () {
122
+ // @formatter:off
123
+ return new EmbeddedDatabaseBuilder ()
124
+ .generateUniqueName (true )
125
+ .setType (EmbeddedDatabaseType .H2 )
126
+ .setScriptEncoding ("UTF-8" )
127
+ .addScript ("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql" )
128
+ .addScript ("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql" )
129
+ .addScript ("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql" )
130
+ .build ();
131
+ // @formatter:on
132
+ }
133
+
95
134
}
0 commit comments