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

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

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

JWT.create介绍

[英]Returns a Json Web Token builder used to create and sign tokens
[中]返回用于创建和签名令牌的Json Web令牌生成器

代码示例

代码示例来源:origin: knowm/XChange

public QuoineSignatureDigest(
  String tokenID, String userSecret, SynchronizedValueFactory<Long> nonceFactory) {
 this.tokenID = tokenID;
 this.userSecret = userSecret.getBytes();
 this.nonceFactory = nonceFactory;
 this.builder = JWT.create();
}

代码示例来源:origin: google/data-transfer-project

@Override
 public String createNewToken(UUID jobId) {
  try {
   return JWT.create()
     .withIssuer(JWTTokenManager.ISSUER)
     .withClaim(JWTTokenManager.ID_CLAIM_KEY, jobId.toString())
     .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRATION_TIME_MILLIS))
     .sign(algorithm);
  } catch (JWTCreationException e) {
   throw new RuntimeException("Error creating token for: " + jobId);
  }
 }
}

代码示例来源:origin: knowm/XChange

JWTCreator.Builder builder = JWT.create();
builder
  .withClaim("access_key", accessKey)

代码示例来源:origin: line/armeria

@Override
public String newId() {
  final Instant now = Instant.now();
  final int un2 = ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE) & 0x7fffffff;
  return JWT.create()
       .withIssuer(issuer)
       .withIssuedAt(Date.from(now))
       .withExpiresAt(Date.from(now.plus(validSeconds, ChronoUnit.SECONDS)))
       // To make multiple tokens issued in the same second unique, we add uniquifiers.
       .withClaim(CLAIM_NAME_UNIQUIFIER1, un1)
       .withClaim(CLAIM_NAME_UNIQUIFIER2, un2)
       .sign(algorithm);
}

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

