【问题标题】:Get Payload JWT获取负载 JWT
【发布时间】:2021-08-26 21:32:27
【问题描述】:

我使用 springboot 并使用 oath2 (JWT)。 我的令牌是下一个

token

我想从控制器或过滤器中获取有效负载,但我不知道如何。

我尝试了这个,但它不起作用

@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") );
    }  
    
    

【问题讨论】:

标签: java spring-boot oauth-2.0 controller jwt


【解决方案1】:

(对不起我的英语) 我可以通过下一个代码获得详细信息:

  1. 获取访问令牌
  2. 解码此令牌
  3. 在地图中添加解码令牌
  4. 在地图中按键获取我需要的详细信息
   @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");

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 2017-05-31
    • 2016-03-12
    • 2018-12-19
    • 2020-09-05
    • 2017-02-11
    • 2021-12-24
    • 2016-05-29
    • 2019-10-29
    相关资源
    最近更新 更多