當前位置: 首頁>>代碼示例>>Java>>正文


Java SignedJWT.sign方法代碼示例

本文整理匯總了Java中com.nimbusds.jwt.SignedJWT.sign方法的典型用法代碼示例。如果您正苦於以下問題:Java SignedJWT.sign方法的具體用法?Java SignedJWT.sign怎麽用?Java SignedJWT.sign使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.nimbusds.jwt.SignedJWT的用法示例。


在下文中一共展示了SignedJWT.sign方法的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 "";
    }
}
 
開發者ID:UMM-CSci-3601-S17,項目名稱:digital-display-garden-iteration-4-dorfner-v2,代碼行數:24,代碼來源:Auth.java

示例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 "";
    }
}
 
開發者ID:UMM-CSci-3601-S17,項目名稱:digital-display-garden-iteration-4-dorfner-v2,代碼行數:26,代碼來源:Auth.java

示例3: getJWT

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
protected SignedJWT getJWT(String sub, Date expires, RSAPrivateKey privateKey)
    throws Exception {
  JWTClaimsSet claimsSet = new JWTClaimsSet();
  claimsSet.setSubject(sub);
  claimsSet.setIssueTime(new Date(new Date().getTime()));
  claimsSet.setIssuer("https://c2id.com");
  claimsSet.setCustomClaim("scope", "openid");
  claimsSet.setExpirationTime(expires);
  List<String> aud = new ArrayList<String>();
  aud.add("bar");
  claimsSet.setAudience("bar");

  JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).build();

  SignedJWT signedJWT = new SignedJWT(header, claimsSet);
  Base64URL sigInput = Base64URL.encode(signedJWT.getSigningInput());
  JWSSigner signer = new RSASSASigner(privateKey);

  signedJWT.sign(signer);

  return signedJWT;
}
 
開發者ID:aliyun-beta,項目名稱:aliyun-oss-hadoop-fs,代碼行數:23,代碼來源:TestJWTRedirectAuthentictionHandler.java

示例4: getIdToken

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
protected JWT getIdToken(@Nonnull ClientID clientId, @Nullable Nonce nonce, @Nullable AccessTokenHash atHash,
		@Nullable CodeHash cHash) throws GeneralSecurityException, JOSEException, ParseException {
	JWTClaimsSet claims = getIdTokenClaims(clientId, nonce, atHash, cHash);

	RSAKey key = getSigningJwk();

	JWSHeader.Builder headerBuilder = new JWSHeader.Builder(JWSAlgorithm.RS256)
			.type(JOSEObjectType.JWT);
	if (params.getBool(INCLUDE_SIGNING_CERT)) {
		headerBuilder = headerBuilder.jwk(key.toPublicJWK());
	}
	JWSHeader header = headerBuilder.build();

	SignedJWT signedJwt = new SignedJWT(header, claims);

	JWSSigner signer = new RSASSASigner(key);
	signedJwt.sign(signer);

	return signedJwt;
}
 
開發者ID:RUB-NDS,項目名稱:PrOfESSOS,代碼行數:21,代碼來源:AbstractOPImplementation.java

示例5: createEmptyJWTwithPublicKey

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
/**
 * creates an empty JSON Web Token
 * 
 * @param webAppBaseURL - the base url of the application
 * 
 * @return the JSON WebToken
 */
public static SignedJWT createEmptyJWTwithPublicKey(String webAppBaseURL) {

    ZonedDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC);
    JWTClaimsSet claims = new JWTClaimsSet.Builder().issuer(webAppBaseURL).jwtID(UUID.randomUUID().toString())
        .issueTime(Date.from(currentTime.toInstant())).build();
    String keyID = UUID.randomUUID().toString();
    JWK jwk = new RSAKey.Builder((RSAPublicKey) RSA_KEYS.getPublic()).keyID(keyID).build();
    JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).jwk(jwk).build();
    SignedJWT signedJWT = new SignedJWT(jwsHeader, claims);
    try {
        signedJWT.sign(new RSASSASigner(RSA_KEYS.getPrivate()));
    } catch (JOSEException e) {
        LOGGER.error(e);
    }
    return signedJWT;

}
 
開發者ID:MyCoRe-Org,項目名稱:mycore,代碼行數:25,代碼來源:MCRJSONWebTokenUtil.java

示例6: createJWT

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
/**
 * creates a JSON Web Token with user id, roles and client public key
 * 
 * @param user - the user that should be returned
 * @param roles - the roles that should be returned
 * @param webAppBaseURL - the base url of the application
 * @param clientPublicKey -  the client public key as JSON Web Key
 * 
 * @return the JSON WebToken
 */
