本文整理匯總了Java中org.jose4j.jwt.JwtClaims.setSubject方法的典型用法代碼示例。如果您正苦於以下問題:Java JwtClaims.setSubject方法的具體用法?Java JwtClaims.setSubject怎麽用?Java JwtClaims.setSubject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.jose4j.jwt.JwtClaims
的用法示例。
在下文中一共展示了JwtClaims.setSubject方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateJWTAssertion
import org.jose4j.jwt.JwtClaims; //導入方法依賴的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: createDefaultClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
/**
* Create a JwtClaims object with prefilled sane defaults.
* @return JwtClaims
*/
private JwtClaims createDefaultClaims() {
JwtClaims claims = new JwtClaims();
claims.setIssuer(SSODataTest.DATA_ISSUER); // who creates the token and signs it
claims.setAudience(SSODataTest.DATA_AUDIENCE); // to whom the token is intended to be sent
claims.setExpirationTimeMinutesInTheFuture(10); // time when the token will expire (10 minutes from now)
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setSubject(SSODataTest.DATA_USER_ID); // the subject/principal is whom the token is about
claims.setClaim(SSOData.KEY_INSTANCE_ID, SSODataTest.DATA_INSTANCE_ID); // additional claims/attributes about the subject can be added
return claims;
}
示例3: createToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Override
public String createToken(final String to) {
try {
final String token = UUID.randomUUID().toString();
final JwtClaims claims = new JwtClaims();
claims.setJwtId(token);
claims.setIssuer(issuer);
claims.setAudience(issuer);
claims.setExpirationTimeMinutesInTheFuture(passwordManagementProperties.getReset().getExpirationMinutes());
claims.setIssuedAtToNow();
final ClientInfo holder = ClientInfoHolder.getClientInfo();
claims.setStringClaim("origin", holder.getServerIpAddress());
claims.setStringClaim("client", holder.getClientIpAddress());
claims.setSubject(to);
final String json = claims.toJson();
return this.cipherExecutor.encode(json);
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return null;
}
示例4: generateToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
public String generateToken(String subject) {
final JwtClaims claims = new JwtClaims();
claims.setSubject(subject);
claims.setExpirationTimeMinutesInTheFuture(TOKEN_EXPIRATION_IN_MINUTES);
final JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setAlgorithmHeaderValue(HMAC_SHA256);
jws.setKey(new HmacKey(tokenSecret));
jws.setDoKeyValidation(false); //relaxes hmac key length restrictions
try {
return jws.getCompactSerialization();
} catch (JoseException e) {
throw new RuntimeException(e);
}
}
示例5: createJwtClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private static JwtClaims createJwtClaims(
List<String> audiences,
String email,
int expiration,
String subject,
int notBefore,
String issuer) {
JwtClaims jwtClaims = new JwtClaims();
jwtClaims.setAudience(audiences);
jwtClaims.setExpirationTimeMinutesInTheFuture(expiration);
jwtClaims.setClaim("email", email);
jwtClaims.setSubject(subject);
jwtClaims.setNotBeforeMinutesInThePast(notBefore);
jwtClaims.setIssuer(issuer);
return jwtClaims;
}
示例6: createToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private static String createToken(Key key, JsonObject jsonClaims) {
JwtClaims claims = new JwtClaims();
claims.setSubject(jsonClaims.toString());
claims.setIssuedAtToNow();
claims.setExpirationTime(NumericDate.fromSeconds(NumericDate.now().getValue() + JWT_TOKEN_EXPIRES_TIME));
JsonWebSignature jws = new JsonWebSignature();
jws.setDoKeyValidation(false);
jws.setPayload(claims.toJson());
jws.setKey(key);
jws.setAlgorithmHeaderValue(ALG);
try {
return jws.getCompactSerialization();
} catch (JoseException ex) {
LOGGER.log(Level.SEVERE, null, ex);
}
return null;
}
示例7: testNpeWithNonExtractableKeyDataHS256
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@Test
public void testNpeWithNonExtractableKeyDataHS256() throws Exception
{
byte[] raw = Base64Url.decode("hup76LcA9B7pqrEtqyb4EBg6XCcr9r0iOCFF1FeZiJM");
FakeHsmNonExtractableSecretKeySpec key = new FakeHsmNonExtractableSecretKeySpec(raw, "HmacSHA256");
JwtClaims claims = new JwtClaims();
claims.setExpirationTimeMinutesInTheFuture(5);
claims.setSubject("subject");
claims.setIssuer("issuer");
JsonWebSignature jws = new JsonWebSignature();
jws.setPayload(claims.toJson());
jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.HMAC_SHA256);
jws.setKey(key);
String jwt = jws.getCompactSerialization();
JwtConsumerBuilder jwtConsumerBuilder = new JwtConsumerBuilder();
jwtConsumerBuilder.setAllowedClockSkewInSeconds(60);
jwtConsumerBuilder.setRequireSubject();
jwtConsumerBuilder.setExpectedIssuer("issuer");
jwtConsumerBuilder.setVerificationKey(key);
JwtConsumer jwtConsumer = jwtConsumerBuilder.build();
JwtClaims processedClaims = jwtConsumer.processToClaims(jwt);
System.out.println(processedClaims);
}
示例8: littleJweRoundTrip
import org.jose4j.jwt.JwtClaims; //導入方法依賴的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"));
}
示例9: signToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的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.jwt.JwtClaims; //導入方法依賴的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: createToken
import org.jose4j.jwt.JwtClaims; //導入方法依賴的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);
}
}
示例12: createMalformedClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
/**
* Create a JwtClaims object with prefilled sane defaults and missing mandatory claims.
* @return JwtClaims
*/
private JwtClaims createMalformedClaims() {
JwtClaims claims = new JwtClaims();
claims.setIssuer(SSODataTest.DATA_ISSUER); // who creates the token and signs it
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setSubject(SSODataTest.DATA_USER_ID); // the subject/principal is whom the token is about
claims.setClaim(SSOData.KEY_INSTANCE_ID, SSODataTest.DATA_INSTANCE_ID); // additional claims/attributes about the subject can be added
return claims;
}
示例13: getClaimsForUser
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private static JwtClaims getClaimsForUser(String user) {
final JwtClaims claims = new JwtClaims();
claims.setExpirationTimeMinutesInTheFuture(5);
claims.setSubject(user);
claims.setIssuer("stroom");
return claims;
}
示例14: getJwtClaims
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
private JwtClaims getJwtClaims() {
JwtClaims claims = new JwtClaims();
claims.setIssuer("Issuer"); // who creates the token and signs it
claims.setAudience("Audience"); // to whom the token is intended to be sent
claims.setExpirationTimeMinutesInTheFuture(10); // time when the token will expire (10 minutes from now)
claims.setGeneratedJwtId(); // a unique identifier for the token
claims.setIssuedAtToNow(); // when the token was issued/created (now)
claims.setNotBeforeMinutesInThePast(2); // time before which the token is not yet valid (2 minutes ago)
claims.setSubject("subject"); // the subject/principal is whom the token is about
claims.setClaim("email", "[email protected]"); // additional claims/attributes about the subject can be added
return claims;
}
示例15: json
import org.jose4j.jwt.JwtClaims; //導入方法依賴的package包/類
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(@ApiResponse(code = 401,
message = "Unauthorized Response",
response = ErrorResponse.class))
public Response json(final UsernamePassword usernamePassword,
@HeaderParam(HttpHeaders.AUTHORIZATION) final String authorization) {
if (!"password".equals(usernamePassword.getPassword())) {
throw ErrorResponses.unauthorized(ErrorCodes.UNAUTHORIZED_CLIENT, "invalid username/password combination", "FORM");
}
final JwtClaims claims = new JwtClaims();
claims.setSubject(usernamePassword.getUsername());
claims.setAudience(HttpAuthorizationHeaders.parseBasicAuthorization(authorization)[0]);
final Form form = new Form();
form.param("grant_type", GrantTypes.JWT_ASSERTION);
form.param("assertion", cryptoOps.sign(claims));
return Response.ok(client.target(authorizationEndpoint).request(MediaType.APPLICATION_JSON_TYPE)
.header(HttpHeaders.AUTHORIZATION, authorization)
.post(Entity.form(form), OAuthTokenResponse.class))
.build();
}