.setAllowedClockSkewInSeconds(confService.getAllowedClockSkew()) .setRequireSubject() .setExpectedIssuer(confService.getIssuer()) .setExpectedAudience(confService.getClientID()) .setVerificationKeyResolver(resolver)
public JsonWebTokenVerificationBuilder expectingIssuer(String issuer) { builder.setExpectedIssuer(issuer); return this; }
/** * Indicates the expected value of the issuer ("iss") claim and that the claim is required. * Equivalent to calling {@link #setExpectedIssuer(boolean, String)} with {@code true} as the first argument. * @param expectedIssuer the value that the issuer claim must have to pass validation, {@code null} means that any value is acceptable * @return the same JwtConsumerBuilder */ public JwtConsumerBuilder setExpectedIssuer(String expectedIssuer) { return setExpectedIssuer(true, expectedIssuer); }
public JWTVerifier(final String secret, final String issuer, final String audience) { final JwtConsumerBuilder builder = new JwtConsumerBuilder(); if (StringUtils.isNotBlank(audience)) builder.setExpectedAudience(audience); if (StringUtils.isNotBlank(issuer)) builder.setExpectedIssuer(issuer); builder.setVerificationKey(new HmacKey(secret.getBytes(StandardCharsets.UTF_8))); builder.setAllowedClockSkewInSeconds(60); builder.setRelaxVerificationKeyValidation(); // Allow HMAC keys < 256 bits consumer = builder.build(); }
.setAllowedClockSkewInSeconds(30) .setRequireSubject() .setExpectedIssuer(oidcConfig.getIssuer()) .setVerificationKey(((RsaJsonWebKey)oidcConfig.getSigningKey()).getKey()) .setExpectedAudience(System.getenv("OIDC_AUDIENCE") != null ? System.getenv("OIDC_AUDIENCE") : System.getProperty("OIDC_AUDIENCE", "hobson-webconsole"))
public static Map<String, Object> verifyJwt(String jwt) throws InvalidJwtException, MalformedClaimException { Map<String, Object> user = null; X509VerificationKeyResolver x509VerificationKeyResolver = new X509VerificationKeyResolver(certificate); x509VerificationKeyResolver.setTryAllOnNoThumbHeader(true); JwtConsumer jwtConsumer = new JwtConsumerBuilder() .setRequireExpirationTime() // the JWT must have an expiration time .setAllowedClockSkewInSeconds((Integer) config.get(CLOCK_SKEW_IN_MINUTE)*60) // allow some leeway in validating time based claims to account for clock skew .setRequireSubject() // the JWT must have a subject claim .setExpectedIssuer(issuer) .setExpectedAudience(audience) .setVerificationKeyResolver(x509VerificationKeyResolver) // verify the signature with the certificates .build(); // create the JwtConsumer instance // Validate the JWT and process it to the Claims JwtClaims claims = jwtConsumer.processToClaims(jwt); if(claims != null) { user = new HashMap<String, Object>(); user.put("userId", claims.getClaimValue("userId")); user.put("clientId", claims.getClaimValue("clientId")); List roles = claims.getStringListClaimValue("roles"); user.put("roles", roles); Object host = claims.getClaimValue("host"); if(host != null) user.put("host", host); } return user; } }
.setRequireExpirationTime() .setAllowedClockSkewInSeconds(60) .setExpectedIssuer(issuer != null ? issuer : config.getIssuer()) .setExpectedAudience(audience != null ? audience : config.getAudience()) .setEvaluationTime(org.jose4j.jwt.NumericDate.now())
.setExpectedIssuer(ISSUER) // whom the JWT needs to have been issued by
.setRequireExpirationTime() .setAllowedClockSkewInSeconds(60) .setExpectedIssuer(issuer != null ? issuer : config.getIssuer()) .setExpectedAudience(audience != null ? audience : config.getAudience()) .setEvaluationTime(org.jose4j.jwt.NumericDate.now())
public static boolean validateToken(String token) { JwtConsumer jwtConsumer = new JwtConsumerBuilder() .setRequireExpirationTime() // the JWT must have an expiration time .setAllowedClockSkewInSeconds(30) // allow some leeway in validating time based claims to account for clock skew .setRequireSubject() // the JWT must have a subject claim .setExpectedIssuer(ISSUER) // whom the JWT needs to have been issued by .setExpectedAudience(AUDIENCE) // to whom the JWT is intended for .setVerificationKey(rsaJsonWebKey.getKey()) // verify the signature with the public key .build(); // create the JwtConsumer instance try { // Validate the JWT and process it to the Claims JwtClaims jwtClaims = jwtConsumer.processToClaims(token); //过期时间 //用户名和ID return true; } catch (InvalidJwtException e) { // InvalidJwtException will be thrown, if the JWT failed processing or validation in anyway. // Hopefully with meaningful explanations(s) about what went wrong. System.out.println("Invalid JWT! " + e); return false; } catch (Exception ex) { ex.printStackTrace(); return false; } }
.setExpectedIssuer(issuer) // Set expected issuer
.setExpectedIssuer(issuer) // Set expected issuer
builder.setExpectedIssuer(true, authContextInfo.getIssuedBy()); } else { builder.setExpectedIssuer(false, null);
.setRequireSubject() .setSkipDefaultAudienceValidation() .setExpectedIssuer(authContextInfo.getIssuedBy()) .setVerificationKey(authContextInfo.getSignerKey()) .setJwsAlgorithmConstraints(