public static SignedJWT createJWT(String user, List<String> roles, String webAppBaseURL, JWK clientPublicKey) {
    ZonedDateTime currentTime = ZonedDateTime.now(ZoneOffset.UTC);
    JWTClaimsSet claims = new JWTClaimsSet.Builder().issuer(webAppBaseURL).jwtID(UUID.randomUUID().toString())
        .expirationTime(Date.from(currentTime.plusMinutes(EXPIRATION_TIME_MINUTES).toInstant()))
        .issueTime(Date.from(currentTime.toInstant()))
        .notBeforeTime(Date.from(currentTime.minusMinutes(EXPIRATION_TIME_MINUTES).toInstant())).subject(user)
        // additional claims/attributes about the subject can be added
        // claims.setClaim("email", "[email protected]");
        // multi-valued claims work too and will end up as a JSON array
        .claim("roles", roles).claim("sub_jwk", clientPublicKey).build();

    String keyID = UUID.randomUUID().toString();
    JWK jwk = new RSAKey.Builder((RSAPublicKey) RSA_KEYS.getPublic()).keyID(keyID).build();
    JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256).jwk(jwk).build();
    SignedJWT signedJWT = new SignedJWT(jwsHeader, claims);
    try {
        signedJWT.sign(new RSASSASigner(RSA_KEYS.getPrivate()));
    } catch (JOSEException e) {
        // TODO Auto-generated catch block
        LOGGER.error(e);
    }
    System.out.println("JWT: " + signedJWT.serialize());
    return signedJWT;
}
 
開發者ID:MyCoRe-Org,項目名稱:mycore,代碼行數:35,代碼來源:MCRJSONWebTokenUtil.java

示例7: 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 "";
    }
}
 
開發者ID:subutai-io,項目名稱:base,代碼行數:21,代碼來源:TokenUtil.java

示例8: 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();
}
 
開發者ID:poynt,項目名稱:java-cloud-sdk,代碼行數:26,代碼來源:JsonWebToken.java

示例9: getJWT

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
protected SignedJWT getJWT(String sub, Date expires, RSAPrivateKey privateKey)
    throws Exception {
  JWTClaimsSet claimsSet = new JWTClaimsSet();
  claimsSet.setSubject(sub);
  claimsSet.setIssueTime(new Date(new Date().getTime()));
  claimsSet.setIssuer("https://c2id.com");
  claimsSet.setCustomClaim("scope", "openid");
  claimsSet.setExpirationTime(expires);
  List<String> aud = new ArrayList<String>();
  aud.add("bar");
  claimsSet.setAudience("bar");

  JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256).build();

  SignedJWT signedJWT = new SignedJWT(header, claimsSet);
  JWSSigner signer = new RSASSASigner(privateKey);

  signedJWT.sign(signer);

  return signedJWT;
}
 
開發者ID:hopshadoop,項目名稱:hops,代碼行數:22,代碼來源:TestJWTRedirectAuthentictionHandler.java

示例10: 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();
}
 
開發者ID:Sylvain-Bugat,項目名稱:swagger-cxf-rest-skeleton,代碼行數:17,代碼來源:AuthenticationTokenService.java

示例11: 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;
}
 
開發者ID:zackbleach,項目名稱:jwt-gen,代碼行數:19,代碼來源:Generator.java

示例12: generateJWToken

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
/**
 * Generates a new JWT token.
 * @param user a User object belonging to the app
 * @param app the app object
 * @return a new JWT or null
 */
public static SignedJWT generateJWToken(User user, App app) {
	if (app != null) {
		try {
			Date now = new Date();
			JWTClaimsSet.Builder claimsSet = new JWTClaimsSet.Builder();
			String userSecret = "";
			claimsSet.issueTime(now);
			claimsSet.expirationTime(new Date(now.getTime() + (app.getTokenValiditySec() * 1000)));
			claimsSet.notBeforeTime(now);
			claimsSet.claim("refresh", getNextRefresh(app.getTokenValiditySec()));
			claimsSet.claim(Config._APPID, app.getId());
			if (user != null) {
				claimsSet.subject(user.getId());
				userSecret = user.getTokenSecret();
			}
			JWSSigner signer = new MACSigner(app.getSecret() + userSecret);
			SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet.build());
			signedJWT.sign(signer);
			return signedJWT;
		} catch (JOSEException e) {
			logger.warn("Unable to sign JWT: {}.", e.getMessage());
		}
	}
	return null;
}
 
開發者ID:Erudika,項目名稱:para,代碼行數:32,代碼來源:SecurityUtils.java

示例13: sign

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
public SignedJWT sign(JWTClaimsSet claimsSet) {
	JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.RS256) //
			.keyID(getKey().get("kid"))//
			.type(JOSEObjectType.JWT) //
			.build();
	SignedJWT signedJWT = new SignedJWT(header, claimsSet);
	try {
		signedJWT.sign(signer);
	}
	catch (JOSEException e) {
		throw new IllegalStateException(e);
	}
	return signedJWT;
}
 
開發者ID:making,項目名稱:spring-boot-actuator-dashboard,代碼行數:15,代碼來源:JwtTokenConverter.java

示例14: testHS256

import com.nimbusds.jwt.SignedJWT; //導入方法依賴的package包/類
@Test(groups = TCKConstants.TEST_GROUP_DEBUG,
    description = "Validate how to use the HS256 signature alg")
public void testHS256() throws Exception {
    JWTClaimsSet claimsSet = JWTClaimsSet.parse("{\"sub\":\"jdoe\"}");
    SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
    SecureRandom random = new SecureRandom();
    BigInteger secret = BigInteger.probablePrime(256, random);
    JWSSigner signer = new MACSigner(secret.toByteArray());
    signedJWT.sign(signer);
}
 
開發者ID:eclipse,項目名稱:microprofile-jwt-auth,代碼行數:11,代碼來源:DebugTest.java

示例15: 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);
    }
}
 
開發者ID:mesosphere,項目名稱:marathon-client,代碼行數:13,代碼來源:DCOSAuthCredentials.java


注:本文中的com.nimbusds.jwt.SignedJWT.sign方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。