當前位置: 首頁>>代碼示例>>Java>>正文


Java JWTClaimsSet.getExpirationTime方法代碼示例

本文整理匯總了Java中com.nimbusds.jwt.JWTClaimsSet.getExpirationTime方法的典型用法代碼示例。如果您正苦於以下問題:Java JWTClaimsSet.getExpirationTime方法的具體用法?Java JWTClaimsSet.getExpirationTime怎麽用?Java JWTClaimsSet.getExpirationTime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.nimbusds.jwt.JWTClaimsSet的用法示例。


在下文中一共展示了JWTClaimsSet.getExpirationTime方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: isValidJWToken

import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
/**
 * Validates a JWT token.
 * @param secret secret used for generating the token
 * @param jwt token to validate
 * @return true if token is valid
 */
public static boolean isValidJWToken(String secret, SignedJWT jwt) {
	try {
		if (secret != null && jwt != null) {
			JWSVerifier verifier = new MACVerifier(secret);
			if (jwt.verify(verifier)) {
				Date referenceTime = new Date();
				JWTClaimsSet claims = jwt.getJWTClaimsSet();

				Date expirationTime = claims.getExpirationTime();
				Date notBeforeTime = claims.getNotBeforeTime();
				boolean expired = expirationTime == null || expirationTime.before(referenceTime);
				boolean notYetValid = notBeforeTime == null || notBeforeTime.after(referenceTime);

				return !(expired || notYetValid);
			}
		}
	} catch (JOSEException e) {
		logger.warn(null, e);
	} catch (ParseException ex) {
		logger.warn(null, ex);
	}
	return false;
}
 
開發者ID:Erudika,項目名稱:para,代碼行數:30,代碼來源:SecurityUtils.java

示例2: retrieveCredential

import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@Override
public JWTCredential retrieveCredential(String token) {
    JWTCredential result = null;
    try {
        JWSObject jws = JWSObject.parse(token);

        String apiKey = jws.getHeader().getKeyID();
        if (apiKey != null && keys.contains(apiKey)) {

            RSAKey rsaKey = (RSAKey) jwkSet.getKeyByKeyId(apiKey).toPublicJWK();
            JWSVerifier verifier = new RSASSAVerifier(rsaKey);

            if (jws.verify(verifier)) {
                JWTClaimsSet claimsSet = JWTClaimsSet.parse(jws.getPayload().toJSONObject());

                // Verify time validity of token.
                Date creationTime = claimsSet.getIssueTime();
                Date expirationTime = claimsSet.getExpirationTime();
                Date now = new Date();
                long validityPeriod = expirationTime.getTime() - creationTime.getTime();
                if (creationTime.before(now) && now.before(expirationTime) && validityPeriod < 120000 /*2 minutes*/) {

                    JSONObject realmAccess = (JSONObject) claimsSet.getClaim("realm_access");

                    JSONArray rolesArray = (JSONArray) realmAccess.get("roles");

                    Set<String> roles = new HashSet<>();
                    rolesArray.forEach(r -> roles.add(r.toString()));

                    result = new JWTCredential(claimsSet.getSubject(), roles);
                }
            }
        }
    } catch (ParseException | JOSEException e) {
        ; // Token is not valid
    }
    return result;
}
 
開發者ID:atbashEE,項目名稱:jsr375-extensions,代碼行數:39,代碼來源:DemoJWTHandler.java

示例3: createJwtProfile

import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
  protected void createJwtProfile(final TokenCredentials credentials, final JWT jwt) throws ParseException {
      final JWTClaimsSet claimSet = jwt.getJWTClaimsSet();
      String subject = claimSet.getSubject();
      if (subject == null) {
          throw new TechnicalException("JWT must contain a subject ('sub' claim)");
      }
      if (!subject.contains(CommonProfile.SEPARATOR)) {
          subject = JwtProfile.class.getName() + CommonProfile.SEPARATOR + subject;
      }

      final Date expirationTime = claimSet.getExpirationTime();
      if (expirationTime != null) {
          final Date now = new Date();
          if (expirationTime.before(now)) {
              logger.error("The JWT is expired: no profile is built");
              return;
          }
      }

      final Map<String, Object> attributes = new HashMap<>(claimSet.getClaims());
      attributes.remove(JwtClaims.SUBJECT);

final List<String> roles = (List<String>) attributes.get(JwtGenerator.INTERNAL_ROLES);
      attributes.remove(JwtGenerator.INTERNAL_ROLES);
final List<String> permissions = (List<String>) attributes.get(JwtGenerator.INTERNAL_PERMISSIONS);
      attributes.remove(JwtGenerator.INTERNAL_PERMISSIONS);

      final CommonProfile profile = ProfileHelper.buildProfile(subject, attributes);
      if (roles != null) {
          profile.addRoles(roles);
      }
      if (permissions != null) {
          profile.addPermissions(permissions);
      }
      credentials.setUserProfile(profile);
  }
 
