当前位置: 首页>>代码示例>>Java>>正文


Java JsonWebSignature.setPayload方法代码示例

本文整理汇总了Java中org.jose4j.jws.JsonWebSignature.setPayload方法的典型用法代码示例。如果您正苦于以下问题:Java JsonWebSignature.setPayload方法的具体用法?Java JsonWebSignature.setPayload怎么用?Java JsonWebSignature.setPayload使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.jose4j.jws.JsonWebSignature的用法示例。


在下文中一共展示了JsonWebSignature.setPayload方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateJWTAssertion

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
public static String generateJWTAssertion(String email, String privateKeyBase64,
    float expiryInSeconds) {
  PrivateKey privateKey = getPrivateKey(privateKeyBase64);
  final JwtClaims claims = new JwtClaims();
  claims.setSubject(email);
  claims.setAudience("https://api.metamind.io/v1/oauth2/token");
  claims.setExpirationTimeMinutesInTheFuture(expiryInSeconds / 60);
  claims.setIssuedAtToNow();

  // Generate the payload
  final JsonWebSignature jws = new JsonWebSignature();
  jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
  jws.setPayload(claims.toJson());
  jws.setKeyIdHeaderValue(UUID.randomUUID().toString());

  // Sign using the private key
  jws.setKey(privateKey);
  try {
    return jws.getCompactSerialization();
  } catch (JoseException e) {
    return null;
  }
}
 
开发者ID:MetaMind,项目名称:quickstart,代码行数:24,代码来源:AssertionGenerator.java

示例2: createSignedTokenFromClaims

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * Create a RSA256 signed token from given claims and RSA jwk.
 * 
 * @param JwtClaims claims
 * @param RsaJsonWebKey rsaJsonWebKey
 * @return String
 * @throws JoseException
 */
private String createSignedTokenFromClaims(JwtClaims claims, RsaJsonWebKey rsaJsonWebKey) throws JoseException {

  // A JWT is a JWS and/or a JWE with JSON claims as the payload.
  // In this example it is a JWS so we create a JsonWebSignature object.
  JsonWebSignature jws = new JsonWebSignature();

  // The payload of the JWS is JSON content of the JWT Claims
  jws.setPayload(claims.toJson());

  // The JWT is signed using the private key
  jws.setKey(rsaJsonWebKey.getPrivateKey());

  // Set the signature algorithm on the JWT/JWS that will integrity protect the claims
  jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);

  return jws.getCompactSerialization();
}
 
开发者ID:Staffbase,项目名称:plugins-sdk-java,代码行数:26,代码来源:SSOFacadeTest.java

示例3: createUnsupportedSignedTokenFromClaims

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * Create a RSA384 signed token from given claims and RSA jwk.
 * 
 * @param JwtClaims claims
 * @param RsaJsonWebKey rsaJsonWebKey
 * @return String
 * @throws JoseException
 */
private String createUnsupportedSignedTokenFromClaims(JwtClaims claims, RsaJsonWebKey rsaJsonWebKey) throws JoseException {

  // A JWT is a JWS and/or a JWE with JSON claims as the payload.
  // In this example it is a JWS so we create a JsonWebSignature object.
  JsonWebSignature jws = new JsonWebSignature();

  // The payload of the JWS is JSON content of the JWT Claims
  jws.setPayload(claims.toJson());

  // The JWT is signed using the private key
  jws.setKey(rsaJsonWebKey.getPrivateKey());

  // Set the signature algorithm on the JWT/JWS that will integrity protect the claims
  jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA384);    

  return jws.getCompactSerialization();
}
 
开发者ID:Staffbase,项目名称:plugins-sdk-java,代码行数:26,代码来源:SSOFacadeTest.java

示例4: encode

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * Sign id token claim string.
 *
 * @param svc    the service
 * @param claims the claims
 * @return the string
 * @throws JoseException the jose exception
 */
