J'ai permis oauth2login comme suit.
@Bean
public SecurityWebFilterChain securityWebFilterChainCatchAll(ServerHttpSecurity http) {
return http
.csrf().disable()
.authorizeExchange()
.pathMatchers("/", "/static/**", "/favicon.ico")
.permitAll()
.anyExchange()
.denyAll()
.and()
.oauth2Login()
.and()
.build();
}
Ensuite, je l'api obtenu comme ceci:
@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();
}
Puis dans mon application.yml
i ai installé un fournisseur auth personnalisé comme:
spring:
security:
oauth2:
client:
registration:
cognito:
clientId: ididid
scope: openid,email,phone,profile
clientName: MYClient
provider:
cognito:
issuerUri: SOMEURI
usernameAttribute: username
Maintenant, quand je démarre mon tout l'application fonctionne comme prévu. Les problèmes commencent quand je veux écrire mon test pour l'application.
Mon test sont annotés avec:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
avec un autowired WebTestClient
et exécuté avec:
webTestClient.get()
.uri("/api/something")
.exchange()
.expectStatus()
.isOk()
.expectHeader()
.contentType(ContentType.APPLICATION_JSON.getMimeType())
.expectBodyList(Map.class)
.hasSize(0);
Lorsque je tente de lancer le test tous échouent car le contexte d'application ne peut pas être créé avec le message d'erreur suivant.
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
Je trouve cet exemple qui a de très beaux exemples mais je ne peux pas le faire fonctionner. Comment je veux que mon essai au travail , je documenté pour mvc de printemps dans le suivant après sous le titre
contourner l'authentification entièrement à l'aide MockMvc
Je veux que mon test ne fait jamais appel au fournisseur de oauth2. Je veux seulement créer un oauth2user avec webTestClient.mutateWith(mockOAuth2Login().oauth2User(new CustomOidcUser())
qui est utilisé pour appeler mes contrôleurs.
Comment puis - je utiliser @SpringBootTest
avec mockOAuth2Login().oauth2User
sans appeler le fournisseur de oauth2 réelle et non obtenir une exception?
Cela peut être une instance de https://github.com/spring-projects/spring-boot/issues/19823 qui est abordée dans le prochain version 2.3 de Spring Boot - vous pourriez être en mesure de voir votre problème résolu en essayant le dernier printemps étape de démarrage.
En attendant, vous pouvez fournir un @MockBean
pour ReactiveClientRegistrationRepository
vous - même:
@MockBean
ReactiveClientRegistrationRepository clientRegistrationRepository;
Si cela ne résout pas votre problème, s'il vous plaît envisager d'afficher un échantillon minimal à GitHub.