1
1
package club .yuit .oauth .boot .config .auth2 ;
2
2
3
3
import club .yuit .oauth .boot .filter .BootBasicAuthenticationFilter ;
4
- import club .yuit .oauth .boot .support .oauth2 .BootAccessDeniedHandler ;
5
4
import club .yuit .oauth .boot .support .oauth2 .BootClientDetailsService ;
6
- import club .yuit .oauth .boot .support .oauth2 .BootOAuth2AuthExceptionEntryPoint ;
7
- import club .yuit .oauth .boot .support .oauth2 .BootOAuth2WebResponseExceptionTranslator ;
8
5
import org .springframework .beans .factory .annotation .Autowired ;
9
- import org .springframework .beans .factory .annotation .Qualifier ;
10
- import org .springframework .boot .autoconfigure .security .SecurityProperties ;
11
- import org .springframework .context .ApplicationContext ;
12
- import org .springframework .context .annotation .Bean ;
13
6
import org .springframework .context .annotation .Configuration ;
14
7
import org .springframework .http .HttpMethod ;
15
8
import org .springframework .security .authentication .AuthenticationManager ;
16
- import org .springframework .security .config . annotation . web . builders . HttpSecurity ;
9
+ import org .springframework .security .core . userdetails . UserDetailsService ;
17
10
import org .springframework .security .oauth2 .config .annotation .configurers .ClientDetailsServiceConfigurer ;
18
11
import org .springframework .security .oauth2 .config .annotation .web .configuration .AuthorizationServerConfigurerAdapter ;
19
12
import org .springframework .security .oauth2 .config .annotation .web .configuration .EnableAuthorizationServer ;
20
13
import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerEndpointsConfigurer ;
21
14
import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerSecurityConfigurer ;
22
- import org .springframework .security .oauth2 .provider .client .ClientCredentialsTokenEndpointFilter ;
23
15
import org .springframework .security .oauth2 .provider .error .WebResponseExceptionTranslator ;
24
16
import org .springframework .security .oauth2 .provider .token .TokenStore ;
25
17
import org .springframework .security .oauth2 .provider .token .store .JwtAccessTokenConverter ;
26
18
import org .springframework .security .oauth2 .provider .token .store .redis .RedisTokenStore ;
27
19
import org .springframework .security .web .AuthenticationEntryPoint ;
28
20
29
- import javax .servlet .FilterChain ;
30
-
31
21
/**
32
22
* @author yuit
33
23
* @date 2018/10/15 14:52
36
26
@ EnableAuthorizationServer
37
27
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
38
28
39
- @ Autowired
29
+
40
30
private AuthenticationManager authenticationManager ;
41
31
42
- @ Autowired
32
+
43
33
private BootClientDetailsService clientDetailsService ;
44
34
45
- @ Autowired
35
+
46
36
private TokenStore tokenStore ;
47
37
48
- @ Autowired ( required = false )
38
+
49
39
private JwtAccessTokenConverter converter ;
50
40
51
- @ Autowired
41
+
52
42
private AuthenticationEntryPoint authenticationEntryPoint ;
53
43
54
44
55
- @ Autowired
45
+
56
46
private WebResponseExceptionTranslator bootWebResponseExceptionTranslator ;
57
47
58
- @ Autowired
48
+
59
49
private BootBasicAuthenticationFilter filter ;
60
50
51
+ private UserDetailsService userDetailsService ;
52
+
53
+ @ Autowired (required = false )
54
+ public OAuth2AuthorizationServerConfig (AuthenticationManager authenticationManager ,
55
+ BootClientDetailsService clientDetailsService ,
56
+ TokenStore tokenStore , JwtAccessTokenConverter converter ,
57
+ AuthenticationEntryPoint authenticationEntryPoint ,
58
+ WebResponseExceptionTranslator bootWebResponseExceptionTranslator ,
59
+ BootBasicAuthenticationFilter filter , UserDetailsService userDetailsService ) {
60
+ this .authenticationManager = authenticationManager ;
61
+ this .clientDetailsService = clientDetailsService ;
62
+ this .tokenStore = tokenStore ;
63
+ this .converter = converter ;
64
+ this .authenticationEntryPoint = authenticationEntryPoint ;
65
+ this .bootWebResponseExceptionTranslator = bootWebResponseExceptionTranslator ;
66
+ this .filter = filter ;
67
+ this .userDetailsService = userDetailsService ;
68
+ }
61
69
62
70
public OAuth2AuthorizationServerConfig () {
63
71
super ();
@@ -70,10 +78,13 @@ public void configure(AuthorizationServerSecurityConfigurer security) throws Exc
70
78
// 允许表单登录
71
79
security .allowFormAuthenticationForClients ();
72
80
81
+ // 加载client的service
73
82
filter .setClientDetailsService (clientDetailsService );
74
83
84
+ // 自定义异常处理端口
75
85
security .authenticationEntryPoint (authenticationEntryPoint );
76
86
87
+ // 认证之前的过滤器
77
88
security .addTokenEndpointAuthenticationFilter (filter );
78
89
79
90
security .tokenKeyAccess ("permitAll()" ).checkTokenAccess ("isAuthenticated()" );
@@ -82,22 +93,29 @@ public void configure(AuthorizationServerSecurityConfigurer security) throws Exc
82
93
83
94
@ Override
84
95
public void configure (ClientDetailsServiceConfigurer clients ) throws Exception {
96
+ // 配置加载客户端的service
85
97
clients .withClientDetails (clientDetailsService );
86
98
}
87
99
88
100
@ Override
89
101
public void configure (AuthorizationServerEndpointsConfigurer endpoints ) throws Exception {
90
102
91
103
endpoints
104
+ // token 存储方式
92
105
.tokenStore (tokenStore )
93
106
.authenticationManager (authenticationManager )
107
+ // 不配置会导致token无法刷新
108
+ .userDetailsService (userDetailsService )
94
109
.allowedTokenEndpointRequestMethods (HttpMethod .POST ,HttpMethod .GET );
95
110
111
+ // 判断当前是否使用jwt
96
112
if (!(tokenStore instanceof RedisTokenStore ) && this .converter !=null ){
97
113
endpoints .accessTokenConverter (converter );
98
114
}
99
115
100
116
117
+
118
+
101
119
// 处理 ExceptionTranslationFilter 抛出的异常
102
120
endpoints .exceptionTranslator (bootWebResponseExceptionTranslator );
103
121
0 commit comments