本文整理汇总了Java中org.apache.shiro.codec.Base64.encodeToString方法的典型用法代码示例。如果您正苦于以下问题:Java Base64.encodeToString方法的具体用法?Java Base64.encodeToString怎么用?Java Base64.encodeToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.shiro.codec.Base64
的用法示例。
在下文中一共展示了Base64.encodeToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rememberSerializedIdentity
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
/**
* Base64-encodes the specified serialized byte array and sets that base64-encoded String as the cookie value.
* <p/>
* The {@code subject} instance is expected to be a {@link WebSubject} instance with an HTTP Request/Response pair
* so an HTTP cookie can be set on the outgoing response. If it is not a {@code WebSubject} or that
* {@code WebSubject} does not have an HTTP Request/Response pair, this implementation does nothing.
*
* @param subject the Subject for which the identity is being serialized.
* @param serialized the serialized bytes to be persisted.
*/
protected void rememberSerializedIdentity(Subject subject, byte[] serialized) {
if (!WebUtils.isHttp(subject)) {
if (log.isDebugEnabled()) {
String msg = "Subject argument is not an HTTP-aware instance. This is required to obtain a servlet " +
"request and response in order to set the rememberMe cookie. Returning immediately and " +
"ignoring rememberMe operation.";
log.debug(msg);
}
return;
}
HttpServletRequest request = WebUtils.getHttpRequest(subject);
HttpServletResponse response = WebUtils.getHttpResponse(subject);
//base 64 encode it and store as a cookie:
String base64 = Base64.encodeToString(serialized);
Cookie template = getCookie(); //the class attribute is really a template for the outgoing cookies
Cookie cookie = new SimpleCookie(template);
cookie.setValue(base64);
cookie.saveTo(request, response);
}
示例2: getCredentials
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
/**
*
* @param info the {@code AuthenticationInfo} stored in the data store to be compared against the submitted authentication
* token's credentials.
* @return Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign ) ) );
*/
@Override
protected Object getCredentials(AuthenticationInfo info) {
Object signature = null;
byte[] saltBytes = null;
if (info instanceof SaltedAuthenticationInfo) {
saltBytes = ((SaltedAuthenticationInfo) info).getCredentialsSalt().getBytes();
}
try {
String stringToSign = (String) info.getCredentials();
byte[] hmacSha1 = HmacSha1.hash(saltBytes, stringToSign);
signature = Base64.encodeToString(hmacSha1);
}
catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
return signature;
}
示例3: rememberSerializedIdentity
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
/**
* Base64-encodes the specified serialized byte array and sets that base64-encoded String as the cookie value.
* <p/>
* The {@code subject} instance is expected to be a {@link WebSubject} instance with an HTTP Request/Response pair
* so an HTTP cookie can be set on the outgoing response. If it is not a {@code WebSubject} or that
* {@code WebSubject} does not have an HTTP Request/Response pair, this implementation does nothing.
*
* @param subject the Subject for which the identity is being serialized.
* @param serialized the serialized bytes to be persisted.
*/
@Override
protected void rememberSerializedIdentity(Subject subject, byte[] serialized) {
if (! (subject instanceof ContextSource)) {
// if (log.isDebugEnabled()) {
// String msg = "Subject argument is not an HTTP-aware instance. This is required to obtain a servlet " +
// "request and response in order to set the rememberMe cookie. Returning immediately and " +
// "ignoring rememberMe operation.";
// log.debug(msg);
// }
return;
}
Context context = ((ContextSource) subject).getContext();
//base 64 encode it and store as a cookie
String base64 = Base64.encodeToString(serialized); // could be java.util.Base64
context.addCookie(Cookie.builder(getCookie()).setValue(base64).build()); // save the cookie
}
示例4: rememberSerializedIdentity
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
@Override
protected void rememberSerializedIdentity(Subject subject, byte[] serialized) {
if (!WebUtils.isHttp(subject)) {
if (LOGGER.isDebugEnabled()) {
String msg = "Subject argument is not an HTTP-aware instance. This is required to obtain a servlet " +
"request and response in order to set the rememberMe cookie. Returning immediately and " +
"ignoring rememberMe operation.";
LOGGER.debug(msg);
}
return;
}
HttpServletRequest request = WebUtils.getHttpRequest(subject);
HttpServletResponse response = WebUtils.getHttpResponse(subject);
// base 64 encode it and store as a cookie:
String base64 = Base64.encodeToString(serialized);
// the class attribute is really a template for the outgoing cookies
Cookie cookie = getCookie();
cookie.setValue(base64);
cookie.saveTo(request, response);
}
示例5: rememberSerializedIdentity
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
@Override
protected void rememberSerializedIdentity(Subject subject, byte[] serialized) {
String base64 = Base64.encodeToString(serialized);
CookieTemplate template = getCookie(); //the class attribute is really a template for the outgoing cookies
NewCookie cookie = new NewCookie(
template.getName(),
base64,
template.getPath(),
template.getDomain(),
template.getVersion(),
template.getComment(),
template.getMaxAge(),
template.getExpiry(),
template.isSecure(),
template.isHttpOnly());
Requests.removeProperty(RM_REMEMBER_COOKIE_KEY);
Requests.setProperty(ADD_REMEMBER_COOKIE_KEY, cookie);
}
示例6: calculateRFC2104HMAC
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
/**
* Computes RFC 2104-compliant HMAC signature.
* * @param data
* The data to be signed.
*
* @param key
* The signing key.
* @return
* The Base64-encoded RFC 2104-compliant HMAC signature.
* @throws java.security.SignatureException
* when signature generation fails
*/
public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException {
String result = null;
try {
Charset utf8ChartSet = Charset.forName("UTF-8");
// get an hmac_sha1 key from the raw key bytes
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(utf8ChartSet), HMAC_SHA1_ALGORITHM);
// get an hmac_sha1 Mac instance and initialize with the signing key
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes(utf8ChartSet));
// base64-encode the hmac
result = Base64.encodeToString(rawHmac);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
}
return result;
}
示例7: clientCredentialsFlowWithHeaderAuthorizationAndPayload
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
/**
* Retrieve an authentication token using a combination of form input and payload
*/
@Test
public void clientCredentialsFlowWithHeaderAuthorizationAndPayload() throws Exception {
//retrieve the credentials
Credentials orgCredentials = getOrgCredentials();
String clientId = orgCredentials.getClientId();
String clientSecret = orgCredentials.getClientSecret();
//Encode the credentials
String clientCredentials = clientId + ":" + clientSecret;
String token = Base64.encodeToString(clientCredentials.getBytes());
//POST the form to the application token endpoint along with the payload
Token apiResponse = this.app().token().getTarget( false ).request()
.header( "Authorization", "Basic " + token )
.accept( MediaType.APPLICATION_JSON )
.post(javax.ws.rs.client.Entity.entity(
hashMap("grant_type", "client_credentials"), MediaType.APPLICATION_JSON_TYPE), Token.class);
//Assert that a valid token with a valid TTL is returned
assertNotNull("It has access_token.", apiResponse.getAccessToken());
assertNotNull("It has expires_in.", apiResponse.getExpirationDate());
}
示例8: serialize
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
public static String serialize(Session session) {
if (null == session) {
return null;
}
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(session);
return Base64.encodeToString(bos.toByteArray());
} catch (Exception e) {
throw new RuntimeException("serialize session error", e);
}
}
示例9: serializaToString
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
public static String serializaToString(Serializable obj){
try{
byte[] value = serialize(obj);
return Base64.encodeToString(value);
}catch (Exception e){
throw new ResultException("serialize session error");
}
}
示例10: toBase64
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
@Override
public String toBase64() {
if ( this.cachedBase64 == null ) {
this.cachedBase64 = Base64.encodeToString(getBytes());
}
return this.cachedBase64;
}
示例11: testBase64
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
@Test
public void testBase64(){
String str = "hello";
String base64Encode = Base64.encodeToString(str.getBytes());
String str2 = Base64.decodeToString(base64Encode);
Assert.assertEquals(str, str2);
}
示例12: serialize
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
public static String serialize(Object session) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(session);
return Base64.encodeToString(bos.toByteArray());
} catch (Exception e) {
throw new RuntimeException("serialize session error", e);
}
}
示例13: generateKeys
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
public void generateKeys() {
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[accessKeyLength];
random.nextBytes(bytes);
String accessKey = Base64.encodeToString(bytes).toUpperCase().replace('+', '0').replace('/', '9');
bytes = new byte[secretKeyLength];
random.nextBytes(bytes);
String secretKey = Base64.encodeToString(bytes);
result = new String[] {accessKey, secretKey};
}
示例14: buildHeader
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
private Header[] buildHeader(ApiKey apiKey, HttpRequestBase httpRequest, String contentType, String entity) throws Exception {
String accessKey = apiKey.getAccessKey();
String secretKey = apiKey.getSecretKey();
String contentMd5 = entity != null ? DigestUtils.md5Hex(entity) : "";
String dateString = dateFormatter.get().format(new Date());
String stringToSign = String.format(Locale.US, "%s\n%s\n%s\n%s\n%s",
httpRequest.getMethod(),
contentMd5,
contentType,
dateString,
httpRequest.getURI().getPath());
byte[] hexHmac = HmacSha1.hash(secretKey, stringToSign);
String base64Hmac = Base64.encodeToString(hexHmac);
Header[] headers = new Header[] {
new BasicHeader("Content-Type", contentType),
new BasicHeader("Content-MD5", contentMd5),
new BasicHeader("Date", dateString),
new BasicHeader("Authorization", String.format(Locale.US, "AWS %s:%s", accessKey, base64Hmac)),
};
return headers;
}
示例15: toBase64
import org.apache.shiro.codec.Base64; //导入方法依赖的package包/类
/**
*
* @return a Base64-encoded string of the underlying {@link #getBytes byte array}.
*/
public String toBase64() {
if (this.base64Encoded == null) {
//cache result in case this method is called multiple times.
this.base64Encoded = Base64.encodeToString(getBytes());
}
return this.base64Encoded;
}