File tree Expand file tree Collapse file tree 1 file changed +7
-11
lines changed
internet-banking-api-gateway/src/main/java/com/javatodev/finance/configuration Expand file tree Collapse file tree 1 file changed +7
-11
lines changed Original file line number Diff line number Diff line change 33import org .springframework .cloud .gateway .filter .GlobalFilter ;
44import org .springframework .context .annotation .Bean ;
55import org .springframework .context .annotation .Configuration ;
6- import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationToken ;
76import reactor .core .publisher .Mono ;
87
8+ import java .security .Principal ;
9+
910@ Configuration
1011public 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}
You can’t perform that action at this time.
0 commit comments