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


Java Payload类代码示例

本文整理汇总了Java中com.nimbusds.jose.Payload的典型用法代码示例。如果您正苦于以下问题:Java Payload类的具体用法?Java Payload怎么用?Java Payload使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: getSignedContent

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public String getSignedContent(String content) {
    Payload contentPayload = new Payload(content);

    try {
        RSASSASigner rsa = new RSASSASigner((RSAPrivateKey) clientJwk);
        JWSAlgorithm alg = JWSAlgorithm.RS256;
        JWSHeader header = new JWSHeader.Builder(alg)
            .keyID(clientJwk.getKeyID())
            .build();
        JWSObject jws = new JWSObject(header, contentPayload);
        jws.sign(rsa);
        return jws.serialize();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:PacktPublishing,项目名称:OAuth-2.0-Cookbook,代码行数:17,代码来源:JwkKeyPairManager.java

示例2: encode

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public String encode(String payload) {
    JWEAlgorithm alg = JWEAlgorithm.A128KW;
    EncryptionMethod encryptionMethod = EncryptionMethod.A128GCM;

    try {
        byte[] decodedKey = Base64.getDecoder().decode(encodedKeypair);
        SecretKey key = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
        JWEObject jwe = new JWEObject(
                new JWEHeader(alg, encryptionMethod),
                new Payload(payload));
        jwe.encrypt(new AESEncrypter(key));
        return jwe.serialize();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:PacktPublishing,项目名称:OAuth-2.0-Cookbook,代码行数:18,代码来源:JweTokenSerializer.java

示例3: encrypt

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public String encrypt(String data, String keyId, String aesKey) {

        try {
            byte[] keyBytes = Base64.decode(aesKey);
            SecretKeySpec secretKey = new SecretKeySpec(keyBytes, 0, keyBytes.length, "AES");

            JWEAlgorithm jweAlgorithm = JWEAlgorithm.A256KW;
            EncryptionMethod encryptionMethod = EncryptionMethod.A128GCM;
            JWEHeader.Builder headerBuilder = new JWEHeader.Builder(jweAlgorithm, encryptionMethod);

            headerBuilder.keyID(keyId);

            JWEHeader header = headerBuilder.build();
            JWEEncrypter encrypter = new AESEncrypter(secretKey);
            encrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance());
            JWEObject jweObject = new JWEObject(header, new Payload(data));
            jweObject.encrypt(encrypter);
            return jweObject.serialize();
        } catch (Exception e) {
            throw new CryptoException("Exception encrypting data: " + e.getMessage(), e);
        }
    }
 
开发者ID:americanexpress,项目名称:amex-api-java-client-core,代码行数:23,代码来源:EncryptionUtility.java

示例4: sign

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public String sign(String algorithm, String kid, String keyStr, String dataToSign) {
    try {

        Key key = getKey(algorithm, keyStr);

        JWSHeader.Builder jwsBuilder = new JWSHeader.Builder("HS256".equals(algorithm) ? JWSAlgorithm.HS256 : JWSAlgorithm.RS256);
        jwsBuilder.keyID(kid);

        JWSHeader signingHeader = jwsBuilder.build();
        JWSSigner signer = "HS256".equals(algorithm) ? new MACSigner(key.getEncoded()) : new RSASSASigner((RSAPrivateKey) key);
        JWSObject jwsObject = new JWSObject(signingHeader, new Payload(dataToSign));
        jwsObject.sign(signer);
        checkObject(jwsObject);

        String parts[] = jwsObject.serialize().split("\\.");

        return "{\"protected\":\"" + parts[0] + "\", \"payload\":\"" + parts[1] + "\", \"signature\":\"" + parts[2] + "\"}";

    } catch (Exception e) {
        throw new CryptoException("Exception signing data: " + e.getMessage(), e);
    }
}
 
开发者ID:americanexpress,项目名称:amex-api-java-client-core,代码行数:23,代码来源:EncryptionUtility.java

示例5: getSubject

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public static String getSubject( String token )
{
    String subject = null;
    try
    {
        Payload payload = parseToken( token );
        JSONObject obj = payload.toJSONObject();
        subject = obj.get( "sub" ).toString();
    }
    catch ( Exception e )
    {
        LOG.error( "Error parsing token", e.getMessage() );
    }

    return subject;
}
 
开发者ID:subutai-io,项目名称:base,代码行数:17,代码来源:TokenUtil.java

示例6: createTokenRSA

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public static String createTokenRSA( PrivateKey privateKey, String claimJson )
{
    try
    {
        JWSSigner signer = new RSASSASigner( ( RSAPrivateKey ) privateKey );

        Payload pl = new Payload( claimJson );
        JWSObject jwsObject = new JWSObject( new JWSHeader( JWSAlgorithm.RS256 ), pl );

        jwsObject.sign( signer );

        return jwsObject.serialize();
    }
    catch ( Exception e )
    {
        LOG.error( "Error creating RSA token", e.getMessage() );

        return "";
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:21,代码来源:TokenUtil.java

示例7: verifyTokenRSA

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public static boolean verifyTokenRSA( PublicKey pKey, String token )
{
    try
    {
        Payload pl = new Payload( token );
        JWSObject jwsObject = new JWSObject( new JWSHeader( JWSAlgorithm.RS256 ), pl );
        JWSVerifier verifier = new RSASSAVerifier( ( RSAPublicKey ) pKey );

        return jwsObject.verify( verifier );
    }
    catch ( JOSEException e )
    {
        LOG.warn( "Error verifying RSA token", e.getMessage() );

        return false;
    }
}
 
开发者ID:subutai-io,项目名称:base,代码行数:18,代码来源:TokenUtil.java

示例8: createToken

import com.nimbusds.jose.Payload; //导入依赖的package包/类
default String createToken(Object userId) {
    try {
        JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();

        builder.issuer(getIssuer());
        builder.subject(userId.toString());
        builder.issueTime(new Date());
        builder.notBeforeTime(new Date());
        builder.expirationTime(new Date(new Date().getTime() + getExpirationDate()));
        builder.jwtID(UUID.randomUUID().toString());

        JWTClaimsSet claimsSet = builder.build();
        JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);

        Payload payload = new Payload(claimsSet.toJSONObject());

        JWSObject jwsObject = new JWSObject(header, payload);

        JWSSigner signer = new MACSigner(getSharedKey());
        jwsObject.sign(signer);
        return jwsObject.serialize();
    } catch (JOSEException ex) {
        return null;
    }
}
 
开发者ID:panchitoboy,项目名称:shiro-jwt,代码行数:26,代码来源:UserRepository.java

示例9: validToken

import com.nimbusds.jose.Payload; //导入依赖的package包/类
@Test
public void validToken() throws JOSEException, ParseException {
    JWTClaimsSet jwtClaims = getJWTClaimsSet("issuer", "subject", new Date(), new Date(), new Date(new Date().getTime() + 100000));

    JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);

    Payload payload = new Payload(jwtClaims.toJSONObject());

    JWSObject jwsObject = new JWSObject(header, payload);

    JWSSigner signer = new MACSigner(sharedKey);
    jwsObject.sign(signer);
    String token = jwsObject.serialize();

    SignedJWT signed = SignedJWT.parse(token);
    JWSVerifier verifier = new MACVerifierExtended(sharedKey, signed.getJWTClaimsSet());
    signed.verify(verifier);

    Assert.assertTrue("Must be valid", signed.verify(verifier));
}
 
开发者ID:panchitoboy,项目名称:shiro-jwt,代码行数:21,代码来源:MACVerifierExtendedTest.java

示例10: invalidTokenNotBeforeTime

import com.nimbusds.jose.Payload; //导入依赖的package包/类
@Test
public void invalidTokenNotBeforeTime() throws JOSEException, ParseException {
    JWTClaimsSet jwtClaims = getJWTClaimsSet("issuer", "subject", new Date(), new Date(new Date().getTime() + 100000), new Date(new Date().getTime() + 200000));

    JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);

    Payload payload = new Payload(jwtClaims.toJSONObject());

    JWSObject jwsObject = new JWSObject(header, payload);

    JWSSigner signer = new MACSigner(sharedKey);
    jwsObject.sign(signer);
    String token = jwsObject.serialize();

    SignedJWT signed = SignedJWT.parse(token);
    JWSVerifier verifier = new MACVerifierExtended(sharedKey, signed.getJWTClaimsSet());
    signed.verify(verifier);

    Assert.assertFalse("Must be invalid", signed.verify(verifier));
}
 
开发者ID:panchitoboy,项目名称:shiro-jwt,代码行数:21,代码来源:MACVerifierExtendedTest.java

示例11: invalidTokenExpirationTime

import com.nimbusds.jose.Payload; //导入依赖的package包/类
@Test
public void invalidTokenExpirationTime() throws JOSEException, ParseException {
    JWTClaimsSet jwtClaims = getJWTClaimsSet("issuer", "subject", new Date(), new Date(), new Date());

    JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);

    Payload payload = new Payload(jwtClaims.toJSONObject());

    JWSObject jwsObject = new JWSObject(header, payload);

    JWSSigner signer = new MACSigner(sharedKey);
    jwsObject.sign(signer);
    String token = jwsObject.serialize();

    SignedJWT signed = SignedJWT.parse(token);
    JWSVerifier verifier = new MACVerifierExtended(sharedKey, signed.getJWTClaimsSet());
    signed.verify(verifier);

    Assert.assertFalse("Must be invalid", signed.verify(verifier));
}
 
开发者ID:panchitoboy,项目名称:shiro-jwt,代码行数:21,代码来源:MACVerifierExtendedTest.java

示例12: encryptAndSign

import com.nimbusds.jose.Payload; //导入依赖的package包/类
@Override
public String encryptAndSign(String jwtSubject) {
    try {
        Date date = Date.from(ZonedDateTime.now(ZoneOffset.UTC).plusSeconds(jwtExpirationSeconds).toInstant());
        JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject(jwtSubject).expirationTime(date).build();
        SignedJWT signedJWT = new SignedJWT(new JWSHeader(jwsAlgorithm), claimsSet);
        signedJWT.sign(new MACSigner(sessionJwtEncryptionKey));
        JWEHeader jweHeader = new JWEHeader.Builder(jweAlgorithm, encryptionMethod).contentType("JWT").build();
        JWEObject jweObject = new JWEObject(jweHeader, new Payload(signedJWT));
        jweObject.encrypt(new DirectEncrypter(sessionJwtEncryptionKey));
        return jweObject.serialize();
    } catch (JOSEException e) {
        throw new RuntimeException("Could not create JWT", e);
    }
}
 
开发者ID:AusDTO,项目名称:spring-security-stateless,代码行数:16,代码来源:JwtEncryption.java

示例13: createToken

import com.nimbusds.jose.Payload; //导入依赖的package包/类
/**
 * 生成token,该方法只在用户登录成功后调用
 * 
 * @param Map集合,可以存储用户id,token生成时间,token过期时间等自定义字段
 * @return token字符串,若失败则返回null
 */
public static String createToken(Map<String, Object> payload) {
	String tokenString=null;
	// 创建一个 JWS object
	JWSObject jwsObject = new JWSObject(header, new Payload(new JSONObject(payload)));
	try {
		// 将jwsObject 进行HMAC签名
		jwsObject.sign(new MACSigner(SECRET));
		tokenString=jwsObject.serialize();
	} catch (JOSEException e) {
		System.err.println("签名失败:" + e.getMessage());
		e.printStackTrace();
	}
	return tokenString;
}
 
开发者ID:bigmeow,项目名称:JWT,代码行数:21,代码来源:Jwt.java

示例14: validToken

import com.nimbusds.jose.Payload; //导入依赖的package包/类
/**
    * 校验token是否合法,返回Map集合,集合中主要包含    state状态码   data鉴权成功后从token中提取的数据
    * 该方法在过滤器中调用,每次请求API时都校验
    * @param token
    * @return  Map<String, Object>
    */
public static Map<String, Object> validToken(String token) {
	Map<String, Object> resultMap = new HashMap<String, Object>();
	try {
		JWSObject jwsObject = JWSObject.parse(token);
		Payload payload = jwsObject.getPayload();
		JWSVerifier verifier = new MACVerifier(SECRET);

		if (jwsObject.verify(verifier)) {
			JSONObject jsonOBj = payload.toJSONObject();
			// token校验成功(此时没有校验是否过期)
			resultMap.put("state", TokenState.VALID.toString());
			// 若payload包含ext字段,则校验是否过期
			if (jsonOBj.containsKey("ext")) {
				long extTime = Long.valueOf(jsonOBj.get("ext").toString());
				long curTime = new Date().getTime();
				// 过期了
				if (curTime > extTime) {
					resultMap.clear();
					resultMap.put("state", TokenState.EXPIRED.toString());
				}
			}
			resultMap.put("data", jsonOBj);

		} else {
			// 校验失败
			resultMap.put("state", TokenState.INVALID.toString());
		}

	} catch (Exception e) {
		//e.printStackTrace();
		// token格式不合法导致的异常
		resultMap.clear();
		resultMap.put("state", TokenState.INVALID.toString());
	}
	return resultMap;
}
 
开发者ID:bigmeow,项目名称:JWT,代码行数:43,代码来源:Jwt.java

示例15: parseToken

import com.nimbusds.jose.Payload; //导入依赖的package包/类
public static Payload parseToken( JWSObject jwsObject )
{
    Payload payload = null;
    try
    {
        payload = jwsObject.getPayload();
    }
    catch ( Exception e )
    {
        LOG.error( "Error parsing token", e.getMessage() );
    }

    return payload;
}
 
开发者ID:subutai-io,项目名称:base,代码行数:15,代码来源:TokenUtil.java


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