本文整理汇总了Java中org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString方法的典型用法代码示例。如果您正苦于以下问题:Java Base64.encodeBase64URLSafeString方法的具体用法?Java Base64.encodeBase64URLSafeString怎么用?Java Base64.encodeBase64URLSafeString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.codec.binary.Base64
的用法示例。
在下文中一共展示了Base64.encodeBase64URLSafeString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldSignAndVerifyWithECDSA384
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldSignAndVerifyWithECDSA384() throws Exception {
ECDSAAlgorithm algorithm384 = (ECDSAAlgorithm) Algorithm.ECDSA384((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_384, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_384, "EC"));
String content384 = "eyJhbGciOiJFUzM4NCJ9.eyJpc3MiOiJhdXRoMCJ9";
for (int i = 0; i < 10; i++) {
byte[] signature = algorithm384.sign(content384.getBytes());
String signature384 = Base64.encodeBase64URLSafeString((signature));
String token = content384 + "." + signature384;
JWT jwt = JWT.require(algorithm384).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm384.verify(decoded, EncodeType.Base64);
}
}
示例2: shouldDoRSA256SigningWithBothKeys
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldDoRSA256SigningWithBothKeys() throws Exception {
Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"), (RSAPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
String jwtContent = String.format("%s.%s", RS256Header, auth0IssPayload);
byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
byte[] signatureBytes = algorithm.sign(contentBytes);
String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
String token = String.format("%s.%s", jwtContent, jwtSignature);
String expectedSignature = "ZB-Tr0vLtnf8I9fhSdSjU6HZei5xLYZQ6nZqM5O6Va0W9PgAqgRT7ShI9CjeYulRXPHvVmSl5EQuYuXdBzM0-H_3p_Nsl6tSMy4EyX2kkhEm6T0HhvarTh8CG0PCjn5p6FP5ZxWwhLcmRN70ItP6Z5MMO4CcJh1JrNxR4Fi4xQgt-CK2aVDMFXd-Br5yQiLVx1CX83w28OD9wssW3Rdltl5e66vCef0Ql6Q5I5e5F0nqGYT989a9fkNgLIx2F8k_az5x07BY59FV2SZg59nSiY7TZNjP8ot11Ew7HKRfPXOdh9eKRUVdhcxzqDePhyzKabU8TG5FP0SiWH5qVPfAgw";
assertThat(signatureBytes, is(notNullValue()));
assertThat(jwtSignature, is(expectedSignature));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例3: shouldDoRSA256SigningWithProvidedPrivateKey
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldDoRSA256SigningWithProvidedPrivateKey() throws Exception {
RSAKeyProvider provider = mock(RSAKeyProvider.class);
PrivateKey privateKey = readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA");
PublicKey publicKey = readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA");
when(provider.getPrivateKey()).thenReturn((RSAPrivateKey) privateKey);
when(provider.getPublicKeyById(null)).thenReturn((RSAPublicKey) publicKey);
Algorithm algorithm = Algorithm.RSA256(provider);
String jwtContent = String.format("%s.%s", RS256Header, auth0IssPayload);
byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
byte[] signatureBytes = algorithm.sign(contentBytes);
String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
String token = String.format("%s.%s", jwtContent, jwtSignature);
assertThat(signatureBytes, is(notNullValue()));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例4: shouldFailJOSEToDERConversionOnInvalidJOSESignatureLength
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldFailJOSEToDERConversionOnInvalidJOSESignatureLength() throws Exception {
exception.expect(SignatureVerificationException.class);
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: SHA256withECDSA");
exception.expectCause(isA(SignatureException.class));
exception.expectCause(hasMessage(is("Invalid JOSE signature format.")));
byte[] bytes = new byte[256];
new SecureRandom().nextBytes(bytes);
String signature = Base64.encodeBase64URLSafeString(bytes);
String token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9." + signature;
ECPublicKey publicKey = (ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC");
ECPrivateKey privateKey = mock(ECPrivateKey.class);
ECDSAKeyProvider provider = ECDSAAlgorithm.providerForKeys(publicKey, privateKey);
Algorithm algorithm = new ECDSAAlgorithm("ES256", "SHA256withECDSA", 128, provider);
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例5: shouldFailECDSA384VerificationOnInvalidDERSignature
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldFailECDSA384VerificationOnInvalidDERSignature() throws Exception {
exception.expect(AlgorithmMismatchException.class);
exception.expectMessage("The provided Algorithm doesn't match the one defined in the JWT's Header.");
byte[] bytes = new byte[96];
new SecureRandom().nextBytes(bytes);
bytes[0] = 0x30;
String signature = Base64.encodeBase64URLSafeString(bytes);
String token = "eyJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9." + signature;
Algorithm algorithm = Algorithm.ECDSA384((ECKey) readPublicKeyFromFile(INVALID_PUBLIC_KEY_FILE_384, "EC"));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例6: getTestBean
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
private ExtensionInterfaceBean getTestBean(String instanceId,
String subscriptionId, String organizationId) throws Exception {
instanceAccess = Mockito.mock(InstanceAccess.class);
serverInfo = getServerInfoMock(3);
String encodedInstId = Base64.encodeBase64URLSafeString(
instanceId.getBytes(StandardCharsets.UTF_8));
String encodedSubId = Base64.encodeBase64URLSafeString(
subscriptionId.getBytes(StandardCharsets.UTF_8));
String encodedOrgId = Base64.encodeBase64URLSafeString(
organizationId.getBytes(StandardCharsets.UTF_8));
Mockito.when(instanceAccess.getAccessInfo(instanceID, subscriptionID,
organizationID)).thenReturn("Access info from IaaS");
Mockito.<List<? extends ServerInformation>> when(instanceAccess
.getServerDetails(instanceID, subscriptionID, organizationID))
.thenReturn(serverInfo);
ExtensionInterfaceBean bean = new ExtensionInterfaceBean();
bean.setInstanceId(encodedInstId);
bean.setSubscriptionId(encodedSubId);
bean.setOrganizationId(encodedOrgId);
bean.setInstanceAccess(instanceAccess);
return bean;
}
示例7: shouldDoECDSA512SigningWithBothKeys
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldDoECDSA512SigningWithBothKeys() throws Exception {
Algorithm algorithm = Algorithm.ECDSA512((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
String jwtContent = String.format("%s.%s", ES512Header, auth0IssPayload);
byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
byte[] signatureBytes = algorithm.sign(contentBytes);
String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
String token = String.format("%s.%s", jwtContent, jwtSignature);
assertThat(signatureBytes, is(notNullValue()));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例8: shouldDoRSA512Signing
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldDoRSA512Signing() throws Exception {
Algorithm algorithmSign = Algorithm.RSA512((RSAKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE, "RSA"));
Algorithm algorithmVerify = Algorithm.RSA512((RSAKey) readPublicKeyFromFile(PUBLIC_KEY_FILE, "RSA"));
String jwtContent = String.format("%s.%s", RS512Header, auth0IssPayload);
byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
byte[] signatureBytes = algorithmSign.sign(contentBytes);
String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
String token = String.format("%s.%s", jwtContent, jwtSignature);
String expectedSignature = "THIPVYzNZ1Yo_dm0k1UELqV0txs3SzyMopCyHcLXOOdgYXF4MlGvBqu0CFvgSga72Sp5LpuC1Oesj40v_QDsp2GTGDeWnvvcv_eo-b0LPSpmT2h1Ibrmu-z70u2rKf28pkN-AJiMFqi8sit2kMIp1bwIVOovPvMTQKGFmova4Xwb3G526y_PeLlflW1h69hQTIVcI67ACEkAC-byjDnnYIklA-B4GWcggEoFwQRTdRjAUpifA6HOlvnBbZZlUd6KXwEydxVS-eh1odwPjB2_sfbyy5HnLsvNdaniiZQwX7QbwLNT4F72LctYdHHM1QCrID6bgfgYp9Ij9CRX__XDEA";
assertThat(signatureBytes, is(notNullValue()));
assertThat(jwtSignature, is(expectedSignature));
JWT jwt = JWT.require(algorithmVerify).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithmVerify.verify(decoded, EncodeType.Base64);
}
示例9: shouldSignAndVerifyWithECDSA512
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldSignAndVerifyWithECDSA512() throws Exception {
ECDSAAlgorithm algorithm512 = (ECDSAAlgorithm) Algorithm.ECDSA512((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_512, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_512, "EC"));
String content512 = "eyJhbGciOiJFUzUxMiJ9.eyJpc3MiOiJhdXRoMCJ9";
for (int i = 0; i < 10; i++) {
byte[] signature = algorithm512.sign(content512.getBytes());
String signature512 = Base64.encodeBase64URLSafeString((signature));
String token = content512 + "." + signature512;
JWT jwt = JWT.require(algorithm512).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm512.verify(decoded, EncodeType.Base64);
}
}
示例10: getToken
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Override
public String getToken() {
String content = String.format("%s.%s", b64(jsonMinify(getHeaderJson())), b64(jsonMinify((getPayloadJson()))));
String signatureEncoded = Base64.encodeBase64URLSafeString(this.signature);
return String.format("%s.%s", content, signatureEncoded);
}
示例11: customJWT
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
private DecodedJWT customJWT(String jsonHeader, String jsonPayload, String signature) throws Exception{
String header = Base64.encodeBase64URLSafeString(jsonHeader.getBytes(StandardCharsets.UTF_8));
String body = Base64.encodeBase64URLSafeString(jsonPayload.getBytes(StandardCharsets.UTF_8));
JWT jwt = JWT.require(Algorithm.HMAC256("secret")).build();
DecodedJWT decodedJWT = jwt.decode(String.format("%s.%s.%s", header, body, signature));
return decodedJWT;
}
示例12: encodeInputString
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
/**
* Encode the inputString to a URL safe string
*
* @param inputString
* @return
*/
public String encodeInputString(String inputString) {
String encodedOutput = "";
if (StringUtils.isNotBlank(inputString)) {
encodedOutput = Base64.encodeBase64URLSafeString(inputString.getBytes());
}
return encodedOutput;
}
示例13: shouldDoECDSA256SigningWithBothKeys
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void shouldDoECDSA256SigningWithBothKeys() throws Exception {
Algorithm algorithm = Algorithm.ECDSA256((ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256, "EC"), (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256, "EC"));
String jwtContent = String.format("%s.%s", ES256Header, auth0IssPayload);
byte[] contentBytes = jwtContent.getBytes(StandardCharsets.UTF_8);
byte[] signatureBytes = algorithm.sign(contentBytes);
String jwtSignature = Base64.encodeBase64URLSafeString(signatureBytes);
String token = String.format("%s.%s", jwtContent, jwtSignature);
assertThat(signatureBytes, is(notNullValue()));
JWT jwt = JWT.require(algorithm).withIssuer("auth0").build();
DecodedJWT decoded = jwt.decode(token);
algorithm.verify(decoded, EncodeType.Base64);
}
示例14: defaultSign
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
private String defaultSign() throws SignatureGenerationException {
String header = Base64.encodeBase64URLSafeString(headerJson.getBytes(StandardCharsets.UTF_8));
String payload = Base64.encodeBase64URLSafeString(payloadJson.getBytes(StandardCharsets.UTF_8));
String content = String.format("%s.%s", header, payload);
byte[] signatureBytes = algorithm.sign(content.getBytes(StandardCharsets.UTF_8));
String signature = Base64.encodeBase64URLSafeString(signatureBytes);
return String.format("%s.%s", content, signature);
}
示例15: testCheckToken
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void testCheckToken() throws Exception {
CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
gen.generate(1024);
X509Certificate cert = gen.getSelfCertificate(new X500Name("CN=ROOT"),
new Date(), 10000000);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
String alias = "temp";
String loc = "./temp.jks";
String password = "changeit";
ks.load(null, password.toCharArray());
ks.setCertificateEntry(alias, cert);
FileOutputStream fos = new FileOutputStream(loc);
ks.store(fos, password.toCharArray());
fos.close();
Mockito.when(configSvc.getProxyConfigurationSetting(
PlatformConfigurationKey.APP_TRUSTSTORE)).thenReturn(loc);
Mockito.when(configSvc.getProxyConfigurationSetting(
PlatformConfigurationKey.APP_TRUSTSTORE_PASSWORD))
.thenReturn(password);
Mockito.when(configSvc.getProxyConfigurationSetting(
PlatformConfigurationKey.APP_TRUSTSTORE_BSS_ALIAS))
.thenReturn(alias);
String token = UUID.randomUUID().toString();
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(token.getBytes(StandardCharsets.UTF_8));
byte[] tokenHash = md.digest();
Key key = gen.getPrivateKey();
Cipher c = Cipher.getInstance(key.getAlgorithm());
c.init(Cipher.ENCRYPT_MODE, key);
String tokenSignature = Base64
.encodeBase64URLSafeString(c.doFinal(tokenHash));
boolean check = platformSvc.checkToken(token, tokenSignature);
assertTrue(check);
Files.delete(new File(loc).toPath());
}