Polish OAuth 2.0 Samples

- Favor @TestConfiguration so as to not disable Spring Boot's
auto-configuration of ClientRegistrationRepository and
OAuth2AuthorizedClientRepository
This commit is contained in:
Josh Cummings 2020-05-27 15:57:48 -06:00
parent 8d84bc58f6
commit d5b8981678
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
5 changed files with 13 additions and 21 deletions

View File

@ -41,6 +41,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@ -83,7 +84,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @since 5.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes={ OAuth2LoginApplication.class, OAuth2LoginApplicationTests.SecurityTestConfig.class })
@SpringBootTest
@AutoConfigureMockMvc
public class OAuth2LoginApplicationTests {
private static final String AUTHORIZATION_BASE_URI = "/oauth2/authorization";
@ -326,6 +327,7 @@ public class OAuth2LoginApplicationTests {
}
@EnableWebSecurity
@TestConfiguration
public static class SecurityTestConfig extends WebSecurityConfigurerAdapter {
// @formatter:off

View File

@ -22,14 +22,13 @@ import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import sample.config.SecurityConfig;
import sample.web.OAuth2WebClientController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.test.context.junit4.SpringRunner;
@ -40,7 +39,7 @@ import static org.springframework.security.test.web.reactive.server.SecurityMock
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOAuth2Login;
@WebFluxTest
@Import({ SecurityConfig.class, OAuth2WebClientController.class })
@Import(SecurityConfig.class)
@AutoConfigureWebTestClient
@RunWith(SpringRunner.class)
public class OAuth2WebClientControllerTests {
@ -94,7 +93,7 @@ public class OAuth2WebClientControllerTests {
.expectStatus().isOk();
}
@Configuration
@TestConfiguration
static class WebClientConfig {
@Bean
WebClient web() {

View File

@ -22,14 +22,13 @@ import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import sample.config.SecurityConfig;
import sample.web.RegisteredOAuth2AuthorizedClientController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.test.context.junit4.SpringRunner;
@ -40,7 +39,7 @@ import static org.springframework.security.test.web.reactive.server.SecurityMock
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockOAuth2Login;
@WebFluxTest
@Import({ SecurityConfig.class, RegisteredOAuth2AuthorizedClientController.class })
@Import(SecurityConfig.class)
@AutoConfigureWebTestClient
@RunWith(SpringRunner.class)
public class RegisteredOAuth2AuthorizedClientControllerTests {
@ -94,7 +93,7 @@ public class RegisteredOAuth2AuthorizedClientControllerTests {
.expectStatus().isOk();
}
@Configuration
@TestConfiguration
static class WebClientConfig {
@Bean
WebClient web() {

View File

@ -21,16 +21,13 @@ import okhttp3.mockwebserver.MockWebServer;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import sample.config.SecurityConfig;
import sample.web.OAuth2WebClientController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@ -42,7 +39,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
@Import({ SecurityConfig.class, OAuth2WebClientController.class })
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
public class OAuth2WebClientControllerTests {
@ -92,7 +88,7 @@ public class OAuth2WebClientControllerTests {
.andExpect(status().isOk());
}
@Configuration
@TestConfiguration
static class WebClientConfig {
@Bean
WebClient web() {

View File

@ -21,16 +21,13 @@ import okhttp3.mockwebserver.MockWebServer;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import sample.config.SecurityConfig;
import sample.web.RegisteredOAuth2AuthorizedClientController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@ -42,7 +39,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest
@Import({ SecurityConfig.class, RegisteredOAuth2AuthorizedClientController.class })
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
public class RegisteredOAuth2AuthorizedClientControllerTests {
@ -92,7 +88,7 @@ public class RegisteredOAuth2AuthorizedClientControllerTests {
.andExpect(status().isOk());
}
@Configuration
@TestConfiguration
static class WebClientConfig {
@Bean
WebClient web() {