public String encode(final OidcRegisteredService svc, final JwtClaims claims) throws JoseException {
    try {
        LOGGER.debug("Attempting to produce id token generated for service [{}]", svc);
        final JsonWebSignature jws = new JsonWebSignature();
        final String jsonClaims = claims.toJson();
        jws.setPayload(jsonClaims);
        LOGGER.debug("Generated claims to put into id token are [{}]", jsonClaims);

        jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.NONE);
        jws.setAlgorithmConstraints(AlgorithmConstraints.NO_CONSTRAINTS);

        String innerJwt = svc.isSignIdToken() ? signIdToken(svc, jws) : jws.getCompactSerialization();
        if (svc.isEncryptIdToken() && StringUtils.isNotBlank(svc.getIdTokenEncryptionAlg())
                && StringUtils.isNotBlank(svc.getIdTokenEncryptionEncoding())) {
            innerJwt = encryptIdToken(svc, jws, innerJwt);
        }

        return innerJwt;
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw Throwables.propagate(e);
    }
}
 
开发者ID:mrluo735,项目名称:cas-5.1.0,代码行数:32,代码来源:OidcIdTokenSigningAndEncryptionService.java

示例5: sign

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public String sign(final JwtClaims claims) {

    try {
        final RsaJsonWebKey aSigningKey = cachedDataProvider.getASigningKey();
        final JsonWebSignature jws = new JsonWebSignature();
        jws.setPayload(claims.toJson());
        jws.setKeyIdHeaderValue(aSigningKey.getKeyId());
        jws.setKey(aSigningKey.getPrivateKey());
        jws.setAlgorithmHeaderValue(aSigningKey.getAlgorithm());
        jws.sign();
        return jws.getCompactSerialization();
    } catch (final JoseException e) {
        throw new InternalServerErrorException(e);
    }
}
 
开发者ID:trajano,项目名称:app-ms,代码行数:20,代码来源:JcaCryptoOps.java

示例6: generateToken

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
public String generateToken(String subject) {
    final JwtClaims claims = new JwtClaims();
    claims.setSubject(subject);
    claims.setExpirationTimeMinutesInTheFuture(TOKEN_EXPIRATION_IN_MINUTES);

    final JsonWebSignature jws = new JsonWebSignature();
    jws.setPayload(claims.toJson());
    jws.setAlgorithmHeaderValue(HMAC_SHA256);
    jws.setKey(new HmacKey(tokenSecret));
    jws.setDoKeyValidation(false); //relaxes hmac key length restrictions

    try {
        return jws.getCompactSerialization();
    } catch (JoseException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:jtanza,项目名称:rufus,代码行数:18,代码来源:TokenGenerator.java

示例7: generateJwt

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
private static String generateJwt(RsaJsonWebKey jwk, Optional<String> keyId)
    throws JoseException {
  JwtClaims claims = new JwtClaims();
  claims.setIssuer("Issuer");
  claims.setAudience("Audience");

  JsonWebSignature jws = new JsonWebSignature();
  jws.setPayload(claims.toJson());
  jws.setKey(jwk.getPrivateKey());
  jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);

  if (keyId.isPresent()) {
    jws.setKeyIdHeaderValue(keyId.get());
  }

  return jws.getCompactSerialization();
}
 
开发者ID:cloudendpoints,项目名称:endpoints-management-java,代码行数:18,代码来源:DefaultAuthTokenVerifierTest.java

示例8: createToken

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
private static String createToken(Key key, JsonObject jsonClaims) {

        JwtClaims claims = new JwtClaims();
        claims.setSubject(jsonClaims.toString());
        claims.setIssuedAtToNow();
        claims.setExpirationTime(NumericDate.fromSeconds(NumericDate.now().getValue() + JWT_TOKEN_EXPIRES_TIME));

        JsonWebSignature jws = new JsonWebSignature();
        jws.setDoKeyValidation(false);
        jws.setPayload(claims.toJson());
        jws.setKey(key);
        jws.setAlgorithmHeaderValue(ALG);

        try {
            return jws.getCompactSerialization();
        } catch (JoseException ex) {
            LOGGER.log(Level.SEVERE, null, ex);
        }

        return null;
    }
 
开发者ID:polarsys,项目名称:eplmp,代码行数:22,代码来源:JWTokenFactory.java

示例9: testNpeWithNonExtractableKeyDataHS256

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
@Test
public void testNpeWithNonExtractableKeyDataHS256() throws Exception
{
    byte[] raw = Base64Url.decode("hup76LcA9B7pqrEtqyb4EBg6XCcr9r0iOCFF1FeZiJM");
    FakeHsmNonExtractableSecretKeySpec key = new FakeHsmNonExtractableSecretKeySpec(raw, "HmacSHA256");
    JwtClaims claims = new JwtClaims();
    claims.setExpirationTimeMinutesInTheFuture(5);
    claims.setSubject("subject");
    claims.setIssuer("issuer");
    JsonWebSignature jws = new JsonWebSignature();
    jws.setPayload(claims.toJson());
    jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
    jws.setKey(key);
    String jwt = jws.getCompactSerialization();
    JwtConsumerBuilder jwtConsumerBuilder = new JwtConsumerBuilder();
    jwtConsumerBuilder.setAllowedClockSkewInSeconds(60);
    jwtConsumerBuilder.setRequireSubject();
    jwtConsumerBuilder.setExpectedIssuer("issuer");
    jwtConsumerBuilder.setVerificationKey(key);
    JwtConsumer jwtConsumer = jwtConsumerBuilder.build();
    JwtClaims processedClaims = jwtConsumer.processToClaims(jwt);
    System.out.println(processedClaims);
}
 
开发者ID:RbkGh,项目名称:Jose4j,代码行数:24,代码来源:JwtConsumerTest.java

示例10: testSign

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
@Test
public void testSign() throws Exception {
    // Base64 string server public/private key
    String vapidPublicKey = "BOH8nTQA5iZhl23+NCzGG9prvOZ5BE0MJXBW+GUkQIvRVTVB32JxmX0V1j6z0r7rnT7+bgi6f2g5fMPpAh5brqM=";
    String vapidPrivateKey = "TRlY/7yQzvqcLpgHQTxiU5fVzAAvAw/cdSh5kLFLNqg=";

    JwtClaims claims = new JwtClaims();
    claims.setAudience("https://developer.services.mozilla.com/a476b8ea-c4b8-4359-832a-e2747b6ab88a");

    JsonWebSignature jws = new JsonWebSignature();
    jws.setPayload(claims.toJson());
    jws.setKey(Utils.loadPrivateKey(vapidPrivateKey));
    jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256);

    System.out.println(jws.getCompactSerialization());
}
 
