com.auth0.jwt.algorithms.Algorithm.verify()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.8k)|赞(0)|评价(0)|浏览(1226)

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

Algorithm.verify介绍

[英]Verify the given token using this Algorithm instance.
[中]使用此算法实例验证给定令牌。

代码示例

代码示例来源: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 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 shouldPassRSA384VerificationWithProvidedPublicKey() 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 = "eyJhbGciOiJSUzM4NCIsImtpZCI6Im15LWtleS1pZCJ9.eyJpc3MiOiJhdXRoMCJ9.ITNTVCT7ercumZKHV4-BXGkJwwa7fyF3CnSfEvm09fDFSkaseDxNo_75WLDmK9WM8RMHTPvkpHcTKm4guYEbC_la7RzFIKpU72bppzQojggSmWWXt_6zq50QP2t5HFMebote1zxhp8ccEdSCX5pyY6J2sm9kJ__HKK32KxIVCTjVCz-bFBS60oG35aYEySdKsxuUdWbD5FQ9I16Ony2x0EPvmlL3GPiAPmgjSFp3LtcBIbCDaoonM7iuDRGIQiDN_n2FKKb1Bt4_38uWPtTkwRpNalt6l53Y3JDdzGI5fMrMo3RQnQlAJxUJKD0eL6dRAA645IVIIXucHwuhgGGIVw";
  Algorithm algorithm = Algorithm.RSA384(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 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 shouldDoRSA384SigningWithBothKeys() throws Exception {
  Algorithm algorithm = Algorithm.RSA384((RSAPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"), (RSAPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
  String jwt = asJWT(algorithm, RS384Header, auth0IssPayload);
  String expectedSignature = "Jx1PaTBnjd_U56MNjifFcY7w9ImDbseg0y8Ijr2pSiA1_wzQb_wy9undaWfzR5YqdIAXvjS8AGuZUAzIoTG4KMgOgdVyYDz3l2jzj6wI-lgqfR5hTy1w1ruMUQ4_wobpdxAiJ4fEbg8Mi_GljOiCO-P1HilxKnpiOJZidR8MQGwTInsf71tOUkK4x5UsdmUueuZbaU-CL5kPnRfXmJj9CcdxZbD9oMlbo23dwkP5BNMrS2LwGGzc9C_-ypxrBIOVilG3WZxcSmuG86LjcZbnL6LBEfph5NmKBgQav147uipb_7umBEr1m2dYiB_9u606n3bcoo3rnsYYK_Xfi1GAEQ";
  assertSignaturePresent(jwt);
  assertSignatureValue(jwt, expectedSignature);
  algorithm.verify(JWT.decode(jwt));
}

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

@Test
public void shouldDoRSA512SigningWithBothKeys() throws Exception {
  Algorithm algorithm = Algorithm.RSA512((RSAPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"), (RSAPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
  String jwt = asJWT(algorithm, RS512Header, auth0IssPayload);
  String expectedSignature = "THIPVYzNZ1Yo_dm0k1UELqV0txs3SzyMopCyHcLXOOdgYXF4MlGvBqu0CFvgSga72Sp5LpuC1Oesj40v_QDsp2GTGDeWnvvcv_eo-b0LPSpmT2h1Ibrmu-z70u2rKf28pkN-AJiMFqi8sit2kMIp1bwIVOovPvMTQKGFmova4Xwb3G526y_PeLlflW1h69hQTIVcI67ACEkAC-byjDnnYIklA-B4GWcggEoFwQRTdRjAUpifA6HOlvnBbZZlUd6KXwEydxVS-eh1odwPjB2_sfbyy5HnLsvNdaniiZQwX7QbwLNT4F72LctYdHHM1QCrID6bgfgYp9Ij9CRX__XDEA";
  assertSignaturePresent(jwt);
  assertSignatureValue(jwt, expectedSignature);
  algorithm.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));
}

相关文章