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


Java JWSAlgorithm類代碼示例

本文整理匯總了Java中com.nimbusds.jose.JWSAlgorithm的典型用法代碼示例。如果您正苦於以下問題:Java JWSAlgorithm類的具體用法?Java JWSAlgorithm怎麽用?Java JWSAlgorithm使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


JWSAlgorithm類屬於com.nimbusds.jose包,在下文中一共展示了JWSAlgorithm類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getSignedContent

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
public String getSignedContent(String content) {
    Payload contentPayload = new Payload(content);

    try {
        RSASSASigner rsa = new RSASSASigner((RSAPrivateKey) clientJwk);
        JWSAlgorithm alg = JWSAlgorithm.RS256;
        JWSHeader header = new JWSHeader.Builder(alg)
            .keyID(clientJwk.getKeyID())
            .build();
        JWSObject jws = new JWSObject(header, contentPayload);
        jws.sign(rsa);
        return jws.serialize();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:PacktPublishing,項目名稱:OAuth-2.0-Cookbook,代碼行數:17,代碼來源:JwkKeyPairManager.java

示例2: determineOptimalAlgorithm

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
public JWSAlgorithm determineOptimalAlgorithm(byte[] secret) {
    JWSAlgorithm result = null;

    Set<JWSAlgorithm> algorithms = MACSigner.getCompatibleAlgorithms(ByteUtils.bitLength(secret));

    if (algorithms.contains(JWSAlgorithm.HS512)) {
        result = JWSAlgorithm.HS512;
    }
    if (result == null && algorithms.contains(JWSAlgorithm.HS384)) {
        result = JWSAlgorithm.HS384;
    }
    if (result == null && algorithms.contains(JWSAlgorithm.HS256)) {
        result = JWSAlgorithm.HS256;
    }

    if (result == null) {
        throw new ConfigurationException("Secret is too short for any JWS HMAC algorithm.");
    }
    return result;
}
 
開發者ID:atbashEE,項目名稱:atbash-octopus,代碼行數:21,代碼來源:HMACAlgorithmFactory.java

示例3: defineJWSAlgorithm

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
public JWSAlgorithm defineJWSAlgorithm(JWTParametersSigning parametersSigning) {
    checkDependencies();

    JWSAlgorithm result;

    switch (parametersSigning.getSecretKeyType()) {
        case HMAC:
            result = hmacAlgorithmFactory.determineOptimalAlgorithm(((HMACSecret) parametersSigning.getJWK()).toSecretKey().getEncoded());
            break;
        case RSA:
            result = JWSAlgorithm.RS256; // FIXME Is this always (what about 384 and 512
            break;
        case EC:
            result = JWSAlgorithm.ES256; // FIXME Is this always (what about 384 and 512
            break;
        default:
            throw new IllegalArgumentException(String.format("Unsupported value for SecretKeyType : %s", parametersSigning.getSecretKeyType()));
    }

    return result;

}
 
開發者ID:atbashEE,項目名稱:atbash-octopus,代碼行數:23,代碼來源:JWTSignerFactory.java

示例4: getAadJwtTokenValidator

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
private ConfigurableJWTProcessor<SecurityContext> getAadJwtTokenValidator()
        throws MalformedURLException {
    final ConfigurableJWTProcessor<SecurityContext> jwtProcessor = new DefaultJWTProcessor<>();
    final JWKSource<SecurityContext> keySource = new RemoteJWKSet<>(
            new URL(KEY_DISCOVERY_URI));
    final JWSAlgorithm expectedJWSAlg = JWSAlgorithm.RS256;
    final JWSKeySelector<SecurityContext> keySelector = new JWSVerificationKeySelector<>(expectedJWSAlg, keySource);
    jwtProcessor.setJWSKeySelector(keySelector);

    jwtProcessor.setJWTClaimsSetVerifier(new DefaultJWTClaimsVerifier<SecurityContext>() {
        @Override
        public void verify(JWTClaimsSet claimsSet, SecurityContext ctx) throws BadJWTException {
            super.verify(claimsSet, ctx);
            final String issuer = claimsSet.getIssuer();
            if (issuer == null || !issuer.contains("https://sts.windows.net/")) {
                throw new BadJWTException("Invalid token issuer");
            }
        }
    });
    return jwtProcessor;
}
 
開發者ID:Microsoft,項目名稱:azure-spring-boot,代碼行數:22,代碼來源:UserPrincipal.java

示例5: generateCookieBody

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的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

示例6: generateSharedGoogleSecret

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的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

示例7: getIdToken

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的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

示例8: getSigningJwk

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
protected RSAKey getSigningJwk() {
	KeyStore.PrivateKeyEntry keyEntry = supplyHonestOrEvil(opivCfg::getHonestOPSigningEntry, opivCfg::getEvilOPSigningEntry);

	RSAPublicKey pubKey = (RSAPublicKey) keyEntry.getCertificate().getPublicKey();
	RSAPrivateKey privKey = (RSAPrivateKey) keyEntry.getPrivateKey();
	List<Base64> chain = Arrays.stream(keyEntry.getCertificateChain()).map(c -> {
		try {
			return Base64.encode(c.getEncoded());
		} catch (CertificateEncodingException ex) {
			throw new IllegalArgumentException("Failed to encode certificate.", ex);
		}
	}).collect(Collectors.toList());

	RSAKey key = new RSAKey.Builder(pubKey)
			.privateKey(privKey)
			.x509CertChain(chain)
			.algorithm(JWSAlgorithm.RS256)
			.build();

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

示例9: setUp

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
	secretKey = TOKEN.getBytes();
	sessionIdentifier = spy(new SessionIdentifier(TOKEN, secretKey, authenticationData));
	signer = new MACSigner(secretKey);
	whenNew(MACSigner.class).withArguments(secretKey).thenReturn(signer);

	JWTClaimsSet.Builder builder = new JWTClaimsSet.Builder();
	builder.issuer(USER);
	builder.claim("mode", MODE);
	JWTClaimsSet buildClaim = builder.build();
	when(authenticationData.buildClaimSet()).thenReturn(buildClaim);

	jwsHeader = new JWSHeader(JWSAlgorithm.HS256);
	signedJWT = spy(new SignedJWT(jwsHeader, buildClaim));
	whenNew(SignedJWT.class).withAnyArguments().thenReturn(signedJWT);

}
 
開發者ID:ccem-dev,項目名稱:otus-api,代碼行數:19,代碼來源:SecurityContextServiceBeanTest.java

示例10: createEmptyJWTwithPublicKey

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的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

示例11: createJWT

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的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

示例12: sign

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
public String sign(String algorithm, String kid, String keyStr, String dataToSign) {
    try {

        Key key = getKey(algorithm, keyStr);

        JWSHeader.Builder jwsBuilder = new JWSHeader.Builder("HS256".equals(algorithm) ? JWSAlgorithm.HS256 : JWSAlgorithm.RS256);
        jwsBuilder.keyID(kid);

        JWSHeader signingHeader = jwsBuilder.build();
        JWSSigner signer = "HS256".equals(algorithm) ? new MACSigner(key.getEncoded()) : new RSASSASigner((RSAPrivateKey) key);
        JWSObject jwsObject = new JWSObject(signingHeader, new Payload(dataToSign));
        jwsObject.sign(signer);
        checkObject(jwsObject);

        String parts[] = jwsObject.serialize().split("\\.");

        return "{\"protected\":\"" + parts[0] + "\", \"payload\":\"" + parts[1] + "\", \"signature\":\"" + parts[2] + "\"}";

    } catch (Exception e) {
        throw new CryptoException("Exception signing data: " + e.getMessage(), e);
    }
}
 
開發者ID:americanexpress,項目名稱:amex-api-java-client-core,代碼行數:23,代碼來源:EncryptionUtility.java

示例13: createTokenRSA

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
public static String createTokenRSA( PrivateKey privateKey, String claimJson )
{
    try
    {
        JWSSigner signer = new RSASSASigner( ( RSAPrivateKey ) privateKey );

        Payload pl = new Payload( claimJson );
        JWSObject jwsObject = new JWSObject( new JWSHeader( JWSAlgorithm.RS256 ), pl );

        jwsObject.sign( signer );

        return jwsObject.serialize();
    }
    catch ( Exception e )
    {
        LOG.error( "Error creating RSA token", e.getMessage() );

        return "";
    }
}
 
開發者ID:subutai-io,項目名稱:base,代碼行數:21,代碼來源:TokenUtil.java

示例14: verifyTokenRSA

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的package包/類
public static boolean verifyTokenRSA( PublicKey pKey, String token )
{
    try
    {
        Payload pl = new Payload( token );
        JWSObject jwsObject = new JWSObject( new JWSHeader( JWSAlgorithm.RS256 ), pl );
        JWSVerifier verifier = new RSASSAVerifier( ( RSAPublicKey ) pKey );

        return jwsObject.verify( verifier );
    }
    catch ( JOSEException e )
    {
        LOG.warn( "Error verifying RSA token", e.getMessage() );

        return false;
    }
}
 
開發者ID:subutai-io,項目名稱:base,代碼行數:18,代碼來源:TokenUtil.java

示例15: selfIssue

import com.nimbusds.jose.JWSAlgorithm; //導入依賴的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


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