2017-06-20 108 views
0

我試圖解析令牌是這樣的:的oauth2 - 解析令牌總是拋出異常

 Claims claims1 = Jwts.parser() 
        .setSigningKey(publicKey) 
        .parseClaimsJws(token); 

但我總是得到這樣的例外:

java.lang.IllegalArgumentException: Key bytes cannot be specified for RSA signatures. Please specify a PublicKey or PrivateKey instance. 

然而,當我嘗試同樣的道理用我的公衆和私鑰https://jwt.io/,它被驗證。兩個問題:

  1. 是否需要指定私鑰?如果是這樣,我怎麼能在我的代碼中這樣做?

  2. 事實上,現在我不關心簽名驗證 - 當然在將來。目前,我會很高興回答如何解析令牌而不驗證它。

回答

0

我假設你的publicKey是Base64編碼的字符串。 在這種情況下,您需要對其進行解碼並創建sun.security.rsa.RSAPublicKeyImpl的實例:

byte[] decode = TextCodec.BASE64.decode(publicKey); 
Key key = new RSAPublicKeyImpl(decode); 
Claims claims1 = Jwts.parser() 
       .setSigningKey(key) 
       .parseClaimsJws(token);