本文整理汇总了Java中org.springframework.security.crypto.codec.Base64.encode方法的典型用法代码示例。如果您正苦于以下问题:Java Base64.encode方法的具体用法?Java Base64.encode怎么用?Java Base64.encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.security.crypto.codec.Base64
的用法示例。
在下文中一共展示了Base64.encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createHeaders
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
static HttpHeaders createHeaders(final String username, final String password) {
return new HttpHeaders() {
/**
*
*/
private static final long serialVersionUID = 1L;
{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encode(
auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
}
};
}
示例2: getWebSocketHttpHeaders
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
public WebSocketHttpHeaders getWebSocketHttpHeaders(
WebSocketSession userAgentSession) {
WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
Principal principal = userAgentSession.getPrincipal();
if (principal != null) {
Authentication authentication = (Authentication) principal;
String usernameColonPwd = authentication.getName() + ":"
+ authentication.getCredentials().toString();
String encodedCredentials = new String(
Base64.encode(usernameColonPwd.getBytes()));
headers.put(HttpHeaders.AUTHORIZATION,
Collections.singletonList("Basic " + encodedCredentials));
if(logger.isDebugEnabled()) {
logger.debug("Added basic authentication header for user " + principal.getName() + " to web sockets http headers");
}
}
else {
if(logger.isDebugEnabled()) {
logger.debug("Skipped adding basic authentication header since user session principal is null");
}
}
return headers;
}
开发者ID:mthizo247,项目名称:spring-cloud-netflix-zuul-websocket,代码行数:24,代码来源:BasicAuthPrincipalHeadersCallback.java
示例3: interceptInner
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
private void interceptInner(HttpHeaders headers, HttpRequest httpRequest) {
URI uri = httpRequest.getURI();
String host = uri.getHost();
int port = uri.getPort();
String url = host + (port == -1 ? "" : ":" + port);
String name = registryName.get();
log.debug("try to auth request to registry: {}", name);
RegistryService registry = registryRepository.getByName(name);
if (registry == null) {
log.debug("auth : none due to unknown registry \"{}\"", name);
return;
}
RegistryCredentials credentials = registry.getCredentials();
if (credentials == null || !StringUtils.hasText(credentials.getPassword())) {
log.debug("auth : none due to unknown registry \"{}\"", name);
return;
}
String result = format("'{'\"username\":\"{0}\",\"password\":\"{1}\",\"email\":\"[email protected]\",\"serveraddress\":\"{2}\",\"auth\":\"\"'}'",
credentials.getUsername(), credentials.getPassword(), url);
log.debug("auth : {}", result);
String xRegistryAuth = new String(Base64.encode(result.getBytes()));
log.debug("X-Registry-Auth : [{}]", xRegistryAuth);
headers.add("X-Registry-Auth", xRegistryAuth);
}
示例4: addSecurity
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
/**
* Agrega seguridad a una llamada a un servicio, para ello agrega dos header
* params, pertenecientes a Usuario y Password.
*
* @param user
* usuario de la llamada
* @param password
* contraseña del que invoca el servicio.
* @param message
* mensaje que actualmente se esta enviando
*/
public void addSecurity(String user, String password,
WebServiceMessage message) {
Charset cs = Charset.forName(CharEncoding.UTF_8);
TransportContext context = TransportContextHolder.getTransportContext();
HttpUrlConnection connection = (HttpUrlConnection) context
.getConnection();
HttpURLConnection uRLConnection = connection.getConnection();
String auth = CREDENTIALS_FORMAT.replace("USER", user).replace(
"PASSWORD", password);
byte[] encode = Base64.encode(auth.getBytes(cs));
uRLConnection.addRequestProperty(AUTHORIZATION_HEADER_PARAM,
HEADER_CREDENTIALS_FORMAT.replace("CREDENTIALS", new String(
encode, cs)));
}
示例5: sendPasswordReminder
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
@Override
public void sendPasswordReminder(User newUser) {
log.debug("Calling send password change link for user {}", newUser);
User user = userRepository.getByUserEmail(newUser.getEmail());
if (user == null) {
throw new IllegalArgumentException("Could not find user for email " + newUser.getEmail());
}
// create user hash:
String input = user.getEmail() + user.getUsername() + String.valueOf(user.getId()) + System.nanoTime();
// hash needs to be URL friendly:
String safeString = new String(Base64.encode(passwordEncoder.encode(input).getBytes()));
String hashedInput = safeString.replaceAll("[/=]", "A");
user.setForgotPasswordHash(hashedInput);
user.setForgotPasswordTime(Calendar.getInstance().getTime());
userRepository.save(user);
String to = user.getUsername() + " <" + user.getEmail() + '>';
Map<String, Object> templateData = new HashMap<String, Object>();
templateData.put("user", user);
templateData.put("reminderUrl", baseUrl + hashedInput);
emailService.sendEmail(to, passwordReminderSubject, passwordReminderTemplate, templateData);
}
示例6: generatePasswordSalt
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
public static String generatePasswordSalt() {
byte[] aesKey = new byte[16];
ranGen.nextBytes(aesKey);
String salt = new String(Base64.encode(aesKey));
salt = salt.replace("\r", "");
salt = salt.replace("\n", "");
return salt;
}
示例7: signRequest
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
public String signRequest(String mapsUrl) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, URISyntaxException, MalformedURLException {
URL url = new URL(mapsUrl);
String resource = url.getPath() + "?" + url.getQuery();
SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(sha1Key);
byte[] sigBytes = mac.doFinal(resource.getBytes());
String signature = new String(Base64.encode(sigBytes));
signature = signature.replace('+', '-');
signature = signature.replace('/', '_');
return signature;
}
示例8: basicAuthorizationHeader
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
/**
* Returns a basic authorization header for the given username and password.
*
* @param username Must not be null
* @param password Must not be null
* @return Returns the header as String. Never returns null.
*/
public static String basicAuthorizationHeader(String username, String password) {
Assert.notNull(username, "The username must not be null.");
Assert.notNull(password, "The password must not be null.");
return "Basic " + new String(Base64.encode(
(username + ":" + password).getBytes(StandardCharsets.ISO_8859_1)));
}
示例9: getClientBasicAuthHeader
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
private static String getClientBasicAuthHeader(String clientId, String clientSecret) {
try {
byte[] autbytes = Base64.encode(String.format("%s:%s", clientId, clientSecret).getBytes("UTF-8"));
String base64 = new String(autbytes);
return String.format("Basic %s", base64);
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
}
示例10: getKeyFromServer
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
private String getKeyFromServer() {
HttpHeaders headers = new HttpHeaders();
String username = this.resource.getClientId();
String password = this.resource.getClientSecret();
if (username != null && password != null) {
byte[] token = Base64.encode((username + ":" + password).getBytes());
headers.add("Authorization", "Basic " + new String(token));
}
HttpEntity<Void> request = new HttpEntity<Void>(headers);
String url = this.resource.getJwt().getKeyUri();
return (String) this.keyUriRestTemplate
.exchange(url, HttpMethod.GET, request, Map.class).getBody()
.get("value");
}
开发者ID:philwebb,项目名称:spring-boot-concourse,代码行数:15,代码来源:ResourceServerTokenServicesConfiguration.java
示例11: getHeaders
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
private HttpHeaders getHeaders(ClientDetails config) {
HttpHeaders headers = new HttpHeaders();
String token = new String(Base64.encode(
(config.getClientId() + ":" + config.getClientSecret()).getBytes()));
headers.set("Authorization", "Basic " + token);
return headers;
}
示例12: accessingRootUriPossibleWithUserAccount
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
@Test
@Ignore
public void accessingRootUriPossibleWithUserAccount() throws Exception {
String header = "Basic " + new String(Base64.encode("greg:turnquist".getBytes()));
this.mvc.perform(
get("/").accept(MediaTypes.HAL_JSON).header("Authorization", header))
.andExpect(
header().string("Content-Type", MediaTypes.HAL_JSON.toString()))
.andExpect(status().isOk()).andDo(print());
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:11,代码来源:SampleSecureOAuth2ApplicationTests.java
示例13: headersForIntrospection
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
private MultiValueMap<String, String> headersForIntrospection() {
HttpHeaders headers = new HttpHeaders();
String basicAuthz = clientId + ":" + clientSecret;
String authenticationCredentials = "Basic " + new String(Base64.encode(basicAuthz.getBytes(Charset.forName("UTF-8"))));
headers.add("Authorization", authenticationCredentials);
headers.add("Accept", "application/json");
return headers;
}
示例14: getBasicAuthHeaders
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
public static HttpHeaders getBasicAuthHeaders(HttpHeaders headers, String username, String password) {
String auth = username + ":" + password;
byte[] bytes = Base64.encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeader = "Basic " + new String(bytes, StandardCharsets.UTF_8);
headers.set(AUTHORIZATION, authHeader);
return headers;
}
示例15: getForObject
import org.springframework.security.crypto.codec.Base64; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
protected <T> T getForObject(String resource, User user, Class<T> responseType, Map<String, ?> urlVariables) {
resource = BASE_URL + resource;
if (user == null) {
if (urlVariables == null) {
return template.getForObject(resource, responseType);
} else {
return template.getForObject(resource, responseType, urlVariables);
}
} else {
String usernameAndPassword = user.getUsername() + ":" + PASSWORD;
byte[] auth = usernameAndPassword.getBytes();
RestTemplate objectTemplate = new LDRestTemplate();
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
byte[] encodedAuthorisation = Base64.encode(auth);
request.getHeaders().add("Authorization", "Basic " + new String(encodedAuthorisation));
return execution.execute(request, body);
};
List<ClientHttpRequestInterceptor> list = new ArrayList<>();
list.add(interceptor);
objectTemplate.setInterceptors(list);
if (urlVariables == null) {
return objectTemplate.getForObject(resource, responseType);
} else {
return objectTemplate.getForObject(resource, responseType, urlVariables);
}
}
}