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

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

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

Algorithm.HMAC256介绍

[英]Creates a new Algorithm instance using HmacSHA256. Tokens specify this as "HS256".
[中]使用HmacSHA256创建新的算法实例。令牌将其指定为“HS256”。

代码示例

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

@Test
public void shouldBeEqualSignatureMethodResults() throws Exception {
  Algorithm algorithm = Algorithm.HMAC256("secret");
  byte[] header = new byte[]{0x00, 0x01, 0x02};
  byte[] payload = new byte[]{0x04, 0x05, 0x06};
  ByteArrayOutputStream bout = new ByteArrayOutputStream();
  bout.write(header);
  bout.write('.');
  bout.write(payload);
  assertThat(algorithm.sign(bout.toByteArray()), is(algorithm.sign(header, payload)));
}

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

@Test
public void shouldValidateExpiresAtIfPresent() throws Exception {
  Clock clock = mock(Clock.class);
  when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE));
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.isvT0Pqx0yjnZk53mUFSeYFJLDs-Ls9IsNAm86gIdZo";
  JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
  DecodedJWT jwt = verification
      .build(clock)
      .verify(token);
  assertThat(jwt, is(notNullValue()));
}

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

@Test
public void shouldValidateNotBeforeIfPresent() throws Exception {
  Clock clock = mock(Clock.class);
  when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE));
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.isvT0Pqx0yjnZk53mUFSeYFJLDs-Ls9IsNAm86gIdZo";
  JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
  DecodedJWT jwt = verification
      .build(clock)
      .verify(token);
  assertThat(jwt, is(notNullValue()));
}

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

@Test
public void shouldValidateIssuedAtIfPresent() throws Exception {
  Clock clock = mock(Clock.class);
  when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE));
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0Nzc1OTJ9.0WJky9eLN7kuxLyZlmbcXRL3Wy8hLoNCEk5CCl2M4lo";
  JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
  DecodedJWT jwt = verification
      .build(clock)
      .verify(token);
  assertThat(jwt, is(notNullValue()));
}

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

@Test
public void shouldSetCorrectTypeInTheHeader() throws Exception {
  String signed = JWTCreator.init()
      .sign(Algorithm.HMAC256("secret"));
  assertThat(signed, is(notNullValue()));
  String[] parts = signed.split("\\.");
  String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
}

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

@Test
public void shouldSetCorrectAlgorithmInTheHeader() throws Exception {
  String signed = JWTCreator.init()
      .sign(Algorithm.HMAC256("secret"));
  assertThat(signed, is(notNullValue()));
  String[] parts = signed.split("\\.");
  String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
  assertThat(headerJson, JsonMatcher.hasEntry("alg", "HS256"));
}

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

@Test
public void shouldValidateNotBeforeWithLeeway() throws Exception {
  Clock clock = mock(Clock.class);
  when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0Nzc1OTJ9.wq4ZmnSF2VOxcQBxPLfeh1J2Ozy1Tj5iUaERm3FKaw8";
  JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"))
      .acceptNotBefore(2);
  DecodedJWT jwt = verification
      .build(clock)
      .verify(token);
  assertThat(jwt, is(notNullValue()));
}

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

@Test
public void shouldAcceptCustomClaimOfTypeLong() throws Exception {
  String jwt = JWTCreator.init()
      .withClaim("name", Long.MAX_VALUE)
      .sign(Algorithm.HMAC256("secret"));
  assertThat(jwt, is(notNullValue()));
  String[] parts = jwt.split("\\.");
  assertThat(parts[1], is("eyJuYW1lIjo5MjIzMzcyMDM2ODU0Nzc1ODA3fQ"));
}

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

@Test
public void shouldAddExpiresAt() throws Exception {
  String signed = JWTCreator.init()
      .withExpiresAt(new Date(1477592000))
      .sign(Algorithm.HMAC256("secret"));
  assertThat(signed, is(notNullValue()));
  assertThat(TokenUtils.splitToken(signed)[1], is("eyJleHAiOjE0Nzc1OTJ9"));
}

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

@Test
public void shouldThrowOnInvalidNotBeforeIfPresent() throws Exception {
  exception.expect(InvalidClaimException.class);
  exception.expectMessage(startsWith("The Token can't be used before"));
  Clock clock = mock(Clock.class);
  when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0Nzc1OTJ9.wq4ZmnSF2VOxcQBxPLfeh1J2Ozy1Tj5iUaERm3FKaw8";
  JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
  verification
      .build(clock)
      .verify(token);
}

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