开发者ID:web-push-libs,项目名称:webpush-java,代码行数:17,代码来源:PushServiceTest.java

示例11: generateJWT

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * Generates a JWT as String representation.
 * Encodes the id and the role of the user as "userId" and "userRole" in the claims of the jwt
 *
 * @param user
 *         The user to generate the JWT from.
 * @return The string representation of the jwt.
 * @throws JoseException
 *         If the Jose library failed to create a JWT token.
 */
public static String generateJWT(User user) throws JoseException {
    // generate claims with user data
    JwtClaims claims = new JwtClaims();
    claims.setIssuer("ALEX");
    claims.setGeneratedJwtId();
    claims.setClaim("id", user.getId());
    claims.setClaim("role", user.getRole());
    claims.setClaim("email", user.getEmail());

    // create signature
    JsonWebSignature jws = new JsonWebSignature();
    jws.setPayload(claims.toJson());
    jws.setKey(getKey().getPrivateKey());
    jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);

    // return signed jwt
    return jws.getCompactSerialization();
}
 
开发者ID:LearnLib,项目名称:alex,代码行数:29,代码来源:JWTHelper.java

示例12: createExternalAccountBinding

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * Creates a JSON structure for external account binding.
 *
 * @param kid
 *            Key Identifier provided by the CA
 * @param accountKey
 *            {@link PublicKey} of the account to register
 * @param macKey
 *            {@link SecretKey} to sign the key identifier with
 * @param resource
 *            "newAccount" resource URL
 * @return Created JSON structure
 */
private Map<String, Object> createExternalAccountBinding(String kid,
            PublicKey accountKey, SecretKey macKey, URL resource)
            throws AcmeException {
    try {
        PublicJsonWebKey keyJwk = PublicJsonWebKey.Factory.newPublicJwk(accountKey);

        JsonWebSignature innerJws = new JsonWebSignature();
        innerJws.setPayload(keyJwk.toJson());
        innerJws.getHeaders().setObjectHeaderValue("url", resource);
        innerJws.getHeaders().setObjectHeaderValue("kid", kid);
        innerJws.setAlgorithmHeaderValue(macKeyAlgorithm(macKey));
        innerJws.setKey(macKey);
        innerJws.setDoKeyValidation(false);
        innerJws.sign();

        JSONBuilder outerClaim = new JSONBuilder();
        outerClaim.put("protected", innerJws.getHeaders().getEncodedHeader());
        outerClaim.put("signature", innerJws.getEncodedSignature());
        outerClaim.put("payload", innerJws.getEncodedPayload());
        return outerClaim.toMap();
    } catch (JoseException ex) {
        throw new AcmeException("Could not create external account binding", ex);
    }
}
 
