summaryrefslogtreecommitdiff
path: root/microservices/02-api-gateway/src/main
diff options
context:
space:
mode:
authorKamal Wickramanayake <kamal@inbox.lk>2026-07-06 06:32:08 +0530
committerKamal Wickramanayake <kamal@inbox.lk>2026-07-06 06:32:08 +0530
commit5ca98df7caebf56990e26aa83a1a2b626ba16660 (patch)
treee71cd8298a49c95d7a03697ef4324b342b399f3e /microservices/02-api-gateway/src/main
parent504045fb23fa75a520a4d2665aea7d423db1fe14 (diff)
Microservices: Documentation improvedHEADmaster
Diffstat (limited to 'microservices/02-api-gateway/src/main')
-rw-r--r--microservices/02-api-gateway/src/main/java/com/example/apigateway/config/RoleHeaderRelayFilter.java13
-rw-r--r--microservices/02-api-gateway/src/main/resources/application.yaml6
2 files changed, 12 insertions, 7 deletions
diff --git a/microservices/02-api-gateway/src/main/java/com/example/apigateway/config/RoleHeaderRelayFilter.java b/microservices/02-api-gateway/src/main/java/com/example/apigateway/config/RoleHeaderRelayFilter.java
index e3bb241..0bdef6b 100644
--- a/microservices/02-api-gateway/src/main/java/com/example/apigateway/config/RoleHeaderRelayFilter.java
+++ b/microservices/02-api-gateway/src/main/java/com/example/apigateway/config/RoleHeaderRelayFilter.java
@@ -4,8 +4,6 @@ import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.oauth2.core.user.OAuth2User;
@@ -33,11 +31,11 @@ public class RoleHeaderRelayFilter implements GlobalFilter, Ordered {
.flatMap(authentication -> {
if (authentication != null && authentication.getPrincipal() instanceof OAuth2User) {
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
-
+
// Extract username (adjust the attribute key based on your IdP provider)
String username = oAuth2User.getAttribute("preferred_username");
if (username == null) {
- username = oAuth2User.getName();
+ username = oAuth2User.getName();
}
// Extract roles/authorities
@@ -48,13 +46,14 @@ public class RoleHeaderRelayFilter implements GlobalFilter, Ordered {
// Mutate the request with new headers
ServerHttpRequest mutatedRequest = exchange.getRequest().mutate()
.header("X-User-Roles", roles)
- .header("X-User-Name", username) // Changed 'U-User-Name' to standard 'X-' format for consistency, or use your exact key
+ .header("X-User-Name", username) // Changed 'U-User-Name' to standard 'X-' format for
+ // consistency, or use your exact key
.build();
// Proceed with the mutated exchange
return chain.filter(exchange.mutate().request(mutatedRequest).build());
}
-
+
// If not authenticated via OAuth2User, just pass through
return chain.filter(exchange);
})
@@ -64,6 +63,6 @@ public class RoleHeaderRelayFilter implements GlobalFilter, Ordered {
@Override
public int getOrder() {
// Run after the Security Web Filter loads the context, but before routing
- return Ordered.LOWEST_PRECEDENCE - 1;
+ return Ordered.LOWEST_PRECEDENCE - 1;
}
}
diff --git a/microservices/02-api-gateway/src/main/resources/application.yaml b/microservices/02-api-gateway/src/main/resources/application.yaml
index f51dbfe..4e1f9e7 100644
--- a/microservices/02-api-gateway/src/main/resources/application.yaml
+++ b/microservices/02-api-gateway/src/main/resources/application.yaml
@@ -2,6 +2,8 @@ server:
port: 8050
address: 127.0.0.1
+# From where this API gatway can load roles of authenticated users?
+# Look at the README file for details.
app:
userRolesUrl: "http://localhost:8052/myservices/security/open/users/{username}/roles"
@@ -49,6 +51,7 @@ spring:
- PrefixPath=/myservices
- TokenRelay # Add oauth (or jwt) token to request header before forwarding
+ # Register this API gateway as an OAuth2 client with the OAuth2 authorization server
security:
oauth2:
client:
@@ -66,6 +69,9 @@ spring:
scope:
- openid
- profile
+
+# Instead of coding a SecurityConfig class with OAuth2 configurations, com.c4-soft.springaddons dependency
+# allows configuring some OAuth2 behaviour within this application.yaml file. This is just convenient.
com:
c4-soft:
springaddons: