io.jsonwebtoken.Claims.getIssuedAt()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(385)

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

Claims.getIssuedAt介绍

[英]Returns the JWT iat (issued at) timestamp or null if not present.

If present, this value is the timestamp when the JWT was created.
[中]返回JWTiat(在发出时)时间戳,如果不存在,则返回null。
如果存在,则该值是创建JWT时的时间戳。

代码示例

代码示例来源:origin: stylefeng/Guns

/**
 * 获取jwt发布时间
 */
public static Date getIssuedAtDateFromToken(String token) {
  return getClaimFromToken(token).getIssuedAt();
}

代码示例来源:origin: SonarSource/sonarqube

private Optional<Token> validateToken(String tokenEncoded, HttpServletRequest request, HttpServletResponse response) {
 Optional<Claims> claims = jwtSerializer.decode(tokenEncoded);
 if (!claims.isPresent()) {
  return Optional.empty();
 }
 Date now = new Date(system2.now());
 Claims token = claims.get();
 if (now.after(addSeconds(token.getIssuedAt(), SESSION_DISCONNECT_IN_SECONDS))) {
  return Optional.empty();
 }
 jwtCsrfVerifier.verifyState(request, (String) token.get(CSRF_JWT_PARAM), token.getSubject());
 if (now.after(addSeconds(getLastRefreshDate(token), SESSION_REFRESH_IN_SECONDS))) {
  refreshToken(token, request, response);
 }
 Optional<UserDto> user = selectUserFromUuid(token.getSubject());
 if (!user.isPresent()) {
  return Optional.empty();
 }
 return Optional.of(new Token(user.get(), claims.get()));
}

代码示例来源:origin: SonarSource/sonarqube

