Skip to content

Commit 4cc67ca

Browse files
authored
SUPPORT-29677 add option to provide custom auth and api url for spring example project (#764)
1 parent 48edc67 commit 4cc67ca

File tree

6 files changed

+52
-20
lines changed

6 files changed

+52
-20
lines changed

examples/spring/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Example to show how the ME endpoints can be used with the Java SDK in a Spring B
1515
2. Navigate to the path `spring/`.
1616
3. Register the client credentials in environment variables:
1717
`CTP_CLIENT_ID`, `CTP_CLIENT_SECRET` and `CTP_PROJECT_KEY`
18+
4. If your [project region](https://docs.commercetools.com/api/general-concepts#regions) is not GCP Europe, then register [API URL](https://docs.commercetools.com/api/general-concepts#hosts) and [Auth URL](https://docs.commercetools.com/api/authorization#requesting-an-access-token-using-the-composable-commerce-oauth-20-service) in environment variables: `CTP_PROJECT_API_BASE_URL`, `CTP_PROJECT_AUTH_URL`
1819

1920
## Using the ME Endpoint Checkout App
2021

examples/spring/src/main/java/com/commercetools/sdk/examples/spring/config/CtpSecurityConfig.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public static class CtpReactiveAuthenticationManagerResolver
104104
@Value(value = "${ctp.project.key}")
105105
private String projectKey;
106106

107+
@Value(value = "${ctp.project.api.base.url:#{null}}")
108+
private String apiBaseUrl;
109+
110+
@Value(value = "${ctp.project.auth.url:#{null}}")
111+
private String authUrl;
112+
107113
private ClientCredentials credentials() {
108114
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
109115
}
@@ -123,9 +129,15 @@ private ProjectApiRoot meClient(final ApiHttpClient client, final Mono<WebSessio
123129
TokenStorage storage = new SessionTokenStorage(session);
124130

125131
ApiRootBuilder builder = ApiRootBuilder.of(client)
126-
.withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
127-
.withProjectKey(projectKey)
132+
.withApiBaseUrl(apiBaseUrl != null ? apiBaseUrl: ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
133+
.withProjectKey(projectKey);
134+
135+
if (authUrl != null) {
136+
builder = builder.withAnonymousRefreshFlow(credentials(), authUrl + "/oauth/" + projectKey + "/anonymous/token", authUrl + "/oauth/token", storage);
137+
} else {
138+
builder = builder
128139
.withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage);
140+
}
129141

130142
return builder.build(projectKey);
131143
}

examples/spring/src/main/java/com/commercetools/sdk/examples/spring/config/MeClientFilter.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
package com.commercetools.sdk.examples.spring.config;
33

4+
import java.util.Map;
5+
46
import com.commercetools.api.client.ProjectApiRoot;
57
import com.commercetools.api.defaultconfig.ApiRootBuilder;
68
import com.commercetools.api.defaultconfig.ServiceRegion;
@@ -18,8 +20,6 @@
1820
import org.springframework.web.server.WebSession;
1921
import reactor.core.publisher.Mono;
2022

21-
import java.util.Map;
22-
2323
@Component
2424
public class MeClientFilter implements WebFilter {
2525
private final ApiHttpClient client;
@@ -33,6 +33,12 @@ public class MeClientFilter implements WebFilter {
3333
@Value(value = "${ctp.project.key}")
3434
private String projectKey;
3535

36+
@Value(value = "${ctp.project.api.base.url:#{null}}")
37+
private String apiBaseUrl;
38+
39+
@Value(value = "${ctp.project.auth.url:#{null}}")
40+
private String authUrl;
41+
3642
private ClientCredentials credentials() {
3743
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
3844
}
@@ -44,7 +50,8 @@ public MeClientFilter(ApiHttpClient client) {
4450

4551
@Override
4652
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
47-
final ContextApiHttpClient contextClient = contextClient(client, new MDCContext(Map.of("requestId", exchange.getRequest().getId())));
53+
final ContextApiHttpClient contextClient = contextClient(client,
54+
new MDCContext(Map.of("requestId", exchange.getRequest().getId())));
4855
final ProjectApiRoot meClient = exchange.getAttributeOrDefault("meClient",
4956
meClient(contextClient, exchange.getSession()));
5057
exchange.getAttributes().put("meClient", meClient);
@@ -54,12 +61,18 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
5461
}
5562

5663
private ProjectApiRoot meClient(ApiHttpClient client, Mono<WebSession> session) {
57-
TokenStorage storage = new SessionTokenStorage(session);
64+
final TokenStorage storage = new SessionTokenStorage(session);
5865

5966
ApiRootBuilder builder = ApiRootBuilder.of(client)
60-
.withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
61-
.withProjectKey(projectKey)
62-
.withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage);
67+
.withApiBaseUrl(apiBaseUrl != null ? apiBaseUrl: ServiceRegion.GCP_EUROPE_WEST1.getApiUrl())
68+
.withProjectKey(projectKey);
69+
70+
if (authUrl != null) {
71+
builder = builder.withAnonymousRefreshFlow(credentials(), authUrl + "/oauth/" + projectKey + "/anonymous/token", authUrl + "/oauth/token", storage);
72+
} else {
73+
builder = builder
74+
.withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage);
75+
}
6376

6477
return builder.build(projectKey);
6578
}

examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpClientBeanService.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
package com.commercetools.sdk.examples.spring.service;
33

4-
import java.util.List;
5-
import java.util.concurrent.CompletableFuture;
6-
74
import com.commercetools.api.client.ProjectApiRoot;
85
import com.commercetools.api.defaultconfig.ApiRootBuilder;
96

@@ -29,15 +26,26 @@ public class CtpClientBeanService {
2926
@Value(value = "${ctp.project.key}")
3027
private String projectKey;
3128

29+
@Value(value = "${ctp.project.api.base.url:#{null}}")
30+
private String apiBaseUrl;
31+
32+
@Value(value = "${ctp.project.auth.url:#{null}}")
33+
private String authUrl;
34+
3235
private ClientCredentials credentials() {
3336
return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build();
3437
}
3538

3639
@Bean
3740
public ApiHttpClient client() {
38-
return ApiRootBuilder.of()
39-
.defaultClient(credentials())
40-
.buildClient();
41+
ApiRootBuilder builder;
42+
if (authUrl != null) {
43+
builder = ApiRootBuilder.of().defaultClient(credentials(), authUrl + "/oauth/token", apiBaseUrl);
44+
} else {
45+
builder = ApiRootBuilder.of().defaultClient(credentials());
46+
}
47+
48+
return builder.buildClient();
4149
}
4250

4351
@Bean

examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpReactiveAuthenticationManager.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.commercetools.api.defaultconfig.ServiceRegion;
1010
import com.commercetools.api.models.cart.CartReferenceBuilder;
1111
import com.commercetools.api.models.customer.CustomerSignInResult;
12-
import com.commercetools.api.models.customer.CustomerSigninBuilder;
1312
import com.commercetools.api.models.customer.MyCustomerSigninBuilder;
1413
import com.commercetools.sdk.examples.spring.config.CtpUserDetails;
1514
import com.commercetools.sdk.examples.spring.config.TokenGrantedAuthority;

examples/spring/src/main/java/com/commercetools/sdk/examples/spring/web/AppController.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import com.commercetools.api.models.product.ProductProjection;
1010
import com.commercetools.sdk.examples.spring.service.MeRepository;
1111
import com.commercetools.sdk.examples.spring.service.ProductsRepository;
12-
import com.commercetools.sdk.examples.spring.service.ProjectRepository;
1312

14-
import org.springframework.beans.factory.annotation.Autowired;
1513
import org.springframework.security.core.Authentication;
1614
import org.springframework.stereotype.Controller;
1715
import org.springframework.ui.Model;
@@ -49,9 +47,10 @@ public String home(Model model) {
4947
}
5048

5149
@GetMapping("/p")
52-
public String pop(@RequestAttribute("contextRoot") ProjectApiRoot contextRoot, @RequestAttribute("meClient") ProjectApiRoot meClient, Model model, WebSession session) {
50+
public String pop(@RequestAttribute("contextRoot") ProjectApiRoot contextRoot,
51+
@RequestAttribute("meClient") ProjectApiRoot meClient, Model model, WebSession session) {
5352

54-
Mono<List<ProductProjection>> products = new ProductsRepository(contextRoot).products();
53+
Mono<List<ProductProjection>> products = new ProductsRepository(contextRoot).products();
5554
final Mono<Cart> cart = new MeRepository(meClient, session).meCart();
5655

5756
model.addAttribute("products", products);

0 commit comments

Comments
 (0)