Java 11492 (#12375)
* Added/Created parent module * moved spring-5-data-reactive (submodule) to spring-reactive-modules (parent) * moved spring-5-reactive-2 (submodule) to spring-reactive-modules (parent) * moved spring-5-reactive-3 (submodule) to spring-reactive-modules (parent) * moved spring-5-reactive-client (submodule) to spring-reactive-modules (parent) * moved spring-5-reactive-oauth (submodule) to spring-reactive-modules (parent) * moved spring-5-reactive-security (submodule) to spring-reactive-modules (parent) * moved spring-5-reactive (submodule) to spring-reactive-modules (parent) * moved spring-reactive (submodule) to spring-reactive-modules (parent) * deleted modules that were moved across * renamed spring-reactive-modules to spring-5-reactive-modules Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.reactive.oauth;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
@EnableWebFluxSecurity
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
|
||||
return http.authorizeExchange()
|
||||
.pathMatchers("/about").permitAll()
|
||||
.anyExchange().authenticated()
|
||||
.and().oauth2Login()
|
||||
.and().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.reactive.oauth;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
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.WebClient;
|
||||
|
||||
@PropertySource("classpath:default-application.yml")
|
||||
@SpringBootApplication
|
||||
public class Spring5ReactiveOauthApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Spring5ReactiveOauthApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebClient webClient(ReactiveClientRegistrationRepository clientRegistrationRepo, ServerOAuth2AuthorizedClientRepository authorizedClientRepo) {
|
||||
ServerOAuth2AuthorizedClientExchangeFilterFunction filter = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrationRepo, authorizedClientRepo);
|
||||
return WebClient.builder()
|
||||
.filter(filter)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.reactive.oauth.web;
|
||||
|
||||
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
||||
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
|
||||
import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import com.baeldung.reactive.oauth.web.dto.Foo;
|
||||
|
||||
@RestController
|
||||
public class MainController {
|
||||
|
||||
@Autowired
|
||||
private WebClient webClient;
|
||||
|
||||
@GetMapping("/")
|
||||
public Mono<String> index(@AuthenticationPrincipal Mono<OAuth2User> oauth2User) {
|
||||
return oauth2User
|
||||
.map(OAuth2User::getName)
|
||||
.map(name -> String.format("Hi, %s", name));
|
||||
}
|
||||
|
||||
@GetMapping("/foos/{id}")
|
||||
public Mono<Foo> getFooResource(@RegisteredOAuth2AuthorizedClient("custom") OAuth2AuthorizedClient client, @PathVariable final long id){
|
||||
return webClient
|
||||
.get()
|
||||
.uri("http://localhost:8088/spring-security-oauth-resource/foos/{id}", id)
|
||||
.attributes(oauth2AuthorizedClient(client))
|
||||
.retrieve()
|
||||
.bodyToMono(Foo.class);
|
||||
}
|
||||
|
||||
@GetMapping("/about")
|
||||
public String getAboutPage() {
|
||||
return "WebFlux OAuth example";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.reactive.oauth.web.dto;
|
||||
|
||||
public class Foo {
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
public Foo() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Foo(final long id, final String name) {
|
||||
super();
|
||||
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.webclient.authorizationcodeclient;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
*
|
||||
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
|
||||
*
|
||||
* As we usually do with other well-known auth providers (github/facebook/...) we have to log-in using user credentials (john/123) and client configurations handled by the auth server
|
||||
*
|
||||
* @author rozagerardo
|
||||
*
|
||||
*/
|
||||
@PropertySource("classpath:webclient-auth-code-client-application.properties")
|
||||
@SpringBootApplication
|
||||
public class OauthClientApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OauthClientApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.webclient.authorizationcodeclient.configuration;
|
||||
|
||||
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.WebClient;
|
||||
|
||||
@Configuration
|
||||
public class WebClientConfig {
|
||||
|
||||
@Bean
|
||||
WebClient webClientForAuthorized(ReactiveClientRegistrationRepository clientRegistrations, ServerOAuth2AuthorizedClientRepository authorizedClients) {
|
||||
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
|
||||
return WebClient.builder()
|
||||
.filter(oauth)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.webclient.authorizationcodeclient.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class WebSecurityConfig {
|
||||
@Bean
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||
http.authorizeExchange()
|
||||
.anyExchange()
|
||||
.authenticated()
|
||||
.and()
|
||||
.oauth2Client()
|
||||
.and()
|
||||
.formLogin();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.webclient.authorizationcodeclient.web;
|
||||
|
||||
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId;
|
||||
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
||||
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
public class ClientRestController {
|
||||
|
||||
private static final String RESOURCE_URI = "http://localhost:8082/spring-security-oauth-resource/foos/1";
|
||||
|
||||
@Autowired
|
||||
WebClient webClient;
|
||||
|
||||
@GetMapping("/auth-code")
|
||||
Mono<String> useOauthWithAuthCode() {
|
||||
Mono<String> retrievedResource = webClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string);
|
||||
}
|
||||
|
||||
@GetMapping("/auth-code-annotated")
|
||||
Mono<String> useOauthWithAuthCodeAndAnnotation(@RegisteredOAuth2AuthorizedClient("bael") OAuth2AuthorizedClient authorizedClient) {
|
||||
Mono<String> retrievedResource = webClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.attributes(oauth2AuthorizedClient(authorizedClient))
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string + ". Principal associated: " + authorizedClient.getPrincipalName() + ". Token will expire at: " + authorizedClient.getAccessToken()
|
||||
.getExpiresAt());
|
||||
}
|
||||
|
||||
@GetMapping("/auth-code-explicit-client")
|
||||
Mono<String> useOauthWithExpicitClient() {
|
||||
Mono<String> retrievedResource = webClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.attributes(clientRegistrationId("bael"))
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.webclient.authorizationcodelogin;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
*
|
||||
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
|
||||
*
|
||||
* As we usually do with other well-known auth providers (github/facebook/...) we have to log-in using user credentials (john/123) and client configurations handled by the auth server
|
||||
*
|
||||
* @author rozagerardo
|
||||
*
|
||||
*/
|
||||
@PropertySource("classpath:webclient-auth-code-login-application.properties")
|
||||
@SpringBootApplication
|
||||
public class OauthClientLoginApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(OauthClientLoginApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.webclient.authorizationcodelogin.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
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.WebClient;
|
||||
|
||||
@Configuration
|
||||
public class WebClientConfig {
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
WebClient webClientForAuthorized(ReactiveClientRegistrationRepository clientRegistrations, ServerOAuth2AuthorizedClientRepository authorizedClients) {
|
||||
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
|
||||
oauth.setDefaultOAuth2AuthorizedClient(true);
|
||||
return WebClient.builder()
|
||||
.filter(oauth)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
WebClient otherWebClient(ReactiveClientRegistrationRepository clientRegistrations, ServerOAuth2AuthorizedClientRepository authorizedClients) {
|
||||
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
|
||||
return WebClient.builder()
|
||||
.filter(oauth)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.webclient.authorizationcodelogin.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class WebSecurityConfig {
|
||||
@Bean
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||
http.authorizeExchange()
|
||||
.anyExchange()
|
||||
.authenticated()
|
||||
.and()
|
||||
.oauth2Login();
|
||||
return http.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.webclient.authorizationcodelogin.web;
|
||||
|
||||
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId;
|
||||
import static org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
||||
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
public class ClientRestController {
|
||||
|
||||
private static final String RESOURCE_URI = "http://localhost:8082/spring-security-oauth-resource/foos/1";
|
||||
|
||||
@Autowired
|
||||
WebClient webClient;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("otherWebClient")
|
||||
WebClient otherWebClient;
|
||||
|
||||
@GetMapping("/auth-code")
|
||||
Mono<String> useOauthWithAuthCode() {
|
||||
Mono<String> retrievedResource = webClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string);
|
||||
}
|
||||
|
||||
@GetMapping("/auth-code-no-client")
|
||||
Mono<String> useOauthWithNoClient() {
|
||||
// This call will fail, since we don't have the client properly set for this webClient
|
||||
Mono<String> retrievedResource = otherWebClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string);
|
||||
}
|
||||
|
||||
@GetMapping("/auth-code-annotated")
|
||||
Mono<String> useOauthWithAuthCodeAndAnnotation(@RegisteredOAuth2AuthorizedClient("bael") OAuth2AuthorizedClient authorizedClient) {
|
||||
Mono<String> retrievedResource = otherWebClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.attributes(oauth2AuthorizedClient(authorizedClient))
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string + ". Principal associated: " + authorizedClient.getPrincipalName() + ". Token will expire at: " + authorizedClient.getAccessToken()
|
||||
.getExpiresAt());
|
||||
}
|
||||
|
||||
@GetMapping("/auth-code-explicit-client")
|
||||
Mono<String> useOauthWithExpicitClient() {
|
||||
Mono<String> retrievedResource = otherWebClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.attributes(clientRegistrationId("bael"))
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
return retrievedResource.map(string -> "We retrieved the following resource using Oauth: " + string);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.webclient.clientcredentials;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
*
|
||||
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
|
||||
*
|
||||
* @author rozagerardo
|
||||
*
|
||||
*/
|
||||
@PropertySource("classpath:webclient-client-credentials-oauth-application.properties")
|
||||
@EnableScheduling
|
||||
@SpringBootApplication
|
||||
public class ClientCredentialsOauthApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ClientCredentialsOauthApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.webclient.clientcredentials.configuration;
|
||||
|
||||
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.UnAuthenticatedServerOAuth2AuthorizedClientRepository;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
@Configuration
|
||||
public class WebClientConfig {
|
||||
|
||||
@Bean
|
||||
WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
|
||||
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
|
||||
oauth.setDefaultClientRegistrationId("bael");
|
||||
return WebClient.builder()
|
||||
.filter(oauth)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.webclient.clientcredentials.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
@Component
|
||||
public class WebClientChonJob {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(WebClientChonJob.class);
|
||||
|
||||
private static final String RESOURCE_URI = "localhost:8082/spring-security-oauth-resource/foos/1";
|
||||
|
||||
@Autowired
|
||||
private WebClient webClient;
|
||||
|
||||
@Scheduled(fixedRate = 5000)
|
||||
public void logResourceServiceResponse() {
|
||||
|
||||
webClient.get()
|
||||
.uri(RESOURCE_URI)
|
||||
.retrieve()
|
||||
.bodyToMono(String.class)
|
||||
.map(string -> "We retrieved the following resource using Client Credentials Grant Type: " + string)
|
||||
.subscribe(logger::info);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.webclient.manualrequest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
*
|
||||
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
|
||||
*
|
||||
* @author rozagerardo
|
||||
*
|
||||
*/
|
||||
@PropertySource("classpath:webclient-manual-request-oauth-application.properties")
|
||||
@SpringBootApplication
|
||||
public class ManualRequestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ManualRequestApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.webclient.manualrequest.configure;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
@Configuration
|
||||
public class WebClientConfig {
|
||||
|
||||
@Bean
|
||||
public WebClient configureWebClient() {
|
||||
return WebClient.builder()
|
||||
.build();
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.webclient.manualrequest.configure;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.web.server.ServerHttpSecurity;
|
||||
import org.springframework.security.web.server.SecurityWebFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class WebSecurityConfig {
|
||||
@Bean
|
||||
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
|
||||
http.authorizeExchange()
|
||||
.anyExchange()
|
||||
.permitAll();
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.webclient.manualrequest.web;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
||||
import org.springframework.util.Base64Utils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.nimbusds.oauth2.sdk.GrantType;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@RestController
|
||||
public class ManualOauthRequestController {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ManualOauthRequestController.class);
|
||||
|
||||
private static final String RESOURCE_ENDPOINT = "localhost:8082/spring-security-oauth-resource/foos/1";
|
||||
|
||||
@Value("${the.authorization.client-id}")
|
||||
private String clientId;
|
||||
|
||||
@Value("${the.authorization.client-secret}")
|
||||
private String clientSecret;
|
||||
|
||||
@Value("${the.authorization.token-uri}")
|
||||
private String tokenUri;
|
||||
|
||||
@Autowired
|
||||
WebClient client;
|
||||
|
||||
@GetMapping("/manual-request-oauth")
|
||||
public Mono<String> obtainSecuredResource() {
|
||||
logger.info("Creating web client...");
|
||||
Mono<String> resource = client.post()
|
||||
.uri(tokenUri)
|
||||
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString((clientId + ":" + clientSecret).getBytes()))
|
||||
.body(BodyInserters.fromFormData(OAuth2ParameterNames.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS.getValue()))
|
||||
.retrieve()
|
||||
.bodyToMono(JsonNode.class)
|
||||
.flatMap(tokenResponse -> {
|
||||
String accessTokenValue = tokenResponse.get("access_token")
|
||||
.textValue();
|
||||
logger.info("Retrieved the following access token: {}", accessTokenValue);
|
||||
return client.get()
|
||||
.uri(RESOURCE_ENDPOINT)
|
||||
.headers(h -> h.setBearerAuth(accessTokenValue))
|
||||
.retrieve()
|
||||
.bodyToMono(String.class);
|
||||
});
|
||||
logger.info("non-blocking Oauth calls registered...");
|
||||
return resource.map(res -> "Retrieved the resource using a manual approach: " + res);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
logging:
|
||||
level:
|
||||
root: DEBUG
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
security:
|
||||
oauth2:
|
||||
client:
|
||||
registration:
|
||||
google:
|
||||
client-id: YOUR_APP_CLIENT_ID
|
||||
client-secret: YOUR_APP_CLIENT_SECRET
|
||||
custom:
|
||||
client-id: fooClientIdPassword
|
||||
client-secret: secret
|
||||
scopes: read,foo
|
||||
authorization-grant-type: authorization_code
|
||||
redirect-uri-template: http://localhost:8080/login/oauth2/code/custom
|
||||
provider:
|
||||
custom:
|
||||
authorization-uri: http://localhost:8081/spring-security-oauth-server/oauth/authorize
|
||||
token-uri: http://localhost:8081/spring-security-oauth-server/oauth/token
|
||||
user-info-uri: http://localhost:8088/spring-security-oauth-resource/users/extra
|
||||
user-name-attribute: user_name
|
||||
@@ -0,0 +1,10 @@
|
||||
spring.security.oauth2.client.registration.bael.client-name=bael
|
||||
spring.security.oauth2.client.registration.bael.client-id=fooClientIdPassword
|
||||
spring.security.oauth2.client.registration.bael.client-secret=secret
|
||||
spring.security.oauth2.client.registration.bael.authorization-grant-type=authorization_code
|
||||
spring.security.oauth2.client.registration.bael.redirect-uri=http://localhost:8080/authorize/oauth2/code/bael
|
||||
|
||||
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token
|
||||
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8081/spring-security-oauth-server/oauth/authorize
|
||||
|
||||
spring.security.user.password=pass
|
||||
@@ -0,0 +1,10 @@
|
||||
spring.security.oauth2.client.registration.bael.client-name=bael
|
||||
spring.security.oauth2.client.registration.bael.client-id=fooClientIdPassword
|
||||
spring.security.oauth2.client.registration.bael.client-secret=secret
|
||||
spring.security.oauth2.client.registration.bael.authorization-grant-type=authorization_code
|
||||
spring.security.oauth2.client.registration.bael.redirect-uri=http://localhost:8080/login/oauth2/code/bael
|
||||
|
||||
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token
|
||||
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8081/spring-security-oauth-server/oauth/authorize
|
||||
spring.security.oauth2.client.provider.bael.user-info-uri=http://localhost:8082/spring-security-oauth-resource/users/extra
|
||||
spring.security.oauth2.client.provider.bael.user-name-attribute=user_name
|
||||
@@ -0,0 +1,4 @@
|
||||
spring.security.oauth2.client.registration.bael.authorization-grant-type=client_credentials
|
||||
spring.security.oauth2.client.registration.bael.client-id=fooClientIdPassword
|
||||
spring.security.oauth2.client.registration.bael.client-secret=secret
|
||||
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token
|
||||
@@ -0,0 +1,3 @@
|
||||
the.authorization.client-id=fooClientIdPassword
|
||||
the.authorization.client-secret=secret
|
||||
the.authorization.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.reactive.oauth;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class Spring5ReactiveOauthIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.webclient.clientcredentials;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.webclient.clientcredentials.service.WebClientChonJob;
|
||||
import com.baeldung.webclient.utils.ListAppender;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
|
||||
/**
|
||||
*
|
||||
* Note: this Live test requires the Authorization Service and the Resource service located in the Baeldung/spring-security-oauth repo
|
||||
*
|
||||
* @author rozagerardo
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = { ClientCredentialsOauthApplication.class })
|
||||
public class OAuth2ClientCredentialsLiveTest {
|
||||
|
||||
@Autowired
|
||||
WebClientChonJob service;
|
||||
|
||||
@Before
|
||||
public void clearLogList() {
|
||||
ListAppender.clearEventList();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFooWithNullId_whenProcessFoo_thenLogsWithDebugTrace() throws Exception {
|
||||
service.logResourceServiceResponse();
|
||||
|
||||
Thread.sleep(3000);
|
||||
|
||||
Collection<String> allLoggedEntries = ListAppender.getEvents()
|
||||
.stream()
|
||||
.map(ILoggingEvent::getFormattedMessage)
|
||||
.collect(Collectors.toList());
|
||||
assertThat(allLoggedEntries).anyMatch(entry -> entry.contains("We retrieved the following resource using Client Credentials Grant Type: {\"id\""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.webclient.manualrequest;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;
|
||||
|
||||
/**
|
||||
*
|
||||
* Note: this Live test requires not only the corresponding application running,
|
||||
* but also the Authorization Service and the Resource service located in the Baeldung/spring-security-oauth repo
|
||||
*
|
||||
*
|
||||
* @author ger
|
||||
*
|
||||
*/
|
||||
public class OAuth2ManualRequestLiveTest {
|
||||
|
||||
private static final String BASE_URL = "http://localhost:8080";
|
||||
private static final String MANUAL_REQUEST_URI = "/manual-request-oauth";
|
||||
|
||||
private static WebTestClient client;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
client = WebTestClient.bindToServer()
|
||||
.baseUrl(BASE_URL)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingDebugHookOn_thenObtainExpectedMessage() {
|
||||
ResponseSpec response = client.get()
|
||||
.uri(MANUAL_REQUEST_URI)
|
||||
.exchange();
|
||||
|
||||
response.expectStatus()
|
||||
.isOk()
|
||||
.expectBody(String.class)
|
||||
.value(Matchers.containsString("Retrieved the resource using a manual approach: {\"id\""));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.webclient.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
|
||||
public class ListAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
static private List<ILoggingEvent> events = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
events.add(eventObject);
|
||||
}
|
||||
|
||||
public static List<ILoggingEvent> getEvents() {
|
||||
return events;
|
||||
}
|
||||
|
||||
public static void clearEventList() {
|
||||
events.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include
|
||||
resource="org/springframework/boot/logging/logback/base.xml" />
|
||||
<appender name="LISTAPPENDER"
|
||||
class="com.baeldung.webclient.utils.ListAppender">
|
||||
</appender>
|
||||
<logger
|
||||
name="com.baeldung.webclient.clientcredentials.service.WebClientChonJob">
|
||||
<appender-ref ref="LISTAPPENDER" />
|
||||
</logger>
|
||||
<root level="info">
|
||||
<appender-ref ref="CONSOLE" />
|
||||
<appender-ref ref="LISTAPPENDER" />
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user