Now using Auth and Resource servers from Baeldung/spring-security-oauth (#6128)

This commit is contained in:
Ger Roza
2019-01-12 18:57:19 -02:00
committed by maibin
parent 469e36f07a
commit b395dc1d41
21 changed files with 48 additions and 174 deletions

View File

@@ -6,9 +6,9 @@ import org.springframework.context.annotation.PropertySource;
/**
*
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
* 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 (bael-user/bael-password) and client configurations (bael-client-id/bael-secret) handled by the auth server
* 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
*

View File

@@ -15,7 +15,7 @@ import reactor.core.publisher.Mono;
@RestController
public class ClientRestController {
private static final String RESOURCE_URI = "http://localhost:8084/retrieve-resource";
private static final String RESOURCE_URI = "http://localhost:8082/spring-security-oauth-resource/foos/1";
@Autowired
WebClient webClient;

View File

@@ -6,19 +6,19 @@ import org.springframework.context.annotation.PropertySource;
/**
*
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
* 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 (bael-user/bael-password) and client configurations (bael-client-id/bael-secret) handled by the auth server
* 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 OauthClientApplication {
public class OauthClientLoginApplication {
public static void main(String[] args) {
SpringApplication.run(OauthClientApplication.class, args);
SpringApplication.run(OauthClientLoginApplication.class, args);
}
}

View File

@@ -16,7 +16,7 @@ import reactor.core.publisher.Mono;
@RestController
public class ClientRestController {
private static final String RESOURCE_URI = "http://localhost:8084/retrieve-resource";
private static final String RESOURCE_URI = "http://localhost:8082/spring-security-oauth-resource/foos/1";
@Autowired
WebClient webClient;

View File

@@ -7,9 +7,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
/**
*
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
*
* As we usually do with other well-known auth providers (github/facebook/...) we have to log-in using credentials handled by the auth server (bael-user/bael-password)
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
*
* @author rozagerardo
*

View File

@@ -4,8 +4,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
@@ -14,12 +12,12 @@ public class WebClientChonJob {
Logger logger = LoggerFactory.getLogger(WebClientChonJob.class);
private static final String RESOURCE_URI = "http://localhost:8084/retrieve-resource";
private static final String RESOURCE_URI = "localhost:8082/spring-security-oauth-resource/foos/1";
@Autowired
private WebClient webClient;
@Scheduled(fixedRate = 1000)
@Scheduled(fixedRate = 5000)
public void logResourceServiceResponse() {
webClient.get()

View File

@@ -6,13 +6,12 @@ import org.springframework.context.annotation.PropertySource;
/**
*
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
*
* As we usually do with other well-known auth providers (github/facebook/...) we have to log-in using user credentials (bael-user/bael-password) and client configurations (bael-client-id/bael-secret) handled by the auth server
* 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 {

View File

@@ -3,8 +3,8 @@ 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.http.MediaType;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
@@ -22,10 +22,16 @@ public class ManualOauthRequestController {
private static Logger logger = LoggerFactory.getLogger(ManualOauthRequestController.class);
private static final String TOKEN_ENDPOINT = "localhost:8085/oauth/token";
private static final String RESOURCE_ENDPOINT = "localhost:8084/retrieve-resource";
private static final String CLIENT_ID = "bael-client-id";
private static final String CLIENT_SECRET = "bael-secret";
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;
@@ -34,8 +40,8 @@ public class ManualOauthRequestController {
public Mono<String> obtainSecuredResource() {
logger.info("Creating web client...");
Mono<String> resource = client.post()
.uri(TOKEN_ENDPOINT)
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes()))
.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)

View File

@@ -1,10 +1,10 @@
spring.security.oauth2.client.registration.bael.client-name=bael
spring.security.oauth2.client.registration.bael.client-id=bael-client-id
spring.security.oauth2.client.registration.bael.client-secret=bael-secret
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:8085/oauth/token
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8085/oauth/authorize
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

View File

@@ -1,10 +1,10 @@
spring.security.oauth2.client.registration.bael.client-name=bael
spring.security.oauth2.client.registration.bael.client-id=bael-client-id
spring.security.oauth2.client.registration.bael.client-secret=bael-secret
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:8085/oauth/token
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8085/oauth/authorize
spring.security.oauth2.client.provider.bael.user-info-uri=http://localhost:8084/user
spring.security.oauth2.client.provider.bael.user-name-attribute=name
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

View File

@@ -1,4 +1,4 @@
spring.security.oauth2.client.registration.bael.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.bael.client-id=bael-client-id
spring.security.oauth2.client.registration.bael.client-secret=bael-secret
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8085/oauth/token
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

View File

@@ -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

View File

@@ -19,9 +19,9 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
/**
*
* Note: this Live test requires the Authorization Service and the Resource service located in the spring-5-security-oauth module
* Note: this Live test requires the Authorization Service and the Resource service located in the Baeldung/spring-security-oauth repo
*
* @author ger
* @author rozagerardo
*
*/
@RunWith(SpringRunner.class)
@@ -46,7 +46,7 @@ public class OAuth2ClientCredentialsLiveTest {
.stream()
.map(ILoggingEvent::getFormattedMessage)
.collect(Collectors.toList());
assertThat(allLoggedEntries).anyMatch(entry -> entry.contains("We retrieved the following resource using Client Credentials Grant Type: This is the resource!"));
assertThat(allLoggedEntries).anyMatch(entry -> entry.contains("We retrieved the following resource using Client Credentials Grant Type: {\"id\""));
}
}

View File

@@ -1,5 +1,6 @@
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;
@@ -8,7 +9,7 @@ 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 spring-5-security-oauth module.
* but also the Authorization Service and the Resource service located in the Baeldung/spring-security-oauth repo
*
*
* @author ger
@@ -37,7 +38,7 @@ public class OAuth2ManualRequestLiveTest {
response.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("Retrieved the resource using a manual approach: This is the resource!");
.value(Matchers.containsString("Retrieved the resource using a manual approach: {\"id\""));
}
}