@Test
public void shouldCreateAnEmptyHMAC512SignedToken() throws Exception {
  String signed = JWT.create().sign(Algorithm.HMAC512("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", "HS512"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.HMAC512("secret"))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
public void shouldCreateAnEmptyHMAC256SignedToken() throws Exception {
  String signed = JWT.create().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"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.HMAC256("secret"))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
public void shouldCreateAnEmptyHMAC384SignedToken() throws Exception {
  String signed = JWT.create().sign(Algorithm.HMAC384("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", "HS384"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.HMAC384("secret"))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
public void shouldCreateAnEmptyRSA512SignedToken() throws Exception {
  String signed = JWT.create().sign(Algorithm.RSA512((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA")));
  assertThat(signed, is(notNullValue()));
  String[] parts = signed.split("\\.");
  String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
  assertThat(headerJson, JsonMatcher.hasEntry("alg", "RS512"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.RSA512((RSAKey) PemUtils.readPublicKeyFromFile(PUBLIC_KEY_FILE_RSA, "RSA")))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
public void shouldCreateAnEmptyRSA384SignedToken() throws Exception {
  String signed = JWT.create().sign(Algorithm.RSA384((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA")));
  assertThat(signed, is(notNullValue()));
  String[] parts = signed.split("\\.");
  String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
  assertThat(headerJson, JsonMatcher.hasEntry("alg", "RS384"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.RSA384((RSAKey) PemUtils.readPublicKeyFromFile(PUBLIC_KEY_FILE_RSA, "RSA")))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
public void shouldCreateAnEmptyECDSA384SignedToken() throws Exception {
  String signed = JWT.create().sign(Algorithm.ECDSA384((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_384, "EC")));
  assertThat(signed, is(notNullValue()));
  String[] parts = signed.split("\\.");
  String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
  assertThat(headerJson, JsonMatcher.hasEntry("alg", "ES384"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.ECDSA384((ECKey) PemUtils.readPublicKeyFromFile(PUBLIC_KEY_FILE_EC_384, "EC")))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
public void shouldCreateAnEmptyRSA256SignedToken() throws Exception {
  String signed = JWT.create().sign(Algorithm.RSA256((RSAKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_RSA, "RSA")));
  assertThat(signed, is(notNullValue()));
  String[] parts = signed.split("\\.");
  String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
  assertThat(headerJson, JsonMatcher.hasEntry("alg", "RS256"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.RSA256((RSAKey) PemUtils.readPublicKeyFromFile(PUBLIC_KEY_FILE_RSA, "RSA")))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
public void shouldCreateAnEmptyECDSA256SignedToken() throws Exception {
  String signed = JWT.create().sign(Algorithm.ECDSA256((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_256, "EC")));
  assertThat(signed, is(notNullValue()));
  String[] parts = signed.split("\\.");
  String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
  assertThat(headerJson, JsonMatcher.hasEntry("alg", "ES256"));
  assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
  assertThat(parts[1], is("e30"));
  JWTVerifier verified = JWT.require(Algorithm.ECDSA256((ECKey) PemUtils.readPublicKeyFromFile(PUBLIC_KEY_FILE_EC_256, "EC")))
      .build();
  assertThat(verified, is(notNullValue()));
}

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

@Test
  public void shouldCreateAnEmptyECDSA512SignedToken() throws Exception {
    String signed = JWT.create().sign(Algorithm.ECDSA512((ECKey) PemUtils.readPrivateKeyFromFile(PRIVATE_KEY_FILE_EC_512, "EC")));
    assertThat(signed, is(notNullValue()));

    String[] parts = signed.split("\\.");
    String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
    assertThat(headerJson, JsonMatcher.hasEntry("alg", "ES512"));
    assertThat(headerJson, JsonMatcher.hasEntry("typ", "JWT"));
    assertThat(parts[1], is("e30"));

    JWTVerifier verified = JWT.require(Algorithm.ECDSA512((ECKey) PemUtils.readPublicKeyFromFile(PUBLIC_KEY_FILE_EC_512, "EC")))
        .build();
    assertThat(verified, is(notNullValue()));
  }
}

代码示例来源:origin: Smith-Cruise/Spring-Boot-Shiro

/**
   * 生成签名,5min后过期
   * @param username 用户名
   * @param secret 用户的密码
   * @return 加密的token
   */
  public static String sign(String username, String secret) {
    try {
      Date date = new Date(System.currentTimeMillis()+EXPIRE_TIME);
      Algorithm algorithm = Algorithm.HMAC256(secret);
      // 附带username信息
      return JWT.create()
          .withClaim("username", username)
          .withExpiresAt(date)
          .sign(algorithm);
    } catch (UnsupportedEncodingException e) {
      return null;
    }
  }
}

代码示例来源:origin: org.knowm.xchange/xchange-quoine

public QuoineSignatureDigest(
  String tokenID, String userSecret, SynchronizedValueFactory<Long> nonceFactory) {
 this.tokenID = tokenID;
 this.userSecret = userSecret.getBytes();
 this.nonceFactory = nonceFactory;
 this.builder = JWT.create();
}

代码示例来源:origin: yunTerry/spring-cloud-netflix

@GetMapping("/token/generate/{uid}")
String getToken(@PathVariable String uid) {
  String token = JWT.create()
      .withSubject(uid)
      .withExpiresAt(getExdate(3))
      .sign(algorithm);
  return token;
}

代码示例来源:origin: com.linecorp.armeria/armeria-saml

@Override
public String newId() {
  final Instant now = Instant.now();
  final int un2 = ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE) & 0x7fffffff;
  return JWT.create()
       .withIssuer(issuer)
       .withIssuedAt(Date.from(now))
       .withExpiresAt(Date.from(now.plus(validSeconds, ChronoUnit.SECONDS)))
       // To make multiple tokens issued in the same second unique, we add uniquifiers.
       .withClaim(CLAIM_NAME_UNIQUIFIER1, un1)
       .withClaim(CLAIM_NAME_UNIQUIFIER2, un2)
       .sign(algorithm);
}

代码示例来源:origin: mesosphere/dcos-commons

private String createToken() throws NoSuchAlgorithmException {
  KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  keyPairGenerator.initialize(2048);
  KeyPair keyPair = keyPairGenerator.generateKeyPair();
  Algorithm algorithm = Algorithm.RSA256((
      RSAPublicKey) keyPair.getPublic(), (RSAPrivateKey) keyPair.getPrivate());
  return JWT.create()
      .withExpiresAt(Date.from(Instant.now().plusSeconds(120)))
      .withClaim("uid", "test")
      .sign(algorithm);
}

代码示例来源:origin: org.nuxeo.ecm.platform/nuxeo-platform-login-jwt

public JWTBuilderImpl() {
  builder = JWT.create();
  // default Nuxeo issuer, checked during validation
  builder.withIssuer(NUXEO_ISSUER);
  // default to current principal as subject
  String subject = ClientLoginModule.getCurrentPrincipal().getActingUser();
  if (subject == null) {
    throw new NuxeoException("No currently logged-in user");
  }
  builder.withSubject(subject);
  // default TTL
  withTTL(0);
}

代码示例来源:origin: mgtechsoftware/smockin

String generateJWT(final SmockinUser user) {
  return JWT.create()
      .withIssuer(jwtIssuer)
      .withClaim(jwtRoleKey, user.getRole().name())
      .withClaim(jwtFullNameKey, user.getFullName())
      .withClaim(jwtUserNameKey, user.getUsername())
      .withSubject(jwtSubjectKey)
      .withIssuedAt(GeneralUtils.getCurrentDate())
      .withExpiresAt(GeneralUtils.toDate(GeneralUtils.getCurrentDateTime().plusDays(99)))
      .sign(jwtAlgorithm);
}

相关文章