本文整理汇总了Java中org.jose4j.jwe.JsonWebEncryption类的典型用法代码示例。如果您正苦于以下问题:Java JsonWebEncryption类的具体用法?Java JsonWebEncryption怎么用?Java JsonWebEncryption使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonWebEncryption类属于org.jose4j.jwe包,在下文中一共展示了JsonWebEncryption类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createJWT
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
/**
* Encrypt the otp to be send via mail
*/
@Override
public String createJWT(String userid, long ttlMillis) {
Key key = new AesKey(ConfigUtil.get(JWTKEY).getBytes());
JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setKey(key);
jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A128KW);
jwe.setEncryptionMethodHeaderParameter(
ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);
jwe.setPayload(userid + "&&" + ttlMillis);
try {
return jwe.getCompactSerialization();
} catch (JoseException e) {
xLogger.warn("Unable to get the jwt service: {0}", e.getMessage());
}
return null;
}
示例2: encryptIdToken
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
private String encryptIdToken(final OidcRegisteredService svc, final JsonWebSignature jws, final String innerJwt) throws Exception {
LOGGER.debug("Service [{}] is set to encrypt id tokens", svc);
final JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setAlgorithmHeaderValue(svc.getIdTokenEncryptionAlg());
jwe.setEncryptionMethodHeaderParameter(svc.getIdTokenEncryptionEncoding());
final Optional<RsaJsonWebKey> jwks = this.serviceJsonWebKeystoreCache.get(svc);
if (!jwks.isPresent()) {
throw new IllegalArgumentException("Service " + svc.getServiceId()
+ " with client id " + svc.getClientId()
+ " is configured to encrypt id tokens, yet no JSON web key is available");
}
final RsaJsonWebKey jsonWebKey = jwks.get();
LOGGER.debug("Found JSON web key to encrypt the id token: [{}]", jsonWebKey);
if (jsonWebKey.getPublicKey() == null) {
throw new IllegalArgumentException("JSON web key used to sign the id token has no associated public key");
}
jwe.setKey(jsonWebKey.getPublicKey());
jwe.setKeyIdHeaderValue(jws.getKeyIdHeaderValue());
jwe.setContentTypeHeaderValue("JWT");
jwe.setPayload(innerJwt);
return jwe.getCompactSerialization();
}
示例3: encryptValue
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
/**
* Encrypt the value based on the seed array whose length was given during afterPropertiesSet,
* and the key and content encryption ids.
*
* @param value the value
* @return the encoded value
*/
private String encryptValue(final Serializable value) {
try {
final JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setPayload(serializeValue(value));
jwe.enableDefaultCompression();
jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.DIRECT);
jwe.setEncryptionMethodHeaderParameter(this.contentEncryptionAlgorithmIdentifier);
jwe.setKey(this.secretKeyEncryptionKey);
LOGGER.debug("Encrypting via [{}]", this.contentEncryptionAlgorithmIdentifier);
return jwe.getCompactSerialization();
} catch (final Exception e) {
throw new RuntimeException("Ensure that you have installed JCE Unlimited Strength Jurisdiction Policy Files. "
+ e.getMessage(), e);
}
}
示例4: decryptJWT
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
/**
* Decrypt the otp received via mail
*/
@Override
public String decryptJWT(String token) {
JsonWebEncryption jwe = new JsonWebEncryption();
Key key = new AesKey(ConfigUtil.get(JWTKEY).getBytes());
jwe.setKey(key);
jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.A128KW);
jwe.setEncryptionMethodHeaderParameter(
ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);
try {
jwe.setCompactSerialization(token);
return jwe.getPayload();
} catch (JoseException e) {
xLogger.warn("Unable to get the jwt service: {0}", e.getMessage());
}
jwe.setKey(key);
return null;
}
示例5: verifyJWE
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
private void verifyJWE(Key key, String keyAlgo, String jweAlgo, String jwt) {
try {
JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setAlgorithmConstraints(
new AlgorithmConstraints(
ConstraintType.WHITELIST,
ALGORITHMS.get(keyAlgo)));
jwe.setContentEncryptionAlgorithmConstraints(
new AlgorithmConstraints(
ConstraintType.WHITELIST,
ALGORITHMS.get(jweAlgo)));
jwe.setCompactSerialization(jwt);
jwe.setKey(key);
JwtClaims claims = JwtClaims.parse(jwe.getPlaintextString());
assertEquals("abc xyz", claims.getSubject());
assertEquals("[email protected]", claims.getClaimValue("email"));
} catch (Exception e) {
fail();
}
}
示例6: littleJweRoundTrip
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
private void littleJweRoundTrip(String alg, String enc, String b64uKey) throws Exception
{
byte[] raw = Base64Url.decode(b64uKey);
Key key = new FakeHsmNonExtractableSecretKeySpec(raw, "AES");
JwtClaims claims = new JwtClaims();
claims.setExpirationTimeMinutesInTheFuture(5);
claims.setSubject("subject");
claims.setIssuer("issuer");
JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setPayload(claims.toJson());
jwe.setAlgorithmHeaderValue(alg);
jwe.setEncryptionMethodHeaderParameter(enc);
jwe.setKey(key);
String jwt = jwe.getCompactSerialization();
JwtConsumerBuilder jwtConsumerBuilder = new JwtConsumerBuilder();
jwtConsumerBuilder.setAllowedClockSkewInSeconds(60);
jwtConsumerBuilder.setRequireSubject();
jwtConsumerBuilder.setExpectedIssuer("issuer");
jwtConsumerBuilder.setDecryptionKey(key);
jwtConsumerBuilder.setDisableRequireSignature();
JwtConsumer jwtConsumer = jwtConsumerBuilder.build();
JwtClaims processedClaims = jwtConsumer.processToClaims(jwt);
Assert.assertThat(processedClaims.getSubject(), equalTo("subject"));
}
示例7: jwe1
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
@Test
public void jwe1() throws JoseException
{
String cs = "eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2Iiwia2lkIjoiOWVyIn0." +
"." +
"XAog2l7TP5-0mIPYjT2ZYg." +
"Zf6vQZhxeAfzk2AyuXsKJSo1R8aluPDvK7a6N7wvSmuIUczDhUtJFmNdXC3d4rPa." +
"XBTguLfGeGKu6YsQVnes2w";
JsonWebStructure jwx = JsonWebStructure.fromCompactSerialization(cs);
jwx.setKey(oct256bitJwk.getKey());
Assert.assertTrue(cs + " should give a JWE " + jwx, jwx instanceof JsonWebEncryption);
Assert.assertEquals(KeyManagementAlgorithmIdentifiers.DIRECT, jwx.getAlgorithmHeaderValue());
Assert.assertEquals(oct256bitJwk.getKeyId(), jwx.getKeyIdHeaderValue());
String payload = jwx.getPayload();
Assert.assertEquals(YOU_LL_GET_NOTHING_AND_LIKE_IT, payload);
}
示例8: jwe2
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
@Test
public void jwe2() throws JoseException
{
String cs = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMTI4Q0JDLUhTMjU2Iiwia2lkIjoiOWVyIn0." +
"RAqGCBMFk7O-B-glFckcFmxUr8BTTXuZk-bXAdRZxpk5Vgs_1yoUQw." +
"hyl68_ADlK4VRDYiQMQS6w." +
"xk--JKIVF4Xjxc0gRGPL30s4PSNtj685WYqXbjyItG0uSffD4ajGXdz4BO8i0sbM." +
"WXaAVpBgftXyO1HkkRvgQQ";
JsonWebStructure jwx = JsonWebStructure.fromCompactSerialization(cs);
jwx.setKey(oct256bitJwk.getKey());
Assert.assertTrue(cs + " should give a JWE " + jwx, jwx instanceof JsonWebEncryption);
Assert.assertEquals(KeyManagementAlgorithmIdentifiers.A256KW, jwx.getAlgorithmHeaderValue());
Assert.assertEquals(oct256bitJwk.getKeyId(), jwx.getKeyIdHeaderValue());
String payload = jwx.getPayload();
Assert.assertEquals(YOU_LL_GET_NOTHING_AND_LIKE_IT, payload);
}
示例9: createToken
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
@NotNull
public static String createToken(@NotNull JsonWebEncryption jwe, @NotNull User user, @NotNull NumericDate expireAt) {
try {
JwtClaims claims = new JwtClaims();
claims.setExpirationTime(expireAt);
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(0.5f); // time before which the token is not yet valid (30 seconds ago)
if (!user.isAnonymous()) {
claims.setSubject(user.getUserName()); // the subject/principal is whom the token is about
setClaim(claims, "email", user.getEmail());
setClaim(claims, "name", user.getRealName());
setClaim(claims, "external", user.getExternalId());
}
jwe.setPayload(claims.toJson());
return jwe.getCompactSerialization();
} catch (JoseException e) {
throw new IllegalStateException(e);
}
}
示例10: encrypt
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
@Override public String encrypt(String data, PublicKey publicKey, String keyId, String contentType) throws JWEFailure {
String encrypted;
JsonWebEncryption jwe = new JsonWebEncryption();
try {
jwe.setKey(publicKey);
jwe.setPlaintext(data);
jwe.setKeyIdHeaderValue(keyId);
jwe.setContentTypeHeaderValue(contentType);
jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.RSA_OAEP_256);
jwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_256_CBC_HMAC_SHA_512);
encrypted = jwe.getCompactSerialization();
} catch (JoseException e) {
throw new JWEFailure("An error occurred attempting to encrypt a JWE", e);
}
return encrypted;
}
示例11: getHeaders
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
@Override
public Map<String, String> getHeaders(String data) throws JWEFailure {
try {
String headerSegment = data.split("\\.")[0];
JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setCompactSerialization(data);
String headersJSON = jwe.getHeaders().getFullHeaderAsJsonString();
Map<String, Object> objectMap = JsonUtil.parseJson(headersJSON);
Map<String, String> headers = new LinkedHashMap<>(objectMap.size());
for (Map.Entry<String, Object> entry : objectMap.entrySet()) {
headers.put(entry.getKey(), String.valueOf(entry.getValue()));
}
return headers;
} catch (Exception e) {
throw new JWEFailure("Unable to parse data for JWE Header!", e);
}
}
示例12: encryptValue
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
/**
* Encrypt the value based on the seed array whose length was given during afterPropertiesSet,
* and the key and content encryption ids.
*
* @param value the value
* @return the encoded value
*/
private String encryptValue(@NotNull final String value) {
try {
final JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setPayload(value);
jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.DIRECT);
jwe.setEncryptionMethodHeaderParameter(this.contentEncryptionAlgorithmIdentifier);
jwe.setKey(this.secretKeyEncryptionKey);
logger.debug("Encrypting via [{}]", this.contentEncryptionAlgorithmIdentifier);
return jwe.getCompactSerialization();
} catch (final Exception e) {
throw new RuntimeException("Ensure that you have installed JCE Unlimited Strength Jurisdiction Policy Files. "
+ e.getMessage(), e);
}
}
示例13: decryptValue
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
/**
* Decrypt value based on the key created during afterPropertiesSet.
*
* @param value the value
* @return the decrypted value
*/
private String decryptValue(@NotNull final String value) {
try {
final JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setKey(this.secretKeyEncryptionKey);
jwe.setCompactSerialization(value);
logger.debug("Decrypting value...");
return jwe.getPayload();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
示例14: encryptValue
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
/**
* Encrypt the value based on the seed array whose length was given during init,
* and the key and content encryption ids.
*
* @param value the value
* @return the encoded value
*/
private String encryptValue(@NotNull final String value) {
try {
final JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setPayload(value);
jwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.DIRECT);
jwe.setEncryptionMethodHeaderParameter(this.contentEncryptionAlgorithmIdentifier);
jwe.setKey(this.secretKeyEncryptionKey);
logger.debug("Encrypting via [{}]", this.contentEncryptionAlgorithmIdentifier);
return jwe.getCompactSerialization();
} catch (final Exception e) {
throw new RuntimeException("Ensure that you have installed JCE Unlimited Strength Jurisdiction Policy Files. "
+ e.getMessage(), e);
}
}
示例15: decryptValue
import org.jose4j.jwe.JsonWebEncryption; //导入依赖的package包/类
/**
* Decrypt value based on the key created during init.
*
* @param value the value
* @return the decrypted value
*/
private String decryptValue(@NotNull final String value) {
try {
final JsonWebEncryption jwe = new JsonWebEncryption();
jwe.setKey(this.secretKeyEncryptionKey);
jwe.setCompactSerialization(value);
logger.debug("Decrypting value...");
return jwe.getPayload();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}