Java Code Examples for org.springframework.http.HttpHeaders#setBasicAuth()
The following examples show how to use
org.springframework.http.HttpHeaders#setBasicAuth() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicAuthenticationInterceptor.java From spring-analysis-note with MIT License | 6 votes |
@Override public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) { headers.setBasicAuth(this.username, this.password, this.charset); } return execution.execute(request, body); }
Example 2
Source File: WebApiUserController.java From syhthems-platform with MIT License | 5 votes |
@PostMapping(value = "/oauth/token") public String tokenEndpoint(HttpServletRequest request) { MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); Map<String, String[]> requestMap = request.getParameterMap(); requestMap.forEach((s, strings) -> { body.add(s, strings[0]); }); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.setBasicAuth(this.oAuthProperties.getClientId(), this.oAuthProperties.getClientSecret(), Charset.forName("UTF-8")); HttpEntity<MultiValueMap> httpEntity = new HttpEntity<>(body, headers); return this.restTemplate.postForObject(this.syhthemsProperties.getSecurity().getSsoServer() + "/oauth/token", httpEntity, String.class); }
Example 3
Source File: BasicAuthenticationInterceptor.java From java-technology-stack with MIT License | 5 votes |
@Override public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) { headers.setBasicAuth(this.username, this.password, this.charset); } return execution.execute(request, body); }
Example 4
Source File: AwsEcrAuthorizer.java From spring-cloud-dataflow with Apache License 2.0 | 5 votes |
@Override public HttpHeaders getAuthorizationHeaders(ContainerImage containerImage, RegistryConfiguration registryConfiguration) { Assert.isTrue(registryConfiguration.getAuthorizationType() == this.getType(), "Incorrect type: " + registryConfiguration.getAuthorizationType()); AmazonECRClientBuilder ecrBuilder = AmazonECRClientBuilder.standard(); if (registryConfiguration.getExtra().containsKey(AWS_REGION)) { ecrBuilder.withRegion(registryConfiguration.getExtra().get(AWS_REGION)); } if (StringUtils.hasText(registryConfiguration.getUser()) && StringUtils.hasText(registryConfiguration.getSecret())) { // Expects that the 'user' == 'Access Key ID' and 'secret' == 'Secret Access Key' ecrBuilder.withCredentials(new AWSStaticCredentialsProvider( new BasicAWSCredentials(registryConfiguration.getUser(), registryConfiguration.getSecret()))); } AmazonECR client = ecrBuilder.build(); GetAuthorizationTokenRequest request = new GetAuthorizationTokenRequest(); if (registryConfiguration.getExtra().containsKey(REGISTRY_IDS)) { request.withRegistryIds(registryConfiguration.getExtra().get(REGISTRY_IDS).split(",")); } GetAuthorizationTokenResult response = client.getAuthorizationToken(request); String token = response.getAuthorizationData().iterator().next().getAuthorizationToken(); final HttpHeaders headers = new HttpHeaders(); headers.setBasicAuth(token); return headers; }
Example 5
Source File: BasicAuthRegistryAuthorizer.java From spring-cloud-dataflow with Apache License 2.0 | 5 votes |
@Override public HttpHeaders getAuthorizationHeaders(ContainerImage containerImage, RegistryConfiguration registryConfiguration) { Assert.isTrue(registryConfiguration.getAuthorizationType() == this.getType(), "Incorrect type: " + registryConfiguration.getAuthorizationType()); final HttpHeaders headers = new HttpHeaders(); headers.setBasicAuth(registryConfiguration.getUser(), registryConfiguration.getSecret()); return headers; }
Example 6
Source File: DockerOAuth2RegistryAuthorizer.java From spring-cloud-dataflow with Apache License 2.0 | 5 votes |
@Override public HttpHeaders getAuthorizationHeaders(ContainerImage containerImage, RegistryConfiguration registryConfiguration) { Assert.isTrue(registryConfiguration.getAuthorizationType() == this.getType(), "Incorrect authorization type: " + registryConfiguration.getAuthorizationType()); Assert.notNull(containerImage, "Valid containerImageName is required!"); String imageRepository = containerImage.getRepository(); Assert.hasText(imageRepository, "Valid repository name (e.g. namespace/repository-name without the tag)" + " is required for the authorization"); final HttpHeaders requestHttpHeaders = new HttpHeaders(); if (StringUtils.hasText(registryConfiguration.getUser()) && StringUtils.hasText(registryConfiguration.getSecret())) { // Use basic authentication to obtain the authorization token. // Usually the public docker hub authorization service doesn't require authentication for image pull requests. requestHttpHeaders.setBasicAuth(registryConfiguration.getUser(), registryConfiguration.getSecret()); } String registryAuthUri = registryConfiguration.getExtra().containsKey(DOCKER_REGISTRY_AUTH_URI_KEY) ? registryConfiguration.getExtra().get(DOCKER_REGISTRY_AUTH_URI_KEY) : DEFAULT_DOCKER_REGISTRY_AUTH_URI; UriComponents uriComponents = UriComponentsBuilder.newInstance() .fromHttpUrl(registryAuthUri).build().expand(imageRepository); ResponseEntity<Map> authorization = this.getRestTemplate(registryConfiguration) .exchange(uriComponents.toUri(), HttpMethod.GET, new HttpEntity<>(requestHttpHeaders), Map.class); Map<String, String> authorizationBody = (Map<String, String>) authorization.getBody(); final HttpHeaders responseHttpHeaders = new HttpHeaders(); responseHttpHeaders.setBearerAuth(authorizationBody.get(TOKEN_KEY)); return responseHttpHeaders; }
Example 7
Source File: DockerConfigJsonSecretToRegistryConfigurationConverter.java From spring-cloud-dataflow with Apache License 2.0 | 4 votes |
/** * @param registryHost * @param username * @param password * @return Returns Token Endpoint Url if dockeroauth2 authorization-type or null for basic auth. */ public String getDockerTokenServiceUri(String registryHost, String username, String password) { final HttpHeaders httpHeaders = new HttpHeaders(); if (StringUtils.hasText(username) && StringUtils.hasText(password)) { httpHeaders.setBasicAuth(username, password); } try { this.restTemplate.exchange( UriComponentsBuilder.newInstance().scheme("https").host(registryHost).path("v2/_catalog").build().toUri(), HttpMethod.GET, new HttpEntity<>(httpHeaders), Map.class); return null; } catch (HttpClientErrorException httpError) { if (httpError.getRawStatusCode() != 401) { return null; } if (!httpError.getResponseHeaders().containsKey("Www-Authenticate")) { return null; // Not Docker OAuth2 } List<String> wwwAuthenticate = httpError.getResponseHeaders().get("Www-Authenticate"); logger.info("" + wwwAuthenticate); Map<String, String> wwwAuthenticateAttributes = Stream.of(wwwAuthenticate.get(0).split(",")) .map(s -> s.split("=")) .collect(Collectors.toMap(b -> b[0], b -> b[1])); String tokenServiceUri = String.format("%s?service=%s&scope=repository:{repository}:pull", wwwAuthenticateAttributes.get("Bearer realm"), wwwAuthenticateAttributes.get("service")); // clear redundant quotes. tokenServiceUri = tokenServiceUri.replaceAll("\"", ""); logger.info("tokenServiceUri: " + tokenServiceUri); return tokenServiceUri; } catch (Exception e) { return null; } }