開發者ID:yaochi,項目名稱:pac4j-plus,代碼行數:38,代碼來源:JwtAuthenticator.java

示例4: retrieveCredential

import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@Override
public JWTCredential retrieveCredential(String token) {
    JWTCredential result = null;
    try {
        JWSObject jws = JWSObject.parse(token);

        String apiKey = jws.getHeader().getKeyID();
        if (apiKey != null && keys.containsKey(apiKey)) {

            byte[] sharedSecret = keys.get(apiKey);
            JWSVerifier verifier = new MACVerifier(sharedSecret);

            if (jws.verify(verifier)) {
                JWTClaimsSet claimsSet = JWTClaimsSet.parse(jws.getPayload().toJSONObject());

                // Verify time validity of token.
                Date creationTime = claimsSet.getIssueTime();
                Date expirationTime = claimsSet.getExpirationTime();
                Date now = new Date();
                long validityPeriod = expirationTime.getTime() - creationTime.getTime();
                if (creationTime.before(now) && now.before(expirationTime) && validityPeriod < 120000 /*2 minutes*/) {

                    JSONObject realmAccess = (JSONObject) claimsSet.getClaim("realm_access");

                    JSONArray rolesArray = (JSONArray) realmAccess.get("roles");

                    Set<String> roles = new HashSet<>();
                    rolesArray.forEach(r -> roles.add(r.toString()));

                    result = new JWTCredential(claimsSet.getSubject(), roles);
                    result.addInfo(API_KEY, apiKey);
                }
            }
        }
    } catch (ParseException | JOSEException e) {
        ; // Token is not valid
    }
    return result;
}
 
開發者ID:rdebusscher,項目名稱:soteria-jwt,代碼行數:40,代碼來源:DemoJWTHandler.java

示例5: verify

import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@Override
public boolean verify(JWTClaimsSet claimsSet) {
    Date now = Date.from(ZonedDateTime.now(ZoneOffset.UTC).toInstant());
    Date expirationTime = claimsSet.getExpirationTime();
    if (expirationTime == null) {
        logger.warn("Missing expiration date in JWT claims set");
        return false;
    }
    return DateUtils.isAfter(expirationTime, now, maxClockSkewSeconds);
}
 
開發者ID:AusDTO,項目名稱:spring-security-stateless,代碼行數:11,代碼來源:ExpirationJwtClaimsSetVerifier.java

示例6: verify

import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
public boolean verify(JWTClaimsSet claimsSet) {
    Date now = Date.from(ZonedDateTime.now(ZoneOffset.UTC).toInstant());
    Date exp = claimsSet.getExpirationTime();

    if(exp == null) {
        LOG.error("Missing expiration date on JWT");
        return false;
    }

    return DateUtils.isAfter(exp, now, MAX_CLOCK_SKEW_SECONDS);
}
 
開發者ID:AusDTO,項目名稱:citizenship-appointment-server,代碼行數:12,代碼來源:ExpirationJwtClaimsVerifier.java

示例7: retrieveCredential

import com.nimbusds.jwt.JWTClaimsSet; //導入方法依賴的package包/類
@Override
public JWTCredential retrieveCredential(String token) {
    JWTCredential result = null;
    try {
        // Parse the JWE string
        JWEObject jweObject = JWEObject.parse(token);
        String apiKey = jweObject.getHeader().getKeyID();
        // Use this apiKey to select the correct privateKey

        RSAKey privateKey = (RSAKey) jwkSet.getKeyByKeyId(apiKey);

        // Decrypt with shared key
        jweObject.decrypt(new RSADecrypter(privateKey));

        // Extract payload
        SignedJWT signedJWT = jweObject.getPayload().toSignedJWT();

        // Check the HMAC, Optional
        signedJWT.verify(new MACVerifier(apiKey));

        // Retrieve the JWT claims...
        JWTClaimsSet claimsSet = signedJWT.getJWTClaimsSet();

        // Verify time validity of token.
        Date creationTime = claimsSet.getIssueTime();
        Date expirationTime = claimsSet.getExpirationTime();
        Date now = new Date();

        long validityPeriod = expirationTime.getTime() - creationTime.getTime();
        if (creationTime.before(now) && now.before(expirationTime) && validityPeriod < 120000 /*2 minutes*/) {

            JSONObject realmAccess = (JSONObject) claimsSet.getClaim("realm_access");

            JSONArray rolesArray = (JSONArray) realmAccess.get("roles");

            Set<String> roles = new HashSet<>();
            rolesArray.forEach(r -> roles.add(r.toString()));

            result = new JWTCredential(claimsSet.getSubject(), roles);
            result.addInfo(API_KEY, apiKey);
            result.addInfo(API_KEY, apiKey);
        }

    } catch (ParseException | JOSEException e) {
        ; // Token is not valid
    }
    return result;
}
 
開發者ID:rdebusscher,項目名稱:soteria-jwt,代碼行數:49,代碼來源:DemoJWTHandler.java


注:本文中的com.nimbusds.jwt.JWTClaimsSet.getExpirationTime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。