本文整理匯總了Java中com.nimbusds.jwt.SignedJWT.serialize方法的典型用法代碼示例。如果您正苦於以下問題:Java SignedJWT.serialize方法的具體用法?Java SignedJWT.serialize怎麽用?Java SignedJWT.serialize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.nimbusds.jwt.SignedJWT
的用法示例。
在下文中一共展示了SignedJWT.serialize方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateCookieBody
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
String generateCookieBody(int secondsToLive) {
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
JWSSigner signer = new RSASSASigner(privateKey);
DateTime expDate = new DateTime((new Date()).getTime() + secondsToLive * 1000);
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.issuer("digital-display-garden")
.claim("exp", expDate.toString())
.build();
SignedJWT signedJWT = new SignedJWT(
new JWSHeader(JWSAlgorithm.RS256),
claimsSet
);
try {
signedJWT.sign(signer);
return signedJWT.serialize();
} catch (JOSEException e) {
e.printStackTrace();
return "";
}
}
示例2: generateSharedGoogleSecret
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
String generateSharedGoogleSecret(String originatingURL) {
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
JWSSigner signer = new RSASSASigner(privateKey);
// Expire in 60 seconds
DateTime expDate = new DateTime((new Date()).getTime() + 60 * 1000);
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.issuer("digital-display-garden")
.claim("originatingURL", originatingURL)
.claim("exp", expDate.toString())
.build();
SignedJWT signedJWT = new SignedJWT(
new JWSHeader(JWSAlgorithm.RS256),
claimsSet
);
try {
signedJWT.sign(signer);
return signedJWT.serialize();
} catch (JOSEException e) {
e.printStackTrace();
return "";
}
}
示例3: createToken
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
public static String createToken( String headerJson, String claimJson, String sharedKey )
{
try
{
JWSHeader header = JWSHeader.parse( headerJson );
JWSSigner signer = new MACSigner( sharedKey.getBytes() );
JWTClaimsSet claimsSet = JWTClaimsSet.parse( claimJson );
SignedJWT signedJWT = new SignedJWT( header, claimsSet );
signedJWT.sign( signer );
return signedJWT.serialize();
}
catch ( Exception e )
{
LOG.error( "Error creating token", e.getMessage() );
return "";
}
}
示例4: selfIssue
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
public String selfIssue() {
JWSSigner signer = new RSASSASigner((RSAPrivateKey) keyPair.getPrivate());
List<String> aud = new ArrayList<String>();
aud.add(Constants.POYNT_API_HOST);
JWTClaimsSet claimsSet = new JWTClaimsSet();
claimsSet.setAudience(aud);
claimsSet.setSubject(config.getAppId());
claimsSet.setIssuer(config.getAppId());
Calendar now = Calendar.getInstance();
claimsSet.setIssueTime(now.getTime());
now.add(Calendar.MINUTE, 15);
claimsSet.setExpirationTime(now.getTime());
claimsSet.setJWTID(UUID.randomUUID().toString());
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.RS256), claimsSet);
try {
signedJWT.sign(signer);
} catch (JOSEException e) {
throw new PoyntSdkException("Failed to sign self issued JWT.");
}
return signedJWT.serialize();
}
示例5: generateJWT
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
private String generateJWT(final String username) throws JOSEException {
// Create HMAC signer
final JWSSigner signer = new MACSigner(secret);
// Prepare JWT with claims set
final JWTClaimsSet claimsSet = new JWTClaimsSet.Builder().subject(username).expirationTime(new Date(new Date().getTime() + 60 * 1000)).claim("http://localhost:8080/", true).build();
final SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
// Apply the HMAC protection
signedJWT.sign(signer);
// Serialize to compact form, produces something like
// eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NDMzODA1NDAsInN1YiI6ImNpZGlhbiIsImh0dHA6XC9cL2xvY2FsaG9zdDo4MDgwXC8iOnRydWV9.EkPxd0EfujgLrk35DX1XmvnmyJsFO8dqbnzsgg78coM
return signedJWT.serialize();
}
示例6: getJwt
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
private static String getJwt(String subject, String issuer, String secret,
int expiresInSeconds) throws JOSEException {
JWSSigner signer = new MACSigner(secret.getBytes());
JWTClaimsSet claimsSet = new JWTClaimsSet();
claimsSet.setSubjectClaim(subject);
claimsSet.setIssuedAtClaim(new Date().getTime());
claimsSet.setIssuerClaim(issuer);
claimsSet.setExpirationTimeClaim(new Date().getTime()
+ (expiresInSeconds * 1000));
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256),
claimsSet);
signedJWT.sign(signer);
String jwt = signedJWT.serialize();
return jwt;
}
示例7: signJWT
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
private static String signJWT(String uid, PrivateKey privateKey) {
final JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).type(JOSEObjectType.JWT).build();
final JWTClaimsSet payload = new JWTClaimsSet.Builder().claim("uid", uid).build();
final SignedJWT signedJWT = new SignedJWT(header, payload);
try {
signedJWT.sign(new RSASSASigner(privateKey));
return signedJWT.serialize();
} catch (JOSEException e) {
throw new RuntimeException(e);
}
}
示例8: createToken
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
private static String createToken(Info info) {
JWTClaimsSet.Builder claimsSetBuilder = new JWTClaimsSet.Builder();
claimsSetBuilder.subject(info.getUserName());
claimsSetBuilder.issueTime(new Date());
claimsSetBuilder.expirationTime(new Date(new Date().getTime() + 30 * 1000));
JSONArray roleValues = new JSONArray();
roleValues.addAll(info.getRoles());
Map<String, Object> roles = new HashMap<>();
roles.put("roles", roleValues);
claimsSetBuilder.claim("realm_access", roles);
JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS512).type(JOSEObjectType.JWT).keyID(info.getApiKey()).build();
SignedJWT signedJWT = new SignedJWT(header, claimsSetBuilder.build());
System.out.println(claimsSetBuilder.build().toJSONObject().toString(JSONStyle.NO_COMPRESS));
try {
JWSSigner signer = new RSASSASigner((RSAKey) jwkSet.getKeyByKeyId(info.getApiKey()));
signedJWT.sign(signer);
} catch (JOSEException e) {
// Should not happen
e.printStackTrace();
}
return signedJWT.serialize();
}
示例9: jwtMAC
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
/**
* JWT
* 使用HMAC SHA-256 進行加密 與 解密
* 基於相同的 secret (對稱算法)
* <p/>
* 算法 Secret長度
* HS256 32
* HS384 64
* HS512 64
*
* @throws Exception
*/
@Test
public void jwtMAC() throws Exception {
String sharedSecret = RandomStringUtils.random(64, true, true);
JWSSigner jwsSigner = new MACSigner(sharedSecret);
//生成idToken
final String payloadText = "I am MyOIDC";
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.subject("subject")
.issuer("https://andaily.com")
.claim("payloadText", payloadText)
.expirationTime(new Date(new Date().getTime() + 60 * 1000))
.build();
// final JWSHeader header = new JWSHeader(JWSAlgorithm.HS256);
// final JWSHeader header = new JWSHeader(JWSAlgorithm.HS384);
final JWSHeader header = new JWSHeader(JWSAlgorithm.HS512);
SignedJWT signedJWT = new SignedJWT(header, claimsSet);
signedJWT.sign(jwsSigner);
final String idToken = signedJWT.serialize();
//校驗idToken
final SignedJWT parseJWT = SignedJWT.parse(idToken);
JWSVerifier jwsVerifier = new MACVerifier(sharedSecret);
final boolean verify = parseJWT.verify(jwsVerifier);
assertTrue(verify);
// final Payload payload = parseJWT.getPayload();
final JWTClaimsSet jwtClaimsSet = parseJWT.getJWTClaimsSet();
assertEquals(jwtClaimsSet.getSubject(), "subject");
}
示例10: createToken
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
public Token createToken(String host, Cidadao cidadao) throws JOSEException {
JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder()
.subject(cidadao.getId().toString()).issuer(host)
.issueTime(DateTime.now().toDate())
.expirationTime(DateTime.now().plusDays(14).toDate());
JWSSigner signer = new MACSigner(tokenSecret);
SignedJWT jwt = new SignedJWT(jwtHeader, builder.build());
jwt.sign(signer);
return new Token(jwt.serialize());
}
示例11: testNoPublicKeyJWT
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
@Test
public void testNoPublicKeyJWT() throws Exception {
try {
Properties props = getProperties();
handler.init(props);
SignedJWT jwt = getJWT("bob", new Date(new Date().getTime() + 5000),
privateKey);
Cookie cookie = new Cookie("hadoop-jwt", jwt.serialize());
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getCookies()).thenReturn(new Cookie[] { cookie });
Mockito.when(request.getRequestURL()).thenReturn(
new StringBuffer(SERVICE_URL));
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.when(response.encodeRedirectURL(SERVICE_URL)).thenReturn(
SERVICE_URL);
AuthenticationToken token = handler.alternateAuthenticate(request,
response);
fail("alternateAuthentication should have thrown a ServletException");
} catch (ServletException se) {
assertTrue(se.getMessage().contains(
"Public key for signature validation must be provisioned"));
} catch (AuthenticationException ae) {
fail("alternateAuthentication should NOT have thrown a AuthenticationException");
}
}
示例12: testCustomCookieNameJWT
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
@Test
public void testCustomCookieNameJWT() throws Exception {
try {
handler.setPublicKey(publicKey);
Properties props = getProperties();
props.put(JWTRedirectAuthenticationHandler.JWT_COOKIE_NAME, "jowt");
handler.init(props);
SignedJWT jwt = getJWT("bob", new Date(new Date().getTime() + 5000),
privateKey);
Cookie cookie = new Cookie("jowt", jwt.serialize());
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getCookies()).thenReturn(new Cookie[] { cookie });
Mockito.when(request.getRequestURL()).thenReturn(
new StringBuffer(SERVICE_URL));
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.when(response.encodeRedirectURL(SERVICE_URL)).thenReturn(
SERVICE_URL);
AuthenticationToken token = handler.alternateAuthenticate(request,
response);
Assert.assertEquals("bob", token.getUserName());
} catch (ServletException se) {
fail("alternateAuthentication should NOT have thrown a ServletException: "
+ se.getMessage());
} catch (AuthenticationException ae) {
fail("alternateAuthentication should NOT have thrown a AuthenticationException");
}
}
示例13: testNoProviderURLJWT
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
@Test
public void testNoProviderURLJWT() throws Exception {
try {
handler.setPublicKey(publicKey);
Properties props = getProperties();
props
.remove(JWTRedirectAuthenticationHandler.AUTHENTICATION_PROVIDER_URL);
handler.init(props);
SignedJWT jwt = getJWT("bob", new Date(new Date().getTime() + 5000),
privateKey);
Cookie cookie = new Cookie("hadoop-jwt", jwt.serialize());
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getCookies()).thenReturn(new Cookie[] { cookie });
Mockito.when(request.getRequestURL()).thenReturn(
new StringBuffer(SERVICE_URL));
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.when(response.encodeRedirectURL(SERVICE_URL)).thenReturn(
SERVICE_URL);
AuthenticationToken token = handler.alternateAuthenticate(request,
response);
fail("alternateAuthentication should have thrown an AuthenticationException");
} catch (ServletException se) {
assertTrue(se.getMessage().contains(
"Authentication provider URL must not be null"));
} catch (AuthenticationException ae) {
fail("alternateAuthentication should NOT have thrown a AuthenticationException");
}
}
示例14: testUnableToParseJWT
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
@Test
public void testUnableToParseJWT() throws Exception {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic();
handler.setPublicKey(publicKey);
Properties props = getProperties();
handler.init(props);
SignedJWT jwt = getJWT("bob", new Date(new Date().getTime() + 5000),
privateKey);
Cookie cookie = new Cookie("hadoop-jwt", "ljm" + jwt.serialize());
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getCookies()).thenReturn(new Cookie[] { cookie });
Mockito.when(request.getRequestURL()).thenReturn(
new StringBuffer(SERVICE_URL));
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.when(response.encodeRedirectURL(SERVICE_URL)).thenReturn(
SERVICE_URL);
AuthenticationToken token = handler.alternateAuthenticate(request,
response);
Mockito.verify(response).sendRedirect(REDIRECT_LOCATION);
} catch (ServletException se) {
fail("alternateAuthentication should NOT have thrown a ServletException");
} catch (AuthenticationException ae) {
fail("alternateAuthentication should NOT have thrown a AuthenticationException");
}
}
示例15: testFailedSignatureValidationJWT
import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
@Test
public void testFailedSignatureValidationJWT() throws Exception {
try {
// Create a public key that doesn't match the one needed to
// verify the signature - in order to make it fail verification...
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.genKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic();
handler.setPublicKey(publicKey);
Properties props = getProperties();
handler.init(props);
SignedJWT jwt = getJWT("bob", new Date(new Date().getTime() + 5000),
privateKey);
Cookie cookie = new Cookie("hadoop-jwt", jwt.serialize());
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getCookies()).thenReturn(new Cookie[] { cookie });
Mockito.when(request.getRequestURL()).thenReturn(
new StringBuffer(SERVICE_URL));
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.when(response.encodeRedirectURL(SERVICE_URL)).thenReturn(
SERVICE_URL);
AuthenticationToken token = handler.alternateAuthenticate(request,
response);
Mockito.verify(response).sendRedirect(REDIRECT_LOCATION);
} catch (ServletException se) {
fail("alternateAuthentication should NOT have thrown a ServletException");
} catch (AuthenticationException ae) {
fail("alternateAuthentication should NOT have thrown a AuthenticationException");
}
}