2016-06-08 17 views
5

Ho un jwt pedina come questoCome posso decodificare il token JWT in Android?

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ 

Come posso decodificare questo in modo che posso ottenere il carico utile come questo

{ 
    "sub": "1234567890", 
    "name": "John Doe", 
    "admin": true 
} 

ho usato this biblioteca, ma non riesce a trovare un modo per fare ciò che voglio

risposta

1

ho usato in un'applicazione web Java e il codice sarà simile di seguito: -

Jwts.parser().setSigningKey('secret-key').parseClaimsJws(token).getBody() 

Restituirà le attestazioni che contengono i valori richiesti.

19

si dovrebbe dividere stringa: Se passate le prime due sezioni attraverso un decodificatore base 64, si otterrà il seguente (formattazione aggiunto per chiarezza):

intestazione

{ 
    "alg": "HS256", 
    "typ": "JWT" 
} 

corpo

{ 
    "sub": "1234567890", 
    "name": "John Doe", 
    "admin": true 
} 

esempio di codice:

public class JWTUtils { 

    public static void decoded(String JWTEncoded) throws Exception { 
     try { 
      String[] split = JWTEncoded.split("\\."); 
      Log.d("JWT_DECODED", "Header: " + getJson(split[0])); 
      Log.d("JWT_DECODED", "Body: " + getJson(split[1])); 
     } catch (UnsupportedEncodingException e) { 
      //Error 
     } 
    } 

    private static String getJson(String strEncoded) throws UnsupportedEncodingException{ 
     byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE); 
     return new String(decodedBytes, "UTF-8"); 
    } 
} 
Metodo

chiamata per esempio

JWTUtils.decoded("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"); 

riferimento libreria: prova https://github.com/jwtk/jjwt

JWT: https://jwt.io/

0

ho usato una libreria di terze parti di nome JWTDecode.Android https://github.com/auth0/JWTDecode.Android. La documentazione è abbastanza buona. Dalla tua domanda, il sottotitolo, il nome ecc. Fanno parte del corpo e sono chiamati reclami. Si potrebbe ottenere loro come questo utilizzando la libreria di cui sopra:

JWT parsedJWT = new JWT(jwtToken); 
    Claim subscriptionMetaData = parsedJWT.getClaim("name"); 
    String parsedValue = subscriptionMetaData.asString(); 
0

Questo funziona utilizzando Java 8 della classe Base64:

public String getDecodedJwt(String jwt) 
{ 
    String result = ""; 

    String[] parts = jwt.split("[.]"); 
    try 
    { 
    int index = 0; 
    for(String part: parts) 
    { 
     if (index >= 2) 
     break; 

     index++; 
     byte[] partAsBytes = part.getBytes("UTF-8"); 
     String decodedPart = new String(java.util.Base64.getUrlDecoder().decode(partAsBytes), "UTF-8"); 

     result += decodedPart; 
    } 
    } 
    catch(Exception e) 
    { 
    throw new RuntimeException("Couldnt decode jwt", e); 
    } 

    return result; 
} 
+0

Il problema con questo è che è funziona solo a partire dal 26 API – Thecave3

Problemi correlati