com.auth0.jwt.JWT.decode()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(644)

本文整理了Java中com.auth0.jwt.JWT.decode()方法的一些代码示例,展示了JWT.decode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JWT.decode()方法的具体详情如下:
包路径:com.auth0.jwt.JWT
类名称:JWT
方法名:decode

JWT.decode介绍

[英]Decode a given Json Web Token.

Note that this method doesn't verify the token's signature! Use it only if you trust the token or you already verified it.
[中]解码给定的Json Web令牌。
请注意,此方法不会验证令牌的签名!仅当您信任令牌或已验证令牌时才使用它。

代码示例

代码示例来源:origin: auth0/java-jwt

@Test
public void getSubject() throws Exception {
  DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ");
  assertThat(jwt.getSubject(), is(notNullValue()));
  assertThat(jwt.getSubject(), is("1234567890"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldGetStringToken() throws Exception {
  DecodedJWT jwt = JWT.decode("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ");
  assertThat(jwt, is(notNullValue()));
  assertThat(jwt.getToken(), is(notNullValue()));
  assertThat(jwt.getToken(), is("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassRSA256VerificationWithProvidedPublicKey() throws Exception {
  RSAKeyProvider provider = mock(RSAKeyProvider.class);
  PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA");
  when(provider.getPublicKeyById("my-key-id")).thenReturn((RSAPublicKey) publicKey);
  String jwt = "eyJhbGciOiJSUzI1NiIsImtpZCI6Im15LWtleS1pZCJ9.eyJpc3MiOiJhdXRoMCJ9.jXrbue3xJmnzWH9kU-uGeCTtgbQEKbch8uHd4Z52t86ncNyepfusl_bsyLJIcxMwK7odRzKiSE9efV9JaRSEDODDBdMeCzODFx82uBM7e46T1NLVSmjYIM7Hcfh81ZeTIk-hITvgtL6hvTdeJWOCZAB0bs18qSVW5SvursRUhY38xnhuNI6HOHCtqp7etxWAu6670L53I3GtXsmi6bXIzv_0v1xZcAFg4HTvXxfhfj3oCqkSs2nC27mHxBmQtmZKWmXk5HzVUyPRwTUWx5wHPT_hCsGer-CMCAyGsmOg466y1KDqf7ogpMYojfVZGWBsyA39LO1oWZ4Ryomkn8t5Vg";
  Algorithm algorithm = Algorithm.RSA256(provider);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA512SigningWithProvidedPrivateKey() throws Exception {
  ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
  PrivateKey privateKey = readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC");
  PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC");
  when(provider.getPrivateKey()).thenReturn((ECPrivateKey) privateKey);
  when(provider.getPublicKeyById(null)).thenReturn((ECPublicKey) publicKey);
  Algorithm algorithm = Algorithm.ECDSA512(provider);
  String jwt = asJWT(algorithm, ES512Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldGetCustomArrayClaimOfTypeString() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjpbInRleHQiLCIxMjMiLCJ0cnVlIl19.lxM8EcmK1uSZRAPd0HUhXGZJdauRmZmLjoeqz4J9yAA";
  DecodedJWT jwt = JWT.decode(token);
  Assert.assertThat(jwt, is(notNullValue()));
  Assert.assertThat(jwt.getClaim("name").asArray(String.class), arrayContaining("text", "123", "true"));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassHMAC384Verification() throws Exception {
  String jwt = "eyJhbGciOiJIUzM4NCIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.uztpK_wUMYJhrRv8SV-1LU4aPnwl-EM1q-wJnqgyb5DHoDteP6lN_gE1xnZJH5vw";
  Algorithm algorithmString = Algorithm.HMAC384("secret");
  Algorithm algorithmBytes = Algorithm.HMAC384("secret".getBytes(StandardCharsets.UTF_8));
  DecodedJWT decoded = JWT.decode(jwt);
  algorithmString.verify(decoded);
  algorithmBytes.verify(decoded);
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailHMAC512VerificationWithInvalidSecretBytes() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: HmacSHA512");
  String jwt = "eyJhbGciOiJIUzUxMiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.VUo2Z9SWDV-XcOc_Hr6Lff3vl7L9e5Vb8ThXpmGDFjHxe3Dr1ZBmUChYF-xVA7cAdX1P_D4ZCUcsv3IefpVaJw";
  Algorithm algorithm = Algorithm.HMAC512("not_real_secret".getBytes(StandardCharsets.UTF_8));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailECDSA256VerificationWhenProvidedPublicKeyIsNull() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
  exception.expectCause(isA(IllegalStateException.class));
  exception.expectCause(hasMessage(is("The given Public Key is null.")));
  ECDSAKeyProvider provider = mock(ECDSAKeyProvider.class);
  when(provider.getPublicKeyById("my-key-id")).thenReturn(null);
  String jwt = "eyJhbGciOiJFUzI1NiIsImtpZCI6Im15LWtleS1pZCJ9.eyJpc3MiOiJhdXRoMCJ9.D_oU4CB0ZEsxHOjcWnmS3ZJvlTzm6WcGFx-HASxnvcB2Xu2WjI-axqXH9xKq45aPBDs330JpRhJmqBSc2K8MXQ";
  Algorithm algorithm = Algorithm.ECDSA256(provider);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoHMAC512SigningWithBytes() throws Exception {
  Algorithm algorithm = Algorithm.HMAC512("secret".getBytes(StandardCharsets.UTF_8));
  String jwt = asJWT(algorithm, HS512Header, auth0IssPayload);
  String expectedSignature = "OXWyxmf-VcVo8viOiTFfLaEy6mrQqLEos5R82Xsx8mtFxQadJAQ1aVniIWN8qT2GNE_pMQPcdzk4x7Cqxsp1dw";
  assertSignaturePresent(jwt);
  assertSignatureValue(jwt, expectedSignature);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassECDSA384VerificationWithJOSESignatureWithBothKeys() throws Exception {
  String jwt = "eyJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJhdXRoMCJ9.50UU5VKNdF1wfykY8jQBKpvuHZoe6IZBJm5NvoB8bR-hnRg6ti-CHbmvoRtlLfnHfwITa_8cJMy6TenMC2g63GQHytc8rYoXqbwtS4R0Ko_AXbLFUmfxnGnMC6v4MS_z";
  Algorithm algorithm = Algorithm.ECDSA384((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_384, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_384, "EC"));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldPassECDSA512VerificationWithJOSESignatureWithBothKeys() throws Exception {
  String jwt = "eyJhbGciOiJFUzUxMiJ9.eyJpc3MiOiJhdXRoMCJ9.AeCJPDIsSHhwRSGZCY6rspi8zekOw0K9qYMNridP1Fu9uhrA1QrG-EUxXlE06yvmh2R7Rz0aE7kxBwrnq8L8aOBCAYAsqhzPeUvyp8fXjjgs0Eto5I0mndE2QHlgcMSFASyjHbU8wD2Rq7ZNzGQ5b2MZfpv030WGUajT-aZYWFUJHVg2";
  Algorithm algorithm = Algorithm.ECDSA512((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoHMAC256SigningWithString() throws Exception {
  Algorithm algorithm = Algorithm.HMAC256("secret");
  String jwt = asJWT(algorithm, HS256Header, auth0IssPayload);
  String expectedSignature = "s69x7Mmu4JqwmdxiK6sesALO7tcedbFsKEEITUxw9ho";
  assertSignaturePresent(jwt);
  assertSignatureValue(jwt, expectedSignature);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailRSA512VerificationWithInvalidPublicKey() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA512withRSA");
  String jwt = "eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mvL5LoMyIrWYjk5umEXZTmbyIrkbbcVPUkvdGZbu0qFBxGOf0nXP5PZBvPcOu084lvpwVox5n3VaD4iqzW-PsJyvKFgi5TnwmsbKchAp7JexQEsQOnTSGcfRqeUUiBZqRQdYsho71oAB3T4FnalDdFEpM-fztcZY9XqKyayqZLreTeBjqJm4jfOWH7KfGBHgZExQhe96NLq1UA9eUyQwdOA1Z0SgXe4Ja5PxZ6Fm37KnVDtDlNnY4JAAGFo6y74aGNnp_BKgpaVJCGFu1f1S5xCQ1HSvs8ZSdVWs5NgawW3wRd0kRt_GJ_Y3mIwiF4qUyHWGtsSHu_qjVdCTtbFyow";
  Algorithm algorithm = Algorithm.RSA512((RSAKey) readPublicKeyFromFile(INVALID_PUBLIC_KEY_FILE, "RSA"));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailECDSA256VerificationWithInvalidPublicKey() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
  String jwt = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.W9qfN1b80B9hnMo49WL8THrOsf1vEjOhapeFemPMGySzxTcgfyudS5esgeBTO908X5SLdAr5jMwPUPBs9b6nNg";
  Algorithm algorithm = Algorithm.ECDSA256((ECKey) readPublicKeyFromFile(INVALID_PUBLIC_KEY_FILE_256, "EC"));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldThrowOnECDSA384VerificationWithDERSignature() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA384withECDSA");
  exception.expectCause(isA(SignatureException.class));
  exception.expectCause(hasMessage(is("Invalid JOSE signature format.")));
  String jwt = "eyJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJhdXRoMCJ9.MGUCMQDnRRTlUo10XXB/KRjyNAEqm+4dmh7ohkEmbk2+gHxtH6GdGDq2L4Idua+hG2Ut+ccCMH8CE2v/HCTMuk3pzAtoOtxkB8rXPK2KF6m8LUuEdCqPwF2yxVJn8ZxpzAur+DEv8w==";
  ECKey key = (ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_384, "EC");
  Algorithm algorithm = Algorithm.ECDSA384(key);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldFailECDSA512VerificationWhenUsingPrivateKey() throws Exception {
  exception.expect(SignatureVerificationException.class);
  exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA512withECDSA");
  exception.expectCause(isA(IllegalStateException.class));
  exception.expectCause(hasMessage(is("The given Public Key is null.")));
  String jwt = "eyJhbGciOiJFUzUxMiJ9.eyJpc3MiOiJhdXRoMCJ9.AZgdopFFsN0amCSs2kOucXdpylD31DEm5ChK1PG0_gq5Mf47MrvVph8zHSVuvcrXzcE1U3VxeCg89mYW1H33Y-8iAF0QFkdfTUQIWKNObH543WNMYYssv3OtOj0znPv8atDbaF8DMYAtcT1qdmaSJRhx-egRE9HGZkinPh9CfLLLt58X";
  Algorithm algorithm = Algorithm.ECDSA512((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA256SigningWithBothKeys() throws Exception {
  Algorithm algorithm = Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  String jwt = asJWT(algorithm, ES256Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithm.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA512Signing() throws Exception {
  Algorithm algorithmSign = Algorithm.ECDSA512((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
  Algorithm algorithmVerify = Algorithm.ECDSA512((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"));
  
  String jwt = asJWT(algorithmSign, ES512Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithmVerify.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA256Signing() throws Exception {
  Algorithm algorithmSign = Algorithm.ECDSA256((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
  Algorithm algorithmVerify = Algorithm.ECDSA256((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"));
  String jwt = asJWT(algorithmSign, ES256Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithmVerify.verify(JWT.decode(jwt));
}

代码示例来源:origin: auth0/java-jwt

@Test
public void shouldDoECDSA512Signing() throws Exception {
  Algorithm algorithmSign = Algorithm.ECDSA512((ECKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
  Algorithm algorithmVerify = Algorithm.ECDSA512((ECKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"));
  
  String jwt = asJWT(algorithmSign, ES512Header, auth0IssPayload);
  assertSignaturePresent(jwt);
  algorithmVerify.verify(JWT.decode(jwt));
}

相关文章