20
20
import org .junit .Rule ;
21
21
import org .junit .Test ;
22
22
import org .springframework .beans .factory .annotation .Autowired ;
23
+ import org .springframework .context .annotation .Bean ;
24
+ import org .springframework .context .annotation .Import ;
23
25
import org .springframework .http .HttpHeaders ;
24
- import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
25
26
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
26
- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
27
- import org .springframework .security .config .annotation .web .configurers .oauth2 .server .authorization .OAuth2AuthorizationServerConfigurer ;
27
+ import org .springframework .security .config .annotation .web .configuration .oauth2 .server .authorization .OAuth2AuthorizationServerConfiguration ;
28
28
import org .springframework .security .config .test .SpringTestRule ;
29
29
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
30
30
import org .springframework .security .oauth2 .core .endpoint .OAuth2AuthorizationResponseType ;
56
56
import static org .mockito .Mockito .verify ;
57
57
import static org .mockito .Mockito .verifyNoInteractions ;
58
58
import static org .mockito .Mockito .when ;
59
- import static org .springframework .security .config .Customizer .withDefaults ;
60
59
import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
61
60
import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .user ;
62
61
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
@@ -93,7 +92,7 @@ public void setup() {
93
92
94
93
@ Test
95
94
public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin () throws Exception {
96
- this .spring .register (OAuth2AuthorizationServerConfiguration .class ).autowire ();
95
+ this .spring .register (AuthorizationServerConfiguration .class ).autowire ();
97
96
98
97
RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
99
98
when (registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
@@ -111,7 +110,7 @@ public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin()
111
110
112
111
@ Test
113
112
public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient () throws Exception {
114
- this .spring .register (OAuth2AuthorizationServerConfiguration .class ).autowire ();
113
+ this .spring .register (AuthorizationServerConfiguration .class ).autowire ();
115
114
116
115
RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
117
116
when (registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
@@ -130,7 +129,7 @@ public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient() t
130
129
131
130
@ Test
132
131
public void requestWhenTokenRequestValidThenResponseIncludesCacheHeaders () throws Exception {
133
- this .spring .register (OAuth2AuthorizationServerConfiguration .class ).autowire ();
132
+ this .spring .register (AuthorizationServerConfiguration .class ).autowire ();
134
133
135
134
RegisteredClient registeredClient = TestRegisteredClients .registeredClient ().build ();
136
135
when (registeredClientRepository .findByClientId (eq (registeredClient .getClientId ())))
@@ -187,29 +186,17 @@ private static String encodeBasicAuth(String clientId, String secret) throws Exc
187
186
}
188
187
189
188
@ EnableWebSecurity
190
- static class OAuth2AuthorizationServerConfiguration extends WebSecurityConfigurerAdapter {
191
- private OAuth2AuthorizationServerConfigurer <HttpSecurity > authorizationServerConfigurer
192
- = new OAuth2AuthorizationServerConfigurer <>();
193
-
194
- // @formatter:off
195
- @ Override
196
- protected void configure (HttpSecurity http ) throws Exception {
197
- http
198
- .authorizeRequests (authorizeRequests ->
199
- authorizeRequests
200
- .anyRequest ().authenticated ()
201
- )
202
- .formLogin (withDefaults ())
203
- .apply (this .authorizationServerConfigurer );
204
-
205
- configure (this .authorizationServerConfigurer );
189
+ @ Import (OAuth2AuthorizationServerConfiguration .class )
190
+ static class AuthorizationServerConfiguration {
191
+
192
+ @ Bean
193
+ RegisteredClientRepository registeredClientRepository () {
194
+ return registeredClientRepository ;
206
195
}
207
- // @formatter:on
208
196
209
- private void configure (OAuth2AuthorizationServerConfigurer <HttpSecurity > authorizationServerConfigurer ) {
210
- authorizationServerConfigurer
211
- .registeredClientRepository (registeredClientRepository )
212
- .authorizationService (authorizationService );
197
+ @ Bean
198
+ OAuth2AuthorizationService authorizationService () {
199
+ return authorizationService ;
213
200
}
214
201
}
215
202
}
0 commit comments