Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中解密和重新验证JWT令牌_Java_Azure_Jwt_Token - Fatal编程技术网

在Java中解密和重新验证JWT令牌

在Java中解密和重新验证JWT令牌,java,azure,jwt,token,Java,Azure,Jwt,Token,我是Azure、JWT、Microsoft标识平台的新手 我需要解密JWT令牌,并验证该令牌是从Azure生成的,而不是在我的普通旧Java代码中手动创建的 我浏览了微软的文档,但没有一篇是关于技术实现的。我真的非常需要帮助 提前感谢。如果您想验证Azure AD令牌,如果您想验证Azure AD访问令牌,我们可以尝试使用sdkjava jwt和jwks rsa来实现它 比如说 安装sdk com.auth0 java jwt 3.10.3 com.auth0 jwks rsa 0.11.0

我是Azure、JWT、Microsoft标识平台的新手

我需要解密JWT令牌,并验证该令牌是从Azure生成的,而不是在我的普通旧Java代码中手动创建的

我浏览了微软的文档,但没有一篇是关于技术实现的。我真的非常需要帮助


提前感谢。

如果您想验证Azure AD令牌,如果您想验证Azure AD访问令牌,我们可以尝试使用sdk
java jwt
jwks rsa
来实现它

比如说

  • 安装sdk
  • 
    com.auth0
    java jwt
    3.10.3
    com.auth0
    jwks rsa
    0.11.0
    
  • 代码
  • //验证签名
    字符串标记=”;
    DecodedJWT jwt=jwt.decode(令牌);
    System.out.println(jwt.getKeyId());
    JwkProvider=null;
    Jwk Jwk=null;
    算法=空;
    试一试{
    提供者=新的UrlJwkProvider(新的URL(“https://login.microsoftonline.com/common/discovery/v2.0/keys"));
    jwk=provider.get(jwt.getKeyId());
    algorithm=algorithm.RSA256((RSAPublicKey)jwk.getPublicKey(),null);
    algorithm.verify(jwt);//如果令牌签名无效,该方法将抛出SignatureReferenceException
    }捕获(格式错误){
    e、 printStackTrace();
    }捕获(JWKE异常){
    e、 printStackTrace();
    }捕获(签名引用异常e){
    System.out.println(e.getMessage());
    }
    //得到索赔
    字符串标记=”;
    DecodedJWT jwt=jwt.decode(令牌);
    Map claims=jwt.getClaims()//关键是索赔名称
    索赔=索赔。获取(“”);
    
    @jps-Azure是进行令牌解密还是进行OpenID?
    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>java-jwt</artifactId>
        <version>3.10.3</version>
    </dependency>
    <dependency>
        <groupId>com.auth0</groupId>
        <artifactId>jwks-rsa</artifactId>
        <version>0.11.0</version>
    </dependency>
    
    // validate signature 
    String token="<your AD token>";
      DecodedJWT jwt = JWT.decode(token);
      System.out.println(jwt.getKeyId());
    
      JwkProvider provider = null;
      Jwk jwk =null;
      Algorithm algorithm=null;
    
      try {
          provider = new UrlJwkProvider(new URL("https://login.microsoftonline.com/common/discovery/v2.0/keys"));
          jwk = provider.get(jwt.getKeyId());
          algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
          algorithm.verify(jwt);// if the token signature is invalid, the method will throw SignatureVerificationException
      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (JwkException e) {
          e.printStackTrace();
      }catch(SignatureVerificationException e){
    
         System.out.println(e.getMessage());
    
      }
    
    // get claims
    String token="<your AD token>";
    DecodedJWT jwt = JWT.decode(token);
    Map<String, Claim> claims = jwt.getClaims();    //Key is the Claim name
    Claim claim = claims.get("<>");