Skip to content

Commit b395dc1

Browse files
rozagerardomaibin
authored andcommitted
Now using Auth and Resource servers from Baeldung/spring-security-oauth (eugenp#6128)
1 parent 469e36f commit b395dc1

21 files changed

+48
-174
lines changed

spring-5-reactive-oauth/src/main/java/com/baeldung/webclient/authorizationcodeclient/OauthClientApplication.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
/**
88
*
9-
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
9+
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
1010
*
11-
* 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
11+
* 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
1212
*
1313
* @author rozagerardo
1414
*

spring-5-reactive-oauth/src/main/java/com/baeldung/webclient/authorizationcodeclient/web/ClientRestController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@RestController
1616
public class ClientRestController {
1717

18-
private static final String RESOURCE_URI = "http://localhost:8084/retrieve-resource";
18+
private static final String RESOURCE_URI = "http://localhost:8082/spring-security-oauth-resource/foos/1";
1919

2020
@Autowired
2121
WebClient webClient;
+4-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
/**
88
*
9-
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
9+
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
1010
*
11-
* 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
11+
* 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
1212
*
1313
* @author rozagerardo
1414
*
1515
*/
1616
@PropertySource("classpath:webclient-auth-code-login-application.properties")
1717
@SpringBootApplication
18-
public class OauthClientApplication {
18+
public class OauthClientLoginApplication {
1919

2020
public static void main(String[] args) {
21-
SpringApplication.run(OauthClientApplication.class, args);
21+
SpringApplication.run(OauthClientLoginApplication.class, args);
2222
}
2323

2424
}

spring-5-reactive-oauth/src/main/java/com/baeldung/webclient/authorizationcodelogin/web/ClientRestController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@RestController
1717
public class ClientRestController {
1818

19-
private static final String RESOURCE_URI = "http://localhost:8084/retrieve-resource";
19+
private static final String RESOURCE_URI = "http://localhost:8082/spring-security-oauth-resource/foos/1";
2020

2121
@Autowired
2222
WebClient webClient;

spring-5-reactive-oauth/src/main/java/com/baeldung/webclient/clientcredentials/ClientCredentialsOauthApplication.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
/**
99
*
10-
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
11-
*
12-
* 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)
10+
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
1311
*
1412
* @author rozagerardo
1513
*

spring-5-reactive-oauth/src/main/java/com/baeldung/webclient/clientcredentials/service/WebClientChonJob.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import org.slf4j.LoggerFactory;
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.scheduling.annotation.Scheduled;
7-
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
8-
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
97
import org.springframework.stereotype.Component;
108
import org.springframework.web.reactive.function.client.WebClient;
119

@@ -14,12 +12,12 @@ public class WebClientChonJob {
1412

1513
Logger logger = LoggerFactory.getLogger(WebClientChonJob.class);
1614

17-
private static final String RESOURCE_URI = "http://localhost:8084/retrieve-resource";
15+
private static final String RESOURCE_URI = "localhost:8082/spring-security-oauth-resource/foos/1";
1816

1917
@Autowired
2018
private WebClient webClient;
2119

22-
@Scheduled(fixedRate = 1000)
20+
@Scheduled(fixedRate = 5000)
2321
public void logResourceServiceResponse() {
2422

2523
webClient.get()

spring-5-reactive-oauth/src/main/java/com/baeldung/webclient/manualrequest/ManualRequestApplication.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77
/**
88
*
9-
* Note: This app is configured to use the authorization service and the resource service located in module spring-5-security-oauth
10-
*
11-
* 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
9+
* Note: This app is configured to use the authorization service and the resource service located in Baeldung/spring-security-oauth repo
1210
*
1311
* @author rozagerardo
1412
*
1513
*/
14+
@PropertySource("classpath:webclient-manual-request-oauth-application.properties")
1615
@SpringBootApplication
1716
public class ManualRequestApplication {
1817

spring-5-reactive-oauth/src/main/java/com/baeldung/webclient/manualrequest/web/ManualOauthRequestController.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
67
import org.springframework.http.HttpHeaders;
7-
import org.springframework.http.MediaType;
88
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
99
import org.springframework.util.Base64Utils;
1010
import org.springframework.web.bind.annotation.GetMapping;
@@ -22,10 +22,16 @@ public class ManualOauthRequestController {
2222

2323
private static Logger logger = LoggerFactory.getLogger(ManualOauthRequestController.class);
2424

25-
private static final String TOKEN_ENDPOINT = "localhost:8085/oauth/token";
26-
private static final String RESOURCE_ENDPOINT = "localhost:8084/retrieve-resource";
27-
private static final String CLIENT_ID = "bael-client-id";
28-
private static final String CLIENT_SECRET = "bael-secret";
25+
private static final String RESOURCE_ENDPOINT = "localhost:8082/spring-security-oauth-resource/foos/1";
26+
27+
@Value("${the.authorization.client-id}")
28+
private String clientId;
29+
30+
@Value("${the.authorization.client-secret}")
31+
private String clientSecret;
32+
33+
@Value("${the.authorization.token-uri}")
34+
private String tokenUri;
2935

3036
@Autowired
3137
WebClient client;
@@ -34,8 +40,8 @@ public class ManualOauthRequestController {
3440
public Mono<String> obtainSecuredResource() {
3541
logger.info("Creating web client...");
3642
Mono<String> resource = client.post()
37-
.uri(TOKEN_ENDPOINT)
38-
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString((CLIENT_ID + ":" + CLIENT_SECRET).getBytes()))
43+
.uri(tokenUri)
44+
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString((clientId + ":" + clientSecret).getBytes()))
3945
.body(BodyInserters.fromFormData(OAuth2ParameterNames.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS.getValue()))
4046
.retrieve()
4147
.bodyToMono(JsonNode.class)
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
spring.security.oauth2.client.registration.bael.client-name=bael
2-
spring.security.oauth2.client.registration.bael.client-id=bael-client-id
3-
spring.security.oauth2.client.registration.bael.client-secret=bael-secret
2+
spring.security.oauth2.client.registration.bael.client-id=fooClientIdPassword
3+
spring.security.oauth2.client.registration.bael.client-secret=secret
44
spring.security.oauth2.client.registration.bael.authorization-grant-type=authorization_code
55
spring.security.oauth2.client.registration.bael.redirect-uri=http://localhost:8080/authorize/oauth2/code/bael
66

7-
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8085/oauth/token
8-
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8085/oauth/authorize
7+
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token
8+
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8081/spring-security-oauth-server/oauth/authorize
99

1010
spring.security.user.password=pass
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
spring.security.oauth2.client.registration.bael.client-name=bael
2-
spring.security.oauth2.client.registration.bael.client-id=bael-client-id
3-
spring.security.oauth2.client.registration.bael.client-secret=bael-secret
2+
spring.security.oauth2.client.registration.bael.client-id=fooClientIdPassword
3+
spring.security.oauth2.client.registration.bael.client-secret=secret
44
spring.security.oauth2.client.registration.bael.authorization-grant-type=authorization_code
55
spring.security.oauth2.client.registration.bael.redirect-uri=http://localhost:8080/login/oauth2/code/bael
66

7-
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8085/oauth/token
8-
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8085/oauth/authorize
9-
spring.security.oauth2.client.provider.bael.user-info-uri=http://localhost:8084/user
10-
spring.security.oauth2.client.provider.bael.user-name-attribute=name
7+
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token
8+
spring.security.oauth2.client.provider.bael.authorization-uri=http://localhost:8081/spring-security-oauth-server/oauth/authorize
9+
spring.security.oauth2.client.provider.bael.user-info-uri=http://localhost:8082/spring-security-oauth-resource/users/extra
10+
spring.security.oauth2.client.provider.bael.user-name-attribute=user_name
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
spring.security.oauth2.client.registration.bael.authorization-grant-type=client_credentials
2-
spring.security.oauth2.client.registration.bael.client-id=bael-client-id
3-
spring.security.oauth2.client.registration.bael.client-secret=bael-secret
4-
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8085/oauth/token
2+
spring.security.oauth2.client.registration.bael.client-id=fooClientIdPassword
3+
spring.security.oauth2.client.registration.bael.client-secret=secret
4+
spring.security.oauth2.client.provider.bael.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
the.authorization.client-id=fooClientIdPassword
2+
the.authorization.client-secret=secret
3+
the.authorization.token-uri=http://localhost:8081/spring-security-oauth-server/oauth/token

spring-5-reactive-oauth/src/test/java/com/baeldung/webclient/clientcredentials/OAuth2ClientCredentialsLiveTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
/**
2121
*
22-
* Note: this Live test requires the Authorization Service and the Resource service located in the spring-5-security-oauth module
22+
* Note: this Live test requires the Authorization Service and the Resource service located in the Baeldung/spring-security-oauth repo
2323
*
24-
* @author ger
24+
* @author rozagerardo
2525
*
2626
*/
2727
@RunWith(SpringRunner.class)
@@ -46,7 +46,7 @@ public void givenFooWithNullId_whenProcessFoo_thenLogsWithDebugTrace() throws Ex
4646
.stream()
4747
.map(ILoggingEvent::getFormattedMessage)
4848
.collect(Collectors.toList());
49-
assertThat(allLoggedEntries).anyMatch(entry -> entry.contains("We retrieved the following resource using Client Credentials Grant Type: This is the resource!"));
49+
assertThat(allLoggedEntries).anyMatch(entry -> entry.contains("We retrieved the following resource using Client Credentials Grant Type: {\"id\""));
5050
}
5151

5252
}

spring-5-reactive-oauth/src/test/java/com/baeldung/webclient/manualrequest/OAuth2ManualRequestLiveTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.webclient.manualrequest;
22

3+
import org.hamcrest.Matchers;
34
import org.junit.Before;
45
import org.junit.Test;
56
import org.springframework.test.web.reactive.server.WebTestClient;
@@ -8,7 +9,7 @@
89
/**
910
*
1011
* Note: this Live test requires not only the corresponding application running,
11-
* but also the Authorization Service and the Resource service located in the spring-5-security-oauth module.
12+
* but also the Authorization Service and the Resource service located in the Baeldung/spring-security-oauth repo
1213
*
1314
*
1415
* @author ger
@@ -37,7 +38,7 @@ public void whenRequestingDebugHookOn_thenObtainExpectedMessage() {
3738
response.expectStatus()
3839
.isOk()
3940
.expectBody(String.class)
40-
.isEqualTo("Retrieved the resource using a manual approach: This is the resource!");
41+
.value(Matchers.containsString("Retrieved the resource using a manual approach: {\"id\""));
4142
}
4243

4344
}

spring-5-security-oauth/src/main/java/com/baeldung/webclient/authorizationserver/AuthorizationServerApplication.java

-17
This file was deleted.

spring-5-security-oauth/src/main/java/com/baeldung/webclient/authorizationserver/configuration/WebSecurityConfig.java

-26
This file was deleted.

spring-5-security-oauth/src/main/java/com/baeldung/webclient/resourceserver/ResourceServerApplication.java

-17
This file was deleted.

spring-5-security-oauth/src/main/java/com/baeldung/webclient/resourceserver/configuration/AuthorizationConfigs.java

-29
This file was deleted.

spring-5-security-oauth/src/main/java/com/baeldung/webclient/resourceserver/web/ResourceRestController.java

-23
This file was deleted.

spring-5-security-oauth/src/main/resources/webclient-authorization-application.properties

-13
This file was deleted.

spring-5-security-oauth/src/main/resources/webclient-resources-application.properties

-6
This file was deleted.

0 commit comments

Comments
 (0)