J'utilise le printemps Oauthclient version 5.2.4.Release en suivant le lien de document de Spring Security https://docs.spring.io/spring-security/site/docs/current/reference/html5/#oauth2Client- Fournisseur de Manager autorisé
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizedClientRepository;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
@AllArgsConstructor
@Configuration
@Slf4j
public class WebClientConfig {
@Bean("AuthProvider")
WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations, ServerOAuth2AuthorizedClientRepository authorizedClients) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(
clientRegistrations,
authorizedClients);
oauth.setDefaultOAuth2AuthorizedClient(true);
oauth.setDefaultClientRegistrationId("AuthProvider");
return WebClient.builder()
.filter(oauth)
.filter(this.logRequest())
.build();
}
private ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
log.info("Request: [{}] {}", clientRequest.method(), clientRequest.url());
log.debug("Payload: {}", clientRequest.body());
return Mono.just(clientRequest);
});
}
application.yaml
security:
oauth2:
client:
provider:
AuthProvider:
token-uri: ${tokenpath<read from environment variable>}
registration:
AuthProvider:
authorization-grant-type: client_credentials
client-id: ${<read from environment variable>}
client-secret: ${<read from environment variable>}
Obtenir l'erreur suivante
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method webClient in com.sample.config.WebClientConfig required a bean of type 'org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository' in your configuration.
Faites-moi savoir si j'ai manqué une configuration comme si je n'avais aucune aide spécifique à partir d'autres questions de Stackoverflow
J'ai utilisé les configurations que @anishb. suggéré comme réponse mais a toujours eu l'erreur:
Parameter 0 of method webClient in com.example.sampleoauth2.WebClientConfig required a bean of type 'org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository' that could not be found.
Donc, je devais utiliser ces dépendances avec printemps 2.3.1.Release:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
Et ceci the WebClientConfig
classe:
@Configuration
public class WebClientConfig {
private static final Logger log = LoggerFactory.getLogger(WebClientConfig.class);
@Bean("cr")
ReactiveClientRegistrationRepository getRegistration(
@Value("${spring.security.oauth2.client.provider.keycloak.token-uri}") String tokenUri,
@Value("${spring.security.oauth2.client.registration.keycloak.client-id}") String clientId,
@Value("${spring.security.oauth2.client.registration.keycloak.client-secret}") String clientSecret
) {
ClientRegistration registration = ClientRegistration
.withRegistrationId("keycloak")
.tokenUri(tokenUri)
.clientId(clientId)
.clientSecret(clientSecret)
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
.build();
return new InMemoryReactiveClientRegistrationRepository(registration);
}
@Bean(name = "keycloak")
WebClient webClient(@Qualifier("cr") ReactiveClientRegistrationRepository clientRegistrations) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
oauth.setDefaultClientRegistrationId("keycloak");
return WebClient.builder()
.filter(oauth)
.filter(logRequest())
.build();
}
private ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
log.info("Request: [{}] {}", clientRequest.method(), clientRequest.url());
log.debug("Payload: {}", clientRequest.body());
return Mono.just(clientRequest);
});
}
}
et tout fonctionne bien, mais c'est question pour moi que la solution de @anishb. n'a pas fonctionné pour moi ?!