summaryrefslogtreecommitdiff
path: root/microservices/02-api-gateway/src/main/java/com
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/java/com
parent504045fb23fa75a520a4d2665aea7d423db1fe14 (diff)
Microservices: Documentation improvedHEADmaster
Diffstat (limited to 'microservices/02-api-gateway/src/main/java/com')
-rw-r--r--microservices/02-api-gateway/src/main/java/com/example/apigateway/config/RoleHeaderRelayFilter.java13
1 files changed, 6 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;
}
}