本文整理汇总了Java中com.nimbusds.jose.JWSObject类的典型用法代码示例。如果您正苦于以下问题:Java JWSObject类的具体用法?Java JWSObject怎么用?Java JWSObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JWSObject类属于com.nimbusds.jose包,在下文中一共展示了JWSObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: getJWTUser
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
public static JWTUser getJWTUser(String token) throws JWTException {
if (StringUtils.isEmpty(token)) {
throw new JWTException("没有找到token信息!");
}
try {
JWSObject jwsObject = JWSObject.parse(token);
if (JWT.verify(jwsObject)) {
// 判断有效期,不在有效期内则直接抛出错误
JWTUser user = new JWTUser(jwsObject.getPayload().toJSONObject());
if (user.getExp() >= Calendar.getInstance().getTimeInMillis()) {
return user;
} else {
throw new JWTException("token已经超过有效期!");
}
} else {
throw new JWTException("token校验失败!");
}
} catch (Exception e) {
throw new JWTException(e);
}
}
示例3: validateSignature
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
/**
* Verify the signature of the JWT token in this method. This method depends
* on the public key that was established during init based upon the
* provisioned public key. Override this method in subclasses in order to
* customize the signature verification behavior.
*
* @param jwtToken the token that contains the signature to be validated
* @return valid true if signature verifies successfully; false otherwise
*/
protected boolean validateSignature(SignedJWT jwtToken) {
boolean valid = false;
if (JWSObject.State.SIGNED == jwtToken.getState()) {
LOG.debug("JWT token is in a SIGNED state");
if (jwtToken.getSignature() != null) {
LOG.debug("JWT token signature is not null");
try {
JWSVerifier verifier = new RSASSAVerifier(publicKey);
if (jwtToken.verify(verifier)) {
valid = true;
LOG.debug("JWT token has been successfully verified");
} else {
LOG.warn("JWT signature verification failed.");
}
} catch (JOSEException je) {
LOG.warn("Error while validating signature", je);
}
}
}
return valid;
}
示例4: 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);
}
}
示例5: checkObjectFailure
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
@Test
public void checkObjectFailure() throws Exception {
Method method = encryptionUtility.getClass().getDeclaredMethod("checkObject", JWSObject.class);
method.setAccessible(true);
JWSObject object = mock(JWSObject.class);
Throwable exception = null;
try {
method.invoke(encryptionUtility, object);
} catch(InvocationTargetException e) {
exception = e.getCause();
}
assertNotNull(exception);
assertTrue(exception instanceof CryptoException);
}
示例6: verifySignature
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
public static boolean verifySignature( String token, String sharedKey )
{
boolean verifiedSignature = false;
try
{
JWSObject jwsObject = JWSObject.parse( token );
JWSVerifier verifier = new MACVerifier( sharedKey.getBytes() );
verifiedSignature = jwsObject.verify( verifier );
}
catch ( Exception e )
{
LOG.warn( e.getMessage() );
}
return verifiedSignature;
}
示例7: 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 "";
}
}
示例8: verifyTokenRSA
import com.nimbusds.jose.JWSObject; //导入依赖的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;
}
}
示例9: validateSignature
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
protected boolean validateSignature(SignedJWT jwtToken) {
boolean valid = false;
if (JWSObject.State.SIGNED == jwtToken.getState()) {
if (jwtToken.getSignature() != null) {
try {
RSAPublicKey publicKey = parseRSAPublicKey(publicKeyPath);
JWSVerifier verifier = new RSASSAVerifier(publicKey);
if (verifier != null && jwtToken.verify(verifier)) {
valid = true;
}
} catch (Exception e) {
LOGGER.info("Exception in validateSignature", e);
}
}
}
return valid;
}
示例10: extractChainData
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
private static Any<Key, JsonObject> extractChainData(Map<String, List<String>> maindata) throws ParseException {
List<String> chain = maindata.get("chain");
try {
PublicKey key = parseKey(MOJANG_KEY);
boolean foundMojangKey = false;
boolean signatureValid = false;
for (String element : chain) {
JWSObject jwsobject = JWSObject.parse(element);
if (!foundMojangKey && jwsobject.getHeader().getX509CertURL().toString().equals(MOJANG_KEY)) {
foundMojangKey = true;
signatureValid = true;
}
if (foundMojangKey && !verify(jwsobject, key)) {
signatureValid = false;
}
JsonObject jsonobject = Utils.GSON.fromJson(jwsobject.getPayload().toString(), JsonObject.class);
key = parseKey(JsonUtils.getString(jsonobject, "identityPublicKey"));
if (jsonobject.has("extraData")) {
return new Any<Key, JsonObject>(signatureValid ? key : null, JsonUtils.getJsonObject(jsonobject, "extraData"));
}
}
} catch (InvalidKeySpecException | JOSEException e) {
throw new DecoderException("Unable to decode login chain", e);
}
throw new DecoderException("Unable to find extraData");
}
示例11: authenticate
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
public JwtClaims authenticate(Jwt jwt) throws JwtVerifyException {
JWSObject jwsObject = jwt.getJwsObject();
JSONObject payload = jwsObject.getPayload().toJSONObject();
// Do the verification steps in order of performance cost / likelihood of failing. Signature verification is
// costly so we do that last.
assertTimeValid(payload);
X509Certificate signingCertificate = getSigningCertificate(jwsObject);
assertAllowedSignerDn(signingCertificate);
assertJwtIssuerMatchesSigner(signingCertificate, payload.get(JwtClaims.ISSUER).toString());
assertSignatureValid(jwsObject, signingCertificate);
//
// IMPORTANT!
//
// You need to assert that signing certificate is valid by walking trust chain. This will depend on
// your CA hierarchy and revocation policy. This has been left for the implementer to decide but is a
// critical part of the verification process.
LOGGER.debug("Verified JWT (jti={})", payload.get(JwtClaims.JWT_ID));
JwtClaims.JwtClaimsBuilder builder = JwtClaims.JwtClaimsBuilder.newInstanceFromClaimsMap(payload);
return builder.build();
}
示例12: 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;
}
}
示例13: createToken
import com.nimbusds.jose.JWSObject; //导入依赖的package包/类
public JWTAuthenticationToken createToken(String token) {
try {
JWSObject jwsObject = JWSObject.parse(token);
String decrypted = jwsObject.getPayload().toString();
try (JsonReader jr = Json.createReader(new StringReader(decrypted))) {
JsonObject object = jr.readObject();
String userId = object.getString("sub", null);
return new JWTAuthenticationToken(userId, token);
}
} catch (ParseException ex) {
throw new AuthenticationException(ex);
}
}
示例14: 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));
}
示例15: 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));
}