예제 #1
0
  public static String createToken(
      String issuer, String subject, String audience, boolean expiry, boolean sign) {
    // Create the JWT Token
    JwtClaims claims = new JwtClaims();
    claims.setSubject(subject);
    if (issuer != null) {
      claims.setIssuer(issuer);
    }
    claims.setIssuedAt(new Date().getTime() / 1000L);
    if (expiry) {
      Calendar cal = Calendar.getInstance();
      cal.add(Calendar.SECOND, 60);
      claims.setExpiryTime(cal.getTimeInMillis() / 1000L);
    }
    if (audience != null) {
      claims.setAudiences(Collections.singletonList(audience));
    }

    if (sign) {
      // Sign the JWT Token
      Properties signingProperties = new Properties();
      signingProperties.put("rs.security.keystore.type", "jks");
      signingProperties.put("rs.security.keystore.password", "password");
      signingProperties.put("rs.security.keystore.alias", "alice");
      signingProperties.put(
          "rs.security.keystore.file", "org/apache/cxf/systest/jaxrs/security/certs/alice.jks");
      signingProperties.put("rs.security.key.password", "password");
      signingProperties.put("rs.security.signature.algorithm", "RS256");

      JwsHeaders jwsHeaders = new JwsHeaders(signingProperties);
      JwsJwtCompactProducer jws = new JwsJwtCompactProducer(jwsHeaders, claims);

      JwsSignatureProvider sigProvider =
          JwsUtils.loadSignatureProvider(signingProperties, jwsHeaders);

      return jws.signWith(sigProvider);
    }

    JwsHeaders jwsHeaders = new JwsHeaders(SignatureAlgorithm.NONE);
    JwsJwtCompactProducer jws = new JwsJwtCompactProducer(jwsHeaders, claims);
    return jws.getSignedEncodedJws();
  }
예제 #2
0
 public IdToken(JwtClaims claims) {
   this(claims.asMap());
 }