OAuth2AuthorizationCodeGrantTests uses OAuth2AuthorizationServerConfiguration

Issue gh-89
This commit is contained in:
Joe Grandja 2020-06-23 11:40:20 -04:00
parent 2cb6229300
commit 2a3dfd953d

View File

@ -20,11 +20,11 @@ import org.junit.BeforeClass;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.oauth2.server.authorization.OAuth2AuthorizationServerConfiguration;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.authorization.OAuth2AuthorizationServerConfigurer;
import org.springframework.security.config.test.SpringTestRule; import org.springframework.security.config.test.SpringTestRule;
import org.springframework.security.oauth2.core.AuthorizationGrantType; import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType; import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType;
@ -56,7 +56,6 @@ import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.security.config.Customizer.withDefaults;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@ -93,7 +92,7 @@ public class OAuth2AuthorizationCodeGrantTests {
@Test @Test
public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin() throws Exception { public void requestWhenAuthorizationRequestNotAuthenticatedThenRedirectToLogin() throws Exception {
this.spring.register(OAuth2AuthorizationServerConfiguration.class).autowire(); this.spring.register(AuthorizationServerConfiguration.class).autowire();
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId()))) when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
@ -111,7 +110,7 @@ public class OAuth2AuthorizationCodeGrantTests {
@Test @Test
public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient() throws Exception { public void requestWhenAuthorizationRequestAuthenticatedThenRedirectToClient() throws Exception {
this.spring.register(OAuth2AuthorizationServerConfiguration.class).autowire(); this.spring.register(AuthorizationServerConfiguration.class).autowire();
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId()))) when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
@ -130,7 +129,7 @@ public class OAuth2AuthorizationCodeGrantTests {
@Test @Test
public void requestWhenTokenRequestValidThenResponseIncludesCacheHeaders() throws Exception { public void requestWhenTokenRequestValidThenResponseIncludesCacheHeaders() throws Exception {
this.spring.register(OAuth2AuthorizationServerConfiguration.class).autowire(); this.spring.register(AuthorizationServerConfiguration.class).autowire();
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId()))) when(registeredClientRepository.findByClientId(eq(registeredClient.getClientId())))
@ -187,29 +186,17 @@ public class OAuth2AuthorizationCodeGrantTests {
} }
@EnableWebSecurity @EnableWebSecurity
static class OAuth2AuthorizationServerConfiguration extends WebSecurityConfigurerAdapter { @Import(OAuth2AuthorizationServerConfiguration.class)
private OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer static class AuthorizationServerConfiguration {
= new OAuth2AuthorizationServerConfigurer<>();
// @formatter:off @Bean
@Override RegisteredClientRepository registeredClientRepository() {
protected void configure(HttpSecurity http) throws Exception { return registeredClientRepository;
http }
.authorizeRequests(authorizeRequests ->
authorizeRequests
.anyRequest().authenticated()
)
.formLogin(withDefaults())
.apply(this.authorizationServerConfigurer);
configure(this.authorizationServerConfigurer); @Bean
} OAuth2AuthorizationService authorizationService() {
// @formatter:on return authorizationService;
private void configure(OAuth2AuthorizationServerConfigurer<HttpSecurity> authorizationServerConfigurer) {
authorizationServerConfigurer
.registeredClientRepository(registeredClientRepository)
.authorizationService(authorizationService);
} }
} }
} }