我正在嘗試用Java驗證Azure AD中的簽名:
而如果我試圖驗證一個令牌,我就會收到以下錯誤:
當使用以下演算法驗證時,令牌的簽名結果是無效的
演算法。SHA256withRSA 應用下面的方法: 用MS ADFS提供的令牌測驗相同的方法,我能夠驗證,但用Azure AD,我不能。如何驗證令牌以避免這個錯誤?如何為Azure AD調整代碼或增加對SHA256與RSA的支持?我的代碼有什么問題嗎?
預先感謝 Juan Antonio uj5u.com熱心網友回復: 你正試圖驗證一個用于Microsoft Graph API的訪問令牌。
你不應該這樣做,主要是因為它不是為你的應用程式準備的。
他們使用了一些不同的東西來簽名,你不能使用相同的方法來驗證 Graph API 令牌。
在獲取令牌時,請確保使用由您的應用程式定義的作用域來為您的應用程式接收令牌。
那個你應該能夠驗證。
如果你還需要呼叫 MS Graph API,那么你需要獲得兩個令牌。
標籤:<dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<版本>8.6.6</版本>
</dependency>
<依賴性>
<groupId>com.Auth0</groupId>
<artifactId>java-jwt</artifactId>
<版本>3.18.2</版本>
</dependency>
<依賴性>
<groupId>com.Auth0</groupId>
<artifactId>jwks-rsa</artifactId>
<版本>0.19.0</版本>
</dependency>
<依賴性>
<groupId>com.s fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<版本>2.12.5</版本>
</dependency>
private boolean verifyJWT(String azureDiscoveryKeys, String token) {
嘗試 {
JwkProvider provider = new UrlJwkProvider(new URL(azureDiscoveryKeys))。
DecodedJWT jwt = JWT.decode(token);
Jwk jwk = provider.get(jwt.getKeyId())。
RSAPublicKey publicKey = (RSAPublicKey) jwk.getPublicKey();
Algorithm alg = Algorithm.RSA256(publicKey, null);
JWTVerifier verifier = JWT.require(alg).build()。
verifier.verify(token)。
回傳true。
} catch(JWTVerificationException | JwkException | MalformedURLException ex) {
System.out.println(ex.getMessage())。
回傳false。
}
}
