本文整理汇总了Java中org.jose4j.jws.JsonWebSignature.setKeyIdHeaderValue方法的典型用法代码示例。如果您正苦于以下问题:Java JsonWebSignature.setKeyIdHeaderValue方法的具体用法?Java JsonWebSignature.setKeyIdHeaderValue怎么用?Java JsonWebSignature.setKeyIdHeaderValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jose4j.jws.JsonWebSignature
的用法示例。
在下文中一共展示了JsonWebSignature.setKeyIdHeaderValue方法的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;
}
}
示例2: prepareJsonWebSignatureForIdTokenSigning
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
private void prepareJsonWebSignatureForIdTokenSigning(final OidcRegisteredService svc, final JsonWebSignature jws,
final RsaJsonWebKey jsonWebKey) {
LOGGER.debug("Service [{}] is set to sign id tokens", svc);
jws.setKey(jsonWebKey.getPrivateKey());
jws.setAlgorithmConstraints(AlgorithmConstraints.DISALLOW_NONE);
if (StringUtils.isBlank(jsonWebKey.getKeyId())) {
jws.setKeyIdHeaderValue(UUID.randomUUID().toString());
} else {
jws.setKeyIdHeaderValue(jsonWebKey.getKeyId());
}
LOGGER.debug("Signing id token with key id header value [{}]", jws.getKeyIdHeaderValue());
jws.setAlgorithmHeaderValue(getJsonWebKeySigningAlgorithm());
LOGGER.debug("Signing id token with algorithm [{}]", jws.getAlgorithmHeaderValue());
}
示例3: 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);
}
}
示例4: 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();
}
示例5: uniqueKidTestFRJwksEndpoint
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
@Test
public void uniqueKidTestFRJwksEndpoint() throws JoseException
{
// JSON content from https://demo.forgerock.com:8443/openam/oauth2/connect/jwk_uri on Jan 8, 2015
String json = "{\"keys\":[{\"kty\":\"RSA\",\"kid\":\"fb301b61-9b8a-4c34-9212-5d6fb9df1a57\",\"use\":\"sig\",\"alg\":\"RS256\",\"n\":\"AK0kHP1O-RgdgLSoWxkuaYoi5Jic6hLKeuKw8WzCfsQ68ntBDf6tVOTn_kZA7Gjf4oJAL1dXLlxIEy-kZWnxT3FF-0MQ4WQYbGBfaW8LTM4uAOLLvYZ8SIVEXmxhJsSlvaiTWCbNFaOfiII8bhFp4551YB07NfpquUGEwOxOmci_\",\"e\":\"AQAB\"}]}";
JsonWebKeySet jwks = new JsonWebKeySet(json);
VerificationJwkSelector verificationJwkSelector = new VerificationJwkSelector();
JsonWebSignature jws = new JsonWebSignature();
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
jws.setKeyIdHeaderValue("fb301b61-9b8a-4c34-9212-5d6fb9df1a57");
List<JsonWebKey> jsonWebKeys = jwks.getJsonWebKeys();
List<JsonWebKey> selected = verificationJwkSelector.selectList(jws, jsonWebKeys);
assertThat(1, equalTo(selected.size()));
assertThat("fb301b61-9b8a-4c34-9212-5d6fb9df1a57", equalTo(selected.get(0).getKeyId()));
}
示例6: uniqueKidTestMiterJwksEndpoint
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
@Test
public void uniqueKidTestMiterJwksEndpoint() throws JoseException
{
// JSON content from https://mitreid.org/jwk on Jan 8, 2015
String json = "{\"keys\":[{\"alg\":\"RS256\",\"e\":\"AQAB\",\"n\":\"23zs5r8PQKpsKeoUd2Bjz3TJkUljWqMD8X98SaIb1LE7dCQzi9jwO58FGL0ieY1Dfnr9-g1iiY8sNzV-byawK98W9yFiopaghfoKtxXgUD8pi0fLPeWmAkntjn28Z_WZvvA265ELbBhphPXEJcFhdzUfgESHVuqFMEqp1pB-CP0\"," +
"\"kty\":\"RSA\",\"kid\":\"rsa1\"}]}";
JsonWebKeySet jwks = new JsonWebKeySet(json);
VerificationJwkSelector verificationJwkSelector = new VerificationJwkSelector();
JsonWebSignature jws = new JsonWebSignature();
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
jws.setKeyIdHeaderValue("rsa1");
List<JsonWebKey> jsonWebKeys = jwks.getJsonWebKeys();
List<JsonWebKey> selected = verificationJwkSelector.selectList(jws, jsonWebKeys);
assertThat(1, equalTo(selected.size()));
assertThat("rsa1", equalTo(selected.get(0).getKeyId()));
}
示例7: uniqueKidTestNriPhpJwksEndpoint
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
@Test
public void uniqueKidTestNriPhpJwksEndpoint() throws JoseException
{
// JSON content from https://connect.openid4.us/connect4us.jwk on Jan 8, 2015
String json = "{\n" +
" \"keys\":[\n" +
" {\n" +
" \"kty\":\"RSA\",\n" +
" \"n\":\"tf_sB4M0sHearRLzz1q1JRgRdRnwk0lz-IcVDFlpp2dtDVyA-ZM8Tu1swp7upaTNykf7cp3Ne_6uW3JiKvRMDdNdvHWCzDHmbmZWGdnFF9Ve-D1cUxj4ETVpUM7AIXWbGs34fUNYl3Xzc4baSyvYbc3h6iz8AIdb_1bQLxJsHBi-ydg3NMJItgQJqBiwCmQYCOnJlekR-Ga2a5XlIx46Wsj3Pz0t0dzM8gVSU9fU3QrKKzDFCoFHTgig1YZNNW5W2H6QwANL5h-nbgre5sWmDmdnfiU6Pj5GOQDmp__rweinph8OAFNF6jVqrRZ3QJEmMnO42naWOsxV2FAUXafksQ\",\n" +
" \"e\":\"AQAB\",\n" +
" \"kid\":\"ABOP-00\"\n" +
" }\n" +
" ]\n" +
"}\n";
JsonWebKeySet jwks = new JsonWebKeySet(json);
VerificationJwkSelector verificationJwkSelector = new VerificationJwkSelector();
JsonWebSignature jws = new JsonWebSignature();
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA384);
jws.setKeyIdHeaderValue("ABOP-00");
List<JsonWebKey> jsonWebKeys = jwks.getJsonWebKeys();
List<JsonWebKey> selected = verificationJwkSelector.selectList(jws, jsonWebKeys);
assertThat(1, equalTo(selected.size()));
assertThat("ABOP-00", equalTo(selected.get(0).getKeyId()));
}
示例8: testAnEx
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
@Test
public void testAnEx() throws Exception
{
String location = "https://www.example.org/";
Get mockGet = mock(Get.class);
when(mockGet.get(location)).thenThrow(new IOException(location + "says 'no GET for you!'"));
HttpsJwks httpsJkws = new HttpsJwks(location);
httpsJkws.setSimpleHttpGet(mockGet);
HttpsJwksVerificationKeyResolver resolver = new HttpsJwksVerificationKeyResolver(httpsJkws);
JsonWebSignature jws = new JsonWebSignature();
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256);
jws.setKeyIdHeaderValue("nope");
try
{
Key key = resolver.resolveKey(jws, Collections.<JsonWebStructure>emptyList());
fail("shouldn't have resolved a key but got " + key);
}
catch (UnresolvableKeyException e)
{
log.debug("this was expected and is okay: {}", e.toString());
}
}
示例9: 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();
}
示例10: 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();
}
示例11: generateAuthToken
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
* Generate an auth token with the given claims and sign the token with the
* private key in the provided {@link RsaJsonWebKey}.
*/
public static String generateAuthToken(
Optional<Collection<String>> audiences,
Optional<String> email,
NumericDate expirationTime,
Optional<String> issuer,
NumericDate notBefore,
Optional<String> subject,
RsaJsonWebKey rsaJsonWebKey) {
JwtClaims claims = new JwtClaims();
if (audiences.isPresent()) {
claims.setAudience(ImmutableList.copyOf(audiences.get()));
}
if (email.isPresent()) {
claims.setClaim("email", email.get());
}
if (issuer.isPresent()) {
claims.setIssuer(issuer.get());
}
if (subject.isPresent()) {
claims.setSubject(subject.get());
}
claims.setExpirationTime(expirationTime);
claims.setNotBefore(notBefore);
JsonWebSignature jsonWebSignature = new JsonWebSignature();
jsonWebSignature.setPayload(claims.toJson());
jsonWebSignature.setKey(rsaJsonWebKey.getPrivateKey());
jsonWebSignature.setKeyIdHeaderValue(rsaJsonWebKey.getKeyId());
jsonWebSignature.setAlgorithmHeaderValue(rsaJsonWebKey.getAlgorithm());
try {
return jsonWebSignature.getCompactSerialization();
} catch (JoseException exception) {
throw new RuntimeException("failed to generate JWT", exception);
}
}
示例12: getJwt
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
/**
* A static method that generate JWT token from JWT claims object
*
* @param claims JwtClaims object
* @return A string represents jwt token
* @throws JoseException JoseException
*/
public static String getJwt(JwtClaims claims) throws JoseException {
String jwt;
RSAPrivateKey privateKey = (RSAPrivateKey) getPrivateKey(
jwtConfig.getKey().getFilename(), jwtConfig.getKey().getPassword(), jwtConfig.getKey().getKeyName());
// A JWT is a JWS and/or a JWE with JSON claims as the payload.
// In this example it is a JWS nested inside a JWE
// So we first 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 sender's private key
jws.setKey(privateKey);
jws.setKeyIdHeaderValue(jwtConfig.getKey().getKid());
// Set the signature algorithm on the JWT/JWS that will integrity protect the claims
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
// Sign the JWS and produce the compact serialization, which will be the inner JWT/JWS
// representation, which is a string consisting of three dot ('.') separated
// base64url-encoded parts in the form Header.Payload.Signature
jwt = jws.getCompactSerialization();
return jwt;
}
示例13: getJwt
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
public static String getJwt(JwtClaims claims) throws JoseException {
String jwt;
RSAPrivateKey privateKey = (RSAPrivateKey) getPrivateKey(
"/config/oauth/primary.jks", "password", "selfsigned");
// A JWT is a JWS and/or a JWE with JSON claims as the payload.
// In this example it is a JWS nested inside a JWE
// So we first 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 sender's private key
jws.setKey(privateKey);
jws.setKeyIdHeaderValue("100");
// Set the signature algorithm on the JWT/JWS that will integrity protect the claims
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.RSA_USING_SHA256);
// Sign the JWS and produce the compact serialization, which will be the inner JWT/JWS
// representation, which is a string consisting of three dot ('.') separated
// base64url-encoded parts in the form Header.Payload.Signature
jwt = jws.getCompactSerialization();
return jwt;
}
示例14: createJwt
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
private String createJwt(String subject) throws Exception {
JwtClaims claims = new JwtClaims();
claims.setSubject(subject);
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(jsonWebKey.getKey());
jws.setKeyIdHeaderValue(jsonWebKey.getKeyId());
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
return jws.getCompactSerialization();
}
示例15: payload2token
import org.jose4j.jws.JsonWebSignature; //导入方法依赖的package包/类
public String payload2token(String subject, Object payload, long minutes) throws JoseException {
JwtClaims claims = new JwtClaims();
claims.setIssuer(TOKEN_ISSUER);
claims.setAudience(TOKEN_AUDIENCE);
claims.setExpirationTimeMinutesInTheFuture(minutes);
claims.setGeneratedJwtId();
claims.setIssuedAtToNow();
claims.setNotBeforeMinutesInThePast(2);
claims.setSubject(subject);
claims.setStringClaim(CLAIM_KEY, jsonHelper.object2json(payload));
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setKey(getKey());
jws.setKeyIdHeaderValue(KEY_ID);
jws.setAlgorithmHeaderValue(getAlgorithm());
String token = jws.getCompactSerialization();
if (!tokenService.contains(token)) {
tokenService.store(token, null); //todo
}
return token;
}