本文整理汇总了Java中com.nimbusds.jose.JWSObject.sign方法的典型用法代码示例。如果您正苦于以下问题:Java JWSObject.sign方法的具体用法?Java JWSObject.sign怎么用?Java JWSObject.sign使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.nimbusds.jose.JWSObject
的用法示例。
在下文中一共展示了JWSObject.sign方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSignedContent
import com.nimbusds.jose.JWSObject; //导入方法依赖的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);
}
}
示例2: sign
import com.nimbusds.jose.JWSObject; //导入方法依赖的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);
}
}
示例3: createTokenRSA
import com.nimbusds.jose.JWSObject; //导入方法依赖的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 "";
}
}
示例4: createToken
import com.nimbusds.jose.JWSObject; //导入方法依赖的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;
}
}
示例5: validToken
import com.nimbusds.jose.JWSObject; //导入方法依赖的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));
}
示例6: invalidTokenNotBeforeTime
import com.nimbusds.jose.JWSObject; //导入方法依赖的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));
}
示例7: invalidTokenExpirationTime
import com.nimbusds.jose.JWSObject; //导入方法依赖的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));
}
示例8: createToken
import com.nimbusds.jose.JWSObject; //导入方法依赖的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;
}
示例9: service
import com.nimbusds.jose.JWSObject; //导入方法依赖的package包/类
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
if (shared_key == null || subdomain == null)
throw new ServletException("Zendesk is not configured.");
// Given a user instance
// Compose the JWT claims set
JWTClaimsSet jwtClaims = new JWTClaimsSet();
jwtClaims.setIssueTime(new Date());
jwtClaims.setJWTID(UUID.randomUUID().toString());
Subject subject = EnvironmentContext.getCurrent().getSubject();
jwtClaims.setCustomClaim("name", getName());
jwtClaims.setCustomClaim("email", subject.getUserName());
// Create JWS header with HS256 algorithm
JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
JWSObject jwsObject = new JWSObject(header, new Payload(jwtClaims.toJSONObject()));
// Create HMAC signer
JWSSigner signer = new MACSigner(shared_key.getBytes());
try {
jwsObject.sign(signer);
} catch (JOSEException e) {
String msg = String.format("Error signing JWT: %s", e.getMessage());
LOG.warn(msg);
response.sendError(500, msg);
}
// Serialise to JWT compact form
String jwtString = jwsObject.serialize();
String redirectUrl = "https://" + subdomain + ".zendesk.com/access/jwt?jwt=" + jwtString;
response.sendRedirect(redirectUrl);
}
示例10: encodeJWT
import com.nimbusds.jose.JWSObject; //导入方法依赖的package包/类
private static String encodeJWT(JsonObject payload) {
try {
ECPrivateKey privatekey = (ECPrivateKey) keypair.getPrivate();
JWSObject jwsobject = new JWSObject(
new JWSHeader.Builder(JWSAlgorithm.ES384).x509CertURL(new URI(Base64.getEncoder().encodeToString(privatekey.getEncoded()))).build(),
new Payload(Utils.GSON.toJson(payload))
);
jwsobject.sign(new ECDSASigner(privatekey));
return jwsobject.serialize();
} catch (Exception e) {
throw new EncoderException("Unable to encode jwt", e);
}
}
开发者ID:ProtocolSupport,项目名称:ProtocolSupportBungee,代码行数:14,代码来源:LoginRequestServerHandshakePacket.java
示例11: newJWSObject
import com.nimbusds.jose.JWSObject; //导入方法依赖的package包/类
public static JWSObject newJWSObject(JWTUser user) throws JOSEException {
JWSSigner signer = new MACSigner(JWT.SHARED_SECRET);
JWSObject jwsObject = new JWSObject(new JWSHeader(JWSAlgorithm.HS256), new Payload(user));
jwsObject.sign(signer);
return jwsObject;
}
示例12: newJwtToken
import com.nimbusds.jose.JWSObject; //导入方法依赖的package包/类
private JWSObject newJwtToken(String kid,String role) throws Exception {
RSAKey rsaKey = RSAKey.parse(jsonFile("jwk/private_key.json"));
JWSSigner signer = new RSASSASigner(rsaKey);
JWSObject jwsObject = new JWSObject(
new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(kid).build(),
new Payload(jsonFile("jwk/payload-" + role + ".json")));
jwsObject.sign(signer);
return jwsObject;
}
开发者ID:IxorTalk,项目名称:ixortalk.aws.cognito.jwt.security.filter,代码行数:15,代码来源:AwsCognitoIdTokenProcessorTest.java