title: How to get username from JWT token while requesting for another service after authentication?
body: <p>I am developing a spring boot application having front end with angular6 and using Eureka for service registry and zuul for authentication gateway, both as separate service. I am using jwt for authentication.</p>
  
  <p>When I hit the url localhost:8080/dis/academics/complaint/getMyComplaints with authorization header having value jwt token, then it goes to gateway service and then gateway forwards this to academics service after valid authentication at gateway.</p>
  
  <p>Now I want to get username from token in academics service. How can I get it?</p>
  
  <p>Controller in Gateway Service</p>
  
  <pre><code>@RestController
  @RequestMapping("/dis")
  public class AuthRestAPIs {
  
  @PostMapping("/signin")
  public ResponseEntity&lt;?&gt; authenticateUser(@Valid @RequestBody LoginForm loginRequest) {
  
      Authentication authentication = authenticationManager.authenticate(
              new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));
  
      SecurityContextHolder.getContext().setAuthentication(authentication);
  
      String jwt = jwtProvider.generateJwtToken(authentication);
      //UserDetails userDetails = (UserDetails) authentication.getPrincipal();
      UserPrinciple userPrincipal = (UserPrinciple) authentication.getPrincipal();
      return ResponseEntity.ok(new JwtResponse(jwt, userPrincipal.getUsername(), userPrincipal.getUserType(), userPrincipal.getAuthorities()));
  }
  } 
  </code></pre>
  
  <p>Controller in Academics Service</p>
  
  <pre><code>@RestController
  @RequestMapping("/complaint") 
  public class ComplaintsController {
  @RequestMapping(value = "/getMyComplaints", method = RequestMethod.GET)
  public &lt;T, U&gt; Object[]  getMyComplaints(Authentication authentication)
  {
      String username = authentication.getName();
  
      //System.out.println(username);
  
      String user_type = "student";
  
      List&lt;Object&gt; complaints = new ArrayList&lt;&gt;();
  
      if(user_type.equals("student"))
      {
          Collections.addAll(complaints, cleanlinessComplaintRepository.findByCreatedBy(username));
          Collections.addAll(complaints, leComplaintRepository.findByCreatedBy(username));
          Collections.addAll(complaints, facultyComplaintRepository.findByCreatedBy(username));
          Collections.addAll(complaints, otherComplaintRepository.findByCreatedBy(username));
      }
  
      return complaints.toArray();
  }
  }
  </code></pre>
  
  <p>I have tried using Authentication class but getting null value. How can I achieve this?</p>