0

I work with springboot and use oath2 (JWT). My token is the next

token

I want to get the payload from a controller or a filter but i dont know how.

I try this but it doesn't work

@GetMapping("/user")
public String getB(@AuthenticationPrincipal Jwt principal, @RequestHeader("refresh") String refresh) {

    System.out.println(principal.getClaims());;
    
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();        
    Object details = authentication.getDetails();        
    
    
    if ( details instanceof OAuth2AuthenticationDetails ){
        OAuth2AuthenticationDetails oAuth2AuthenticationDetails = (OAuth2AuthenticationDetails)details;
        Map<String, Object> decodedDetails = (Map<String, Object>)oAuth2AuthenticationDetails.getDecodedDetails();

        System.out.println( decodedDetails.get("time_created") );
    }  
    
    
mateo cunha
  • 37
  • 1
  • 9

1 Answers1

0

(SORRY FOR MY ENGLISH) I can get a details with the next code:

  1. get access token
  2. decode this token
  3. add decode token in a map
  4. get detail that i need by key in the map
   @GetMapping("/user")
    public String getB(OAuth2Authentication auth) throws JsonMappingException, JsonProcessingException {

        final OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) auth.getDetails();
        // token
        String accessToken = details.getTokenValue();
        System.out.println(accessToken);

        JsonParser parser = JsonParserFactory.getJsonParser();
        Map<String, ?> tokenData = parser.parseMap(JwtHelper.decode(accessToken).getClaims());
    
        String customField = (String) tokenData.get("time_created");
jps
  • 20,041
  • 15
  • 75
  • 79
mateo cunha
  • 37
  • 1
  • 9