开发者ID:shred,项目名称:acme4j,代码行数:38,代码来源:AccountBuilder.java

示例13: signToken

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * Signs an JWT authentication token, acting as simulated authentication
 * endpoint that issues auth tokens.
 *
 * @param tokenIssuer
 * @param signatureKeyPair
 * @param expirationTime
 *            Expiration time in minutes to set for {@code exp} claim. Can
 *            be <code>null</code>, in which case the header is left out.
 * @return
 * @throws JoseException
 */
private String signToken(String tokenIssuer, RsaJsonWebKey signatureKeyPair, DateTime expirationTime)
        throws JoseException {
    // Create the Claims, which will be the content of the JWT
    JwtClaims claims = new JwtClaims();
    claims.setIssuer(tokenIssuer);
    if (expirationTime != null) {
        claims.setExpirationTime(NumericDate.fromMilliseconds(expirationTime.getMillis()));
    }
    claims.setGeneratedJwtId();
    NumericDate now = NumericDate.fromMilliseconds(UtcTime.now().getMillis());
    claims.setIssuedAt(now);
    // the subject/principal is whom the token is about
    claims.setSubject(TOKEN_SUBJECT);
    // additional claims
    claims.setClaim("role", TOKEN_ROLE);

    JsonWebSignature jws = new JsonWebSignature();
    jws.setPayload(claims.toJson());
    jws.setKey(signatureKeyPair.getPrivateKey());
    jws.setKeyIdHeaderValue(signatureKeyPair.getKeyId());
    jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
    return jws.getCompactSerialization();
}
 
开发者ID:elastisys,项目名称:scale.commons,代码行数:36,代码来源:TestAuthTokenHeaderValidator.java

示例14: signToken

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
 * Signs an JWT authentication token, acting as simulated authentication
 * endpoint that issues auth tokens.
 *
 * @param tokenIssuer
 * @param signatureKeyPair
 * @param expirationTime
 *            Expiration time in minutes to set for {@code exp} claim. Can
 *            be <code>null</code>, in which case the header is left out.
 * @return
 * @throws JoseException
 */
private String signToken(String tokenIssuer, RsaJsonWebKey signatureKeyPair, DateTime expirationTime)
        throws JoseException {
    // Create the Claims, which will be the content of the JWT
    JwtClaims claims = new JwtClaims();
    claims.setIssuer(tokenIssuer);
    if (expirationTime != null) {
        claims.setExpirationTime(NumericDate.fromMilliseconds(expirationTime.getMillis()));
    }
    claims.setGeneratedJwtId();
    NumericDate now = NumericDate.fromMilliseconds(UtcTime.now().getMillis());
    claims.setIssuedAt(now);
    // the subject/principal is whom the token is about
    claims.setSubject("[email protected]");
    // additional claims
    claims.setClaim("role", "user");

    JsonWebSignature jws = new JsonWebSignature();
    jws.setPayload(claims.toJson());
    jws.setKey(signatureKeyPair.getPrivateKey());
    jws.setKeyIdHeaderValue(signatureKeyPair.getKeyId());
    jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
    return jws.getCompactSerialization();
}
 
开发者ID:elastisys,项目名称:scale.commons,代码行数:36,代码来源:TestAuthTokenRequestFilter.java

示例15: getJwsCompactSerialization

import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
@Override
public String getJwsCompactSerialization(String payload, byte[] secretKey) {
    try {
        Key key = new AesKey(secretKey);

        JsonWebSignature jws = new JsonWebSignature();
        jws.setPayload(payload);
        jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
        jws.setKey(key);
        String jwsCompactSerialization = jws.getCompactSerialization();

        return jwsCompactSerialization;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
开发者ID:ix3,项目名称:ix3,代码行数:17,代码来源:JwsImplJose4j.java


注:本文中的org.jose4j.jws.JsonWebSignature.setPayload方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。