Skip to content

Commit c77372f

Browse files
gateway configuraiton changes
1 parent fb16a47 commit c77372f

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

internet-banking-api-gateway/src/main/java/com/javatodev/finance/configuration/GatewayConfiguration.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,27 @@
33
import org.springframework.cloud.gateway.filter.GlobalFilter;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
6-
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
76
import reactor.core.publisher.Mono;
87

8+
import java.security.Principal;
9+
910
@Configuration
1011
public class GatewayConfiguration {
1112

12-
//setting auth-id for API requests.
13+
private static final String HTTP_HEADER_AUTH_USER_ID = "X-Auth-Id";
14+
private static final String UNAUTHORIZED_USER_NAME = "SYSTEM USER";
1315

1416
@Bean
1517
public GlobalFilter customGlobalFilter() {
16-
return ((exchange, chain) -> exchange.getPrincipal().map(principal -> {
17-
String userName = "";
18-
19-
if (principal instanceof JwtAuthenticationToken) {
20-
//Get username from Principal
21-
userName = principal.getName();
22-
}
18+
return (exchange, chain) -> exchange.getPrincipal().map(Principal::getName).defaultIfEmpty(UNAUTHORIZED_USER_NAME).map(principal -> {
2319
// adds header to proxied request
2420
exchange.getRequest().mutate()
25-
.header("X-Auth-Id", userName)
21+
.header(HTTP_HEADER_AUTH_USER_ID, principal)
2622
.build();
2723
return exchange;
2824
}).flatMap(chain::filter).then(Mono.fromRunnable(() -> {
2925

30-
})));
26+
}));
3127
}
3228

3329
}

0 commit comments

Comments
 (0)