Optional<Claims> decode(String token) {
 checkIsStarted();
 Claims claims = null;
 try {
  claims = Jwts.parser()
   .setSigningKey(secretKey)
   .parseClaimsJws(token)
   .getBody();
  requireNonNull(claims.getId(), "Token id hasn't been found");
  requireNonNull(claims.getSubject(), "Token subject hasn't been found");
  requireNonNull(claims.getExpiration(), "Token expiration date hasn't been found");
  requireNonNull(claims.getIssuedAt(), "Token creation date hasn't been found");
  return Optional.of(claims);
 } catch (ExpiredJwtException | SignatureException e) {
  return Optional.empty();
 } catch (Exception e) {
  throw AuthenticationException.newBuilder()
   .setSource(Source.jwt())
   .setLogin(claims == null ? null : claims.getSubject())
   .setMessage(e.getMessage())
   .build();
 }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void decode_token() {
 setSecretKey(A_SECRET_KEY);
 underTest.start();
 Date now = new Date();
 String token = underTest.encode(new JwtSession(USER_LOGIN, 20 * 60));
 Claims claims = underTest.decode(token).get();
 assertThat(claims.getId()).isNotEmpty();
 assertThat(claims.getSubject()).isEqualTo(USER_LOGIN);
 assertThat(claims.getExpiration()).isNotNull();
 assertThat(claims.getIssuedAt()).isNotNull();
 // Check expiration date it set to more than 19 minutes in the future
 assertThat(claims.getExpiration()).isAfterOrEqualsTo(new Date(now.getTime() + 19 * 60 * 1000));
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void refresh_token() {
 setSecretKey(A_SECRET_KEY);
 underTest.start();
 Date now = new Date();
 Date createdAt = DateUtils.parseDate("2016-01-01");
 // Expired in 10 minutes
 Date expiredAt = new Date(now.getTime() + 10 * 60 * 1000);
 Claims token = new DefaultClaims()
  .setId("id")
  .setSubject("subject")
  .setIssuer("sonarqube")
  .setIssuedAt(createdAt)
  .setExpiration(expiredAt);
 token.put("key", "value");
 // Refresh the token with a higher expiration time
 String encodedToken = underTest.refresh(token, 20 * 60);
 Claims result = underTest.decode(encodedToken).get();
 assertThat(result.getId()).isEqualTo("id");
 assertThat(result.getSubject()).isEqualTo("subject");
 assertThat(result.getIssuer()).isEqualTo("sonarqube");
 assertThat(result.getIssuedAt()).isEqualTo(createdAt);
 assertThat(result.get("key")).isEqualTo("value");
 // Expiration date has been changed
 assertThat(result.getExpiration()).isNotEqualTo(expiredAt)
  .isAfterOrEqualsTo(new Date(now.getTime() + 19 * 1000));
}

代码示例来源:origin: stackoverflow.com

@Test
public void tokenValidateTest() {
  String token = "exX0xXAi0iJKV1QiLCJhbGciOiJIUzI1NiJ9.eyXK2IjxxxJkIjp7InByb3ZpZGVyIjoicGFzc3dvcmQiLCJ1aWQiOiI5NDA2ZDc1Ny01ZjdmLTQ0MGMtYWRiMS05MmU4Y2U3N2QzYTBjKSwiaWF0IjoxNDYwMNYwODMzxQ.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  String secret = "6x3Ox29Xx1xxxXXiI0P3PL5pWdNUE5sPcXXXLxxxX";

  try {
    Claims claims = Jwts.parser().setSigningKey(secret.getBytes("UTF-8")).parseClaimsJws(token).getBody();

    Date issuedDate = claims.getIssuedAt();

    assert(issuedDate.before(new Date()));

  } catch (Exception ex) {
    ex.printStackTrace();
  }

}

代码示例来源:origin: cassiomolin/jersey-jwt-springsecurity

/**
 * Extract the issued date from the token claims.
 *
 * @param claims
 * @return Issued date of the JWT token
 */
private ZonedDateTime extractIssuedDateFromClaims(@NotNull Claims claims) {
  return ZonedDateTime.ofInstant(claims.getIssuedAt().toInstant(), ZoneId.systemDefault());
}

代码示例来源:origin: cassiomolin/jersey-jwt

/**
 * Extract the issued date from the token claims.
 *
 * @param claims
 * @return Issued date of the JWT token
 */
private ZonedDateTime extractIssuedDateFromClaims(@NotNull Claims claims) {
  return ZonedDateTime.ofInstant(claims.getIssuedAt().toInstant(), ZoneId.systemDefault());
}

代码示例来源:origin: bfwg/springboot-jwt-starter

public Date getIssuedAtDateFromToken(String token) {
  Date issueAt;
  try {
    final Claims claims = this.getAllClaimsFromToken(token);
    issueAt = claims.getIssuedAt();
  } catch (Exception e) {
    issueAt = null;
  }
  return issueAt;
}

代码示例来源:origin: qiurunze123/GEEKQ-CM

/**
 * 获取jwt发布时间
 */
public Date getIssuedAtDateFromToken(String token) {
  return getClaimFromToken(token).getIssuedAt();
}

代码示例来源:origin: qiurunze123/GEEKQ-CM

/**
 * 获取jwt发布时间
 */
public Date getIssuedAtDateFromToken(String token) {
  return getClaimFromToken(token).getIssuedAt();
}

代码示例来源:origin: qiurunze123/GEEKQ-CM

/**
 * 获取jwt发布时间
 */
public Date getIssuedAtDateFromToken(String token) {
  return getClaimFromToken(token).getIssuedAt();
}

代码示例来源:origin: qiurunze123/GEEKQ-CM

/**
 * 获取jwt发布时间
 */
public Date getIssuedAtDateFromToken(String token) {
  return getClaimFromToken(token).getIssuedAt();
}

代码示例来源:origin: Caratacus/Crown

/**
 * 获取jwt发布时间
 */
public static Date getIssuedAt(String token) {
  return getClaim(token).getIssuedAt();
}

代码示例来源:origin: qiurunze123/GEEKQ-CM

/**
 * 获取jwt发布时间
 */
public Date getIssuedAtDateFromToken(String token) {
  return getClaimFromToken(token).getIssuedAt();
}

代码示例来源:origin: qiurunze123/GEEKQ-CM

/**
 * 获取jwt发布时间
 */
public Date getIssuedAtDateFromToken(String token) {
  return getClaimFromToken(token).getIssuedAt();
}

代码示例来源:origin: com.arsframework/ars-core

/**
 * 解析令牌
 *
 * @param code 令牌标识
 * @return 令牌对象
 */
public static Token parse(String code) {
  if (code == null) {
    throw new IllegalArgumentException("Code must not be null");
  }
  Claims claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(code).getBody();
  int timeout = (int) (claims.getExpiration().getTime() - claims.getIssuedAt().getTime());
  return new Token(code, timeout, claims);
}

代码示例来源:origin: org.sonarsource.sonarqube/sonar-server

private Optional<Token> validateToken(String tokenEncoded, HttpServletRequest request, HttpServletResponse response) {
 Optional<Claims> claims = jwtSerializer.decode(tokenEncoded);
 if (!claims.isPresent()) {
  return Optional.empty();
 }
 Date now = new Date(system2.now());
 Claims token = claims.get();
 if (now.after(addSeconds(token.getIssuedAt(), SESSION_DISCONNECT_IN_SECONDS))) {
  return Optional.empty();
 }
 jwtCsrfVerifier.verifyState(request, (String) token.get(CSRF_JWT_PARAM), token.getSubject());
 if (now.after(addSeconds(getLastRefreshDate(token), SESSION_REFRESH_IN_SECONDS))) {
  refreshToken(token, request, response);
 }
 Optional<UserDto> user = selectUserFromDb(token.getSubject());
 if (!user.isPresent()) {
  return Optional.empty();
 }
 return Optional.of(new Token(user.get(), claims.get()));
}

代码示例来源:origin: com.stormpath.sdk/stormpath-sdk-servlet

private int getMaxAge(String token, byte[] clientSecret, CookieConfig cookieConfig, HttpServletRequest request, HttpServletResponse response) {
  // non-zero indicates override from cookie config
  if (cookieConfig.getMaxAge() != 0 ) {
    return cookieConfig.getMaxAge();
  }
  if(Strings.hasText(token) && token.split("\\.").length == 3) {
    // otherwise, use the claims in the JWT to determine maxAge
    Jws<Claims> claimsJws = Jwts.parser().setSigningKeyResolver(createKeyResolver(request, response)).parseClaimsJws(token);
    DateTime issueAt = new DateTime(claimsJws.getBody().getIssuedAt());
    DateTime expiration = new DateTime(claimsJws.getBody().getExpiration());
    return Seconds.secondsBetween(issueAt, expiration).getSeconds() - Seconds.secondsBetween(issueAt, DateTime.now()).getSeconds();
  }
  return DEFAULT_COOKIE_MAX_AGE;
}

代码示例来源:origin: stormpath/stormpath-sdk-java

private int getMaxAge(String token, byte[] clientSecret, CookieConfig cookieConfig, HttpServletRequest request, HttpServletResponse response) {
  // non-zero indicates override from cookie config
  if (cookieConfig.getMaxAge() != 0 ) {
    return cookieConfig.getMaxAge();
  }
  if(Strings.hasText(token) && token.split("\\.").length == 3) {
    // otherwise, use the claims in the JWT to determine maxAge
    Jws<Claims> claimsJws = Jwts.parser().setSigningKeyResolver(createKeyResolver(request, response)).parseClaimsJws(token);
    DateTime issueAt = new DateTime(claimsJws.getBody().getIssuedAt());
    DateTime expiration = new DateTime(claimsJws.getBody().getExpiration());
    return Seconds.secondsBetween(issueAt, expiration).getSeconds() - Seconds.secondsBetween(issueAt, DateTime.now()).getSeconds();
  }
  return DEFAULT_COOKIE_MAX_AGE;
}

相关文章