JWT 문자열이 있습니다. 나는 "decode JWT"에 대한 최고의 Google 검색 결과 중 하나에 붙여 넣을 수 있으며 올바르게 디코딩하고 이에 대한 비밀 또는 암호화 문구가 필요하지 않습니다. 하지만 내가 본 모든 예는 버디 또는 jwtt, 내가 그들이 암호화 된 비밀을 제공해야합니다. 어떻게하면 쉽게 원시 문자열을 디코딩 할 수 있을까요?Clojure/Java의 JWT 디코드
0
A
답변
0
아래의 답변은 유익하고 양호합니다.
(ns decode-jwt
(:require [cheshire.core :as json])
(:import [org.apache.commons.codec.binary Base64]))
(-> returned-jwt ;; returned-jwt is your full jwt string
(clojure.string/split #"\.") ;; split into the 3 parts of a jwt, header, body, signature
second ;; get the body
Base64/decodeBase64 ;; read it into a byte array
String. ;; byte array to string
json/decode ;; make it into a sensible clojure map
)
1
JWT를 디코딩하는 것은 매우 간단하지만 서명을 확인하기 전까지는 클레임을 신뢰해서는 안됩니다. 그것이 적절한 JWT 라이브러리가 비밀을 제공 할 것을 요구하는 이유입니다. 또는 RSA를 사용하여 서명 된 경우 공개 키를 제공합니다.
<header> . <payload> . <signature>
각 부분을 base64 인코딩이다 .
의해 분리 된 세 부분 -
어쨌든 JWT의 형식은 매우 간단하다. 소유권 주장을보고 싶은 경우 다음과 같이 할 수 있습니다.
String jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
String [] jwtParts = jwt.split("\\.");
if (jwtParts.length == 3) {
String payload = new String(Base64.getDecoder().decode(jwtParts[1]));
System.out.println(payload);
// Now you can use some JSON library to decode payload.
}
1
https://jwt.io/
Clojure의 JWT 라이브러리에 : [funcool/buddy], doc:
http://funcool.github.io/buddy-sign/latest/`
user=> (require '[buddy.sign.jwt :as jwt])
user=> (jwt/sign {:userid 1} "secret")
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjF9.5sxiy9q0YcpnXl2jBl-s4--C9iq5-4qC6CrW30NfRS4"
user=> (jwt/unsign "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOjF9.5sxiy9q0YcpnXl2jBl-s4--C9iq5-4qC6CrW30NfRS4" "secret")
{:userid 1}
user=>
여기에 나를 위해 일한 Clojure의 버전입니다