티스토리 뷰

Development/JAVA

[JAVA] JWT decode

쥬리리리 2021. 11. 10. 10:48

애플 로그인 진행 중 JWT 복호화 작업을 진행하게 되었다.

 

애플 로그인 성공 시 JWT를 리턴해주고, payload 쪽을 디코딩하여 사용한다. 

 

 

@RequestMapping(value="{pageId:^appleLoginCallback$}")
public String appleLoginCallback(@RequestBody String data, HttpServletRequest request, Model model) throws Exception {

    Map<String, Object> map = new HashMap<String, Object>();
    String[] params = data.split("&");

    for(String param : params){
        String name = param.split("=")[0];
        String value = param.split("=")[1];
        map.put(name, value);
    }

    String token = MapUtils.getString(map, "id_token");
    String[] check = token.split("\\.");
    Base64.Decoder decoder = Base64.getDecoder();
    String payload = new String(decoder.decode(check[1]));

    ObjectMapper mapper = new ObjectMapper();
    Map<String, Object> returnMap = mapper.readValue(payload, Map.class);
	
    return "/login";
}

애플에서 리턴된 id_token값을 복호화 작업 후 map에 담아주는 소스다.

 

 

{
  "iss": "https://appleid.apple.com",
  "aud": "",
  "exp": 1636531005,
  "iat": 1636444605,
  "sub": "",
  "c_hash": "",
  "email": "",
  "email_verified": "true",
  "is_private_email": "true",
  "auth_time": 1636444605,
  "nonce_supported": true
}

--> payload 복호화 된 값

'Development > JAVA' 카테고리의 다른 글

[JAVA] json to int  (0) 2022.01.05
[JAVA] spring boot + security 애플 로그인 구현  (0) 2021.11.12
[JAVA] Http POST urlencoded  (0) 2021.08.24
[JAVA] Bearer token Authorization  (0) 2021.08.22
[JAVA] URL 한글 인코딩  (0) 2021.08.20
댓글
링크
최근에 올라온 글
Total
Today
Yesterday