title: Need to get authenticated user name from the OAuth2User in Spring Cloud Gateway routes
body: <p>I am a newbie to Spring Cloud Gateway. I have created a simple SCG application and my use case is to forward the authenticated username to my downstream application via http request header.</p>
  
  <p>This is my SpringBoot class</p>
  
  <pre><code>package com.xyz;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties.Jwt;
  import org.springframework.cloud.gateway.route.RouteLocator;
  import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
  import org.springframework.cloud.security.oauth2.gateway.TokenRelayGatewayFilterFactory;
  import org.springframework.context.annotation.Bean;
  import org.springframework.security.core.annotation.AuthenticationPrincipal;
  import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
  import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
  import org.springframework.security.oauth2.core.user.OAuth2User;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.GetMapping;
  
  @Controller
  @SpringBootApplication
  public class OracleSSOMultiplexer {
  
      @Autowired
      private TokenRelayGatewayFilterFactory filterFactory;
  
  
  
  
      @GetMapping("/")
      public String index(Model model,
                          @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,
                          @AuthenticationPrincipal OAuth2User oauth2User) {
          model.addAttribute("userName", oauth2User.getName());
          model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());
          model.addAttribute("userAttributes", oauth2User.getAttributes());
          return "index";
      }
  
      @Bean
      public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
  
          return builder.routes()
                  //.route("resource", r -&gt; r.path("/resource")
  
                  .route(r -&gt; r.path("/hyperion/**")
                      .filters(f -&gt; f.filters(filterFactory.apply())
                              .addRequestHeader("HYPLOGIN", "scott") // I need to pass the authenticated username here instead of hardcoding it
                      .addResponseHeader("hyp-response", "hyp-response-header-val"))
                  .uri("http://localhost:8081/")
                  .id("hyperionModule"))
                  .build();
      }
  
      public static void main(String[] args) {
          SpringApplication.run(OracleSSOMultiplexer.class, args);
      }
  }
  </code></pre>
  
  <p>and this my application.yml</p>
  
  <pre><code>server:
    port: 8080
  
  logging:
    level:
      root: INFO
      org.springframework.web: INFO
      org.springframework.web.HttpLogging: DEBUG
      org.springframework.security: DEBUG
      org.springframework.security.oauth2: DEBUG
      org.springframework.cloud.gateway: DEBUG
  
  spring:
    autoconfigure:
      # TODO: remove when fixed https://github.com/spring-projects/spring-security/issues/6314
      exclude: org.springframework.boot.actuate.autoconfigure.security.reactive.ReactiveManagementWebSecurityAutoConfiguration
    thymeleaf:
      cache: false
    security:
      oauth2:
        client:
          registration:
            gateway:
              provider: pingfederate
              client-id: oidchyperion
              client-secret: Lms@12345
              authorization-grant-type: authorization_code
              redirect-uri: http://localhost:8080/login/oauth2/code/gateway
              scope: openid
          provider:
            pingfederate:
              authorization-uri: https://xyz:9035/as/authorization.oauth2
              token-uri: https://xyz:9035/as/token.oauth2
              user-info-uri: https://xyz:9035/idp/userinfo.openid
              user-name-attribute: sub
              jwk-set-uri: https://xyz:9035/pf/JWKS
  #  cloud:
  #    gateway:
  #      routes:
  #        - id: resource
  #          uri: http://resource:9000
  #          predicates:
  #            - Path=/resource
  #          filters:
  #            - TokenRelay=
  #            - RemoveRequestHeader=Cookie
  </code></pre>
  
  <p>I am able to perform OAuth Authentication and able to display the authenticated user information using the Model at the index.html. I need now to just pass the logged in user's username (oauth2user.getName()) in the request header but I am not able to figure out. </p>
  
  <p>Any help in this regards is appreciated.</p>
  
  <p>Thanks,
  Thani</p>