1
1
package cn .iocoder .springboot .lab68 .authorizationserverdemo .config ;
2
2
3
3
import org .springframework .beans .factory .annotation .Autowired ;
4
+ import org .springframework .context .annotation .Bean ;
4
5
import org .springframework .context .annotation .Configuration ;
5
6
import org .springframework .security .authentication .AuthenticationManager ;
6
7
import org .springframework .security .oauth2 .config .annotation .configurers .ClientDetailsServiceConfigurer ;
7
8
import org .springframework .security .oauth2 .config .annotation .web .configuration .AuthorizationServerConfigurerAdapter ;
8
9
import org .springframework .security .oauth2 .config .annotation .web .configuration .EnableAuthorizationServer ;
9
10
import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerEndpointsConfigurer ;
10
11
import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerSecurityConfigurer ;
12
+ import org .springframework .security .oauth2 .provider .ClientDetailsService ;
13
+ import org .springframework .security .oauth2 .provider .client .JdbcClientDetailsService ;
14
+ import org .springframework .security .oauth2 .provider .token .TokenStore ;
15
+ import org .springframework .security .oauth2 .provider .token .store .JdbcTokenStore ;
16
+
17
+ import javax .sql .DataSource ;
11
18
12
19
/**
13
20
* 授权服务器配置
@@ -22,27 +29,40 @@ public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigur
22
29
@ Autowired
23
30
private AuthenticationManager authenticationManager ;
24
31
32
+ /**
33
+ * 数据源 DataSource
34
+ */
35
+ @ Autowired
36
+ private DataSource dataSource ;
37
+
38
+ @ Bean
39
+ public TokenStore jdbcTokenStore () {
40
+ return new JdbcTokenStore (dataSource );
41
+ }
42
+
25
43
@ Override
26
44
public void configure (AuthorizationServerEndpointsConfigurer endpoints ) throws Exception {
27
- endpoints .authenticationManager (authenticationManager );
45
+ endpoints .authenticationManager (authenticationManager )
46
+ .tokenStore (jdbcTokenStore ());
28
47
}
29
48
30
49
@ Override
31
50
public void configure (AuthorizationServerSecurityConfigurer oauthServer ) throws Exception {
32
- oauthServer .checkTokenAccess ("isAuthenticated()" )
33
- // .tokenKeyAccess("permitAll()")
34
- ;
51
+ oauthServer .checkTokenAccess ("isAuthenticated()" );
52
+ // oauthServer.tokenKeyAccess("isAuthenticated()")
53
+ // .checkTokenAccess("isAuthenticated()");
54
+ // oauthServer.tokenKeyAccess("permitAll()")
55
+ // .checkTokenAccess("permitAll()");
56
+ }
57
+
58
+ @ Bean
59
+ public ClientDetailsService jdbcClientDetailsService () {
60
+ return new JdbcClientDetailsService (dataSource );
35
61
}
36
62
37
63
@ Override
38
64
public void configure (ClientDetailsServiceConfigurer clients ) throws Exception {
39
- clients .inMemory ()
40
- .withClient ("clientapp" ).secret ("112233" ) // Client 账号、密码。
41
- .authorizedGrantTypes ("authorization_code" ) // 授权码模式
42
- .redirectUris ("http://127.0.0.1:9090/login" ) // 配置回调地址,选填。
43
- .scopes ("read_userinfo" , "read_contacts" ) // 可授权的 Scope
44
- // .and().withClient() // 可以继续配置新的 Client
45
- ;
65
+ clients .withClientDetails (jdbcClientDetailsService ());
46
66
}
47
67
48
68
}
0 commit comments