次のように私はoauth2loginを有効にしています。
@Bean
public SecurityWebFilterChain securityWebFilterChainCatchAll(ServerHttpSecurity http) {
return http
.csrf().disable()
.authorizeExchange()
.pathMatchers("/", "/static/**", "/favicon.ico")
.permitAll()
.anyExchange()
.denyAll()
.and()
.oauth2Login()
.and()
.build();
}
それから私はこのように確保APIを持っています。
@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http, final List<HttpSecurityConfig> configs) {
return http
.securityMatcher(ServerWebExchangeMatchers.pathMatchers("/api/**"))
.authorizeExchange()
.pathMatchers(HttpMethod.GET, "/api").permitAll()
.anyExchange().authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(new HttpStatusServerEntryPoint(HttpStatus.UNAUTHORIZED))
.and()
.build();
}
その後、私の中でapplication.yml
私は、セットアップにカスタム認証プロバイダーなどがあります。
spring:
security:
oauth2:
client:
registration:
cognito:
clientId: ididid
scope: openid,email,phone,profile
clientName: MYClient
provider:
cognito:
issuerUri: SOMEURI
usernameAttribute: username
予想通り今私は自分のアプリケーションのすべての作業を起動するとき。私は自分のアプリケーションのためのテストを書きたいときに問題が開始されます。
私のテストをして注釈を付けています。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
autowiredでWebTestClient
として実行:
webTestClient.get()
.uri("/api/something")
.exchange()
.expectStatus()
.isOk()
.expectHeader()
.contentType(ContentType.APPLICATION_JSON.getMimeType())
.expectBodyList(Map.class)
.hasSize(0);
私は、アプリケーションコンテキストは、次のエラーメッセージを使用して作成することができないので、彼らはすべて失敗し、テストを実行しようとします。
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.reactive.WebFluxSecurityConfiguration': Unsatisfied dependency expressed through method 'setSecurityWebFilterChains' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityWebFilterChainCatchAll' defined in class path resource [***]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.server.SecurityWebFilterChain]: Factory method 'securityWebFilterChainCatchAll' threw exception; nested exception is java.lang.IllegalArgumentException: clientRegistrationRepository cannot be null
私が見つかりました。この例は非常にいい例がありますが、それでも私はそれが働いて得ることができません。どのように私は私が春のMVCのための文書化作業に私のテストをしたい、次の見出しの下のポスト
完全MockMvcを使用してバイパス認証
私は私のテストは、実際のOAuth2プロバイダを呼び出すことはありませんでしたいです。私だけでoauth2user作成したいwebTestClient.mutateWith(mockOAuth2Login().oauth2User(new CustomOidcUser())
私のコントローラを呼び出すために使用されていることを。
どのようにして使用することができ@SpringBootTest
てmockOAuth2Login().oauth2User
、実際のOAuth2プロバイダを呼び出していないと、例外を得ていないながら?
これは、インスタンスのかもしれhttps://github.com/spring-projects/spring-boot/issues/19823春ブーツの今後の2.3リリースで対処されて-あなたはあなたの問題は、最新の春を試すことで解決見ることができるかもしれませんブートマイルストーン。
一方で、あなたが提供することができます@MockBean
のためにReactiveClientRegistrationRepository
自分自身:
@MockBean
ReactiveClientRegistrationRepository clientRegistrationRepository;
それはあなたの問題が解決しない場合は、GitHubのに最小限のサンプルを掲載ご検討ください。