@Test
public void shouldGetSubject() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJUb2szbnMifQ.RudAxkslimoOY3BLl2Ghny3BrUKu9I1ZrXzCZGDJtNs";
  DecodedJWT jwt = JWT.require(Algorithm.HMAC256("secret"))
      .build()
      .verify(token);
  assertThat(jwt, is(notNullValue()));
  assertThat(jwt.getSubject(), is("Tok3ns"));
}

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

@Test
public void shouldThrowOnInvalidIssuedAtIfPresent() throws Exception {
  exception.expect(InvalidClaimException.class);
  exception.expectMessage(startsWith("The Token can't be used before"));
  Clock clock = mock(Clock.class);
  when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0Nzc1OTJ9.0WJky9eLN7kuxLyZlmbcXRL3Wy8hLoNCEk5CCl2M4lo";
  JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
  verification
      .build(clock)
      .verify(token);
}

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

@Test
public void shouldGetAlgorithm() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ";
  DecodedJWT jwt = JWT.require(Algorithm.HMAC256("secret"))
      .build()
      .verify(token);
  assertThat(jwt, is(notNullValue()));
  assertThat(jwt.getAlgorithm(), is("HS256"));
}

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

@Test
public void shouldGetKeyId() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiIsImtpZCI6ImtleSJ9.e30.von1Vt9tq9cn5ZYdX1f4cf2EE7fUvb5BCBlKOTm9YWs";
  DecodedJWT jwt = JWT.require(Algorithm.HMAC256("secret"))
      .build()
      .verify(token);
  assertThat(jwt, is(notNullValue()));
  assertThat(jwt.getKeyId(), is("key"));
}

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

@Test
public void shouldAddJWTId() throws Exception {
  String signed = JWTCreator.init()
      .withJWTId("jwt_id_123")
      .sign(Algorithm.HMAC256("secret"));
  assertThat(signed, is(notNullValue()));
  assertThat(TokenUtils.splitToken(signed)[1], is("eyJqdGkiOiJqd3RfaWRfMTIzIn0"));
}

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

@Test
public void shouldValidateCustomClaimOfTypeBoolean() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjp0cnVlfQ.FwQ8VfsZNRqBa9PXMinSIQplfLU4-rkCLfIlTLg_MV0";
  DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
      .withClaim("name", true)
      .build()
      .verify(token);
  assertThat(jwt, is(notNullValue()));
}

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

@Test
public void shouldValidateSubject() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.Rq8IxqeX7eA6GgYxlcHdPFVRNFFZc5rEI3MQTZZbK3I";
  DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
      .withSubject("1234567890")
      .build()
      .verify(token);
  assertThat(jwt, is(notNullValue()));
}

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

@Test
public void shouldValidateJWTId() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJqd3RfaWRfMTIzIn0.0kegfXUvwOYioP8PDaLMY1IlV8HOAzSVz3EGL7-jWF4";
  DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret"))
      .withJWTId("jwt_id_123")
      .build()
      .verify(token);
  assertThat(jwt, is(notNullValue()));
}

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

@Test
public void shouldGetArrayAudience() throws Exception {
  String token = "eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOlsiSG9wZSIsIlRyYXZpcyIsIlNvbG9tb24iXX0.Tm4W8WnfPjlmHSmKFakdij0on2rWPETpoM7Sh0u6-S4";
  DecodedJWT jwt = JWT.require(Algorithm.HMAC256("secret"))
      .build()
      .verify(token);
  assertThat(jwt, is(notNullValue()));
  assertThat(jwt.getAudience(), is(IsCollectionWithSize.hasSize(3)));
  assertThat(jwt.getAudience(), is(IsCollectionContaining.hasItems("Hope", "Travis", "Solomon")));
}

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

@Test
public void shouldThrowOnInvalidCustomClaimValueOfTypeBoolean() throws Exception {
  exception.expect(InvalidClaimException.class);
  exception.expectMessage("The Claim 'name' value doesn't match the required one.");
  String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjpbInNvbWV0aGluZyJdfQ.3ENLez6tU_fG0SVFrGmISltZPiXLSHaz_dyn-XFTEGQ";
  JWTVerifier.init(Algorithm.HMAC256("secret"))
      .withClaim("name", true)
      .build()
      .verify(token);
}

相关文章