2017-05-19 7 views
0

私はAzureからのJWTトークンを検証し、JJWTを使用しています。私は、それぞれのフィールドに対応するキー文書からモジュラスと指数を取り出します。フィールドはそれぞれ nとeです。 JWT署名がローカルで計算された署名と一致しません。 JWTの妥当性は主張することができず、信頼されるべきではありません。RSAPublicKeyを生成してJJWT RSAトークンの検証に入力する方法

これはコードです。誰かが私が作った間違いを見ますか?コードは、署名の不一致エラーをスローする検証まですべて正常に実行されます。

private Claims extractClaimsForRsaSignedJwts(String token, String mod, String exp) { 
    Claims claims = null; 
    byte[] modBytes = Base64.decodeBase64(mod.getBytes()); 
    byte[] expBytes = Base64.decodeBase64(exp.getBytes()); 
    BigInteger modulus = new BigInteger(modBytes); 
    BigInteger exponent = new BigInteger(expBytes); 
    RSAPublicKeySpec pubKeySpecification = new RSAPublicKeySpec(modulus, exponent); 
    KeyFactory keyFac = null; 
    try { 
     keyFac = KeyFactory.getInstance("RSA"); 
    } catch (NoSuchAlgorithmException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    RSAPublicKey rsaPub = null; 
    try { 
     rsaPub = (RSAPublicKey) keyFac.generatePublic(pubKeySpecification); 
    } catch (Exception e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    JwtParser jwtParser = Jwts.parser().setSigningKey(rsaPub); 

    try { 
     claims = jwtParser.parseClaimsJws(token).getBody(); 
    } catch (Exception e) { 
     // JWT signature does not match locally computed signature. JWT validity cannot be asserted and should not be trusted. 
     System.out.println("The RSA JWT key validation failed: " + e.getMessage()); 
    } 

    return claims; 
} 

ありがとうございます!

1月

答えて

0

問題が見つかりました。 BigIntegerは、正の数に対してsignum 1で構築する必要があります。 AzureAD JWTの署名検証のためのコードのように動作します。これが最終コードです。 Screen Shot Of Code With Correction

関連する問題