public ProviderDiscoveryClient(String providerLocationUri) { Assert.hasText(providerLocationUri, "providerLocationUri cannot be empty"); try { this.providerLocation = UriComponentsBuilder.fromHttpUrl(providerLocationUri) .path(PROVIDER_END_PATH) .build() .encode() .toUri(); } catch (Exception ex) { throw new IllegalArgumentException("Invalid URI for providerLocationUri: " + ex.getMessage(), ex); } }
public EmailService(JavaMailSender mailSender, String loginUrl, String fromAddress) { this.mailSender = mailSender; this.loginUrl = loginUrl; // if we are provided a from address use that, if not fallback to default based on loginUrl if (fromAddress != null && !fromAddress.isEmpty()) { this.fromAddress = fromAddress; } else { String host = UriComponentsBuilder.fromHttpUrl(loginUrl).build().getHost(); this.fromAddress = "admin@" + host; } }
public static String getHostForURI(String uri) { UriComponentsBuilder b = UriComponentsBuilder.fromHttpUrl(uri); return b.build().getHost(); }
public static String addSubdomainToUrl(String url, String subdomain) { if (!hasText(subdomain)) { return url; } subdomain = subdomain.trim(); subdomain = subdomain.endsWith(".") ? subdomain : subdomain + "."; UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); builder.host(subdomain + builder.build().getHost()); return builder.build().toUriString(); }
@Test // SPR-14828 public void fromUriStringQueryParamEncodedAndContainingPlus() { String httpUrl = "http://localhost:8080/test/print?value=%EA%B0%80+%EB%82%98"; URI uri = UriComponentsBuilder.fromHttpUrl(httpUrl).build(true).toUri(); assertEquals(httpUrl, uri.toString()); }
@Test(expected = IllegalArgumentException.class) // SPR-10539 public void fromHttpUrlStringInvalidIPv6Host() { UriComponentsBuilder.fromHttpUrl("http://[1abc:2abc:3abc::5ABC:6abc:8080/resource").build().encode(); }
@Test // SPR-10779 public void fromHttpUrlStringCaseInsesitiveScheme() { assertEquals("http", UriComponentsBuilder.fromHttpUrl("HTTP://www.google.com").build().getScheme()); assertEquals("https", UriComponentsBuilder.fromHttpUrl("HTTPS://www.google.com").build().getScheme()); }
protected String validateAndConstructTokenKeyUrl(String keyUrl) { if (!UaaUrlUtils.isUrl(keyUrl)) { throw new IllegalArgumentException("Invalid Key URL"); } return UriComponentsBuilder.fromHttpUrl(keyUrl).scheme("https").path("/token_keys").build().toUriString(); } }
private String buildAuthorizationRequestUri() { MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); parameters.set(OAuth2ParameterNames.RESPONSE_TYPE, this.responseType.getValue()); parameters.set(OAuth2ParameterNames.CLIENT_ID, this.clientId); if (!CollectionUtils.isEmpty(this.scopes)) { parameters.set(OAuth2ParameterNames.SCOPE, StringUtils.collectionToDelimitedString(this.scopes, " ")); } if (this.state != null) { parameters.set(OAuth2ParameterNames.STATE, this.state); } if (this.redirectUri != null) { parameters.set(OAuth2ParameterNames.REDIRECT_URI, this.redirectUri); } if (!CollectionUtils.isEmpty(this.additionalParameters)) { this.additionalParameters.entrySet().stream() .filter(e -> !e.getKey().equals(OAuth2ParameterNames.REGISTRATION_ID)) .forEach(e -> parameters.set(e.getKey(), e.getValue().toString())); } return UriComponentsBuilder.fromHttpUrl(this.authorizationUri) .queryParams(parameters) .encode(StandardCharsets.UTF_8) .build() .toUriString(); } }
private String expandRedirectUri(HttpServletRequest request, ClientRegistration clientRegistration, String action) { // Supported URI variables -> baseUrl, action, registrationId // Used in -> CommonOAuth2Provider.DEFAULT_REDIRECT_URL = "{baseUrl}/{action}/oauth2/code/{registrationId}" Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("registrationId", clientRegistration.getRegistrationId()); String baseUrl = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) .replaceQuery(null) .replacePath(request.getContextPath()) .build() .toUriString(); uriVariables.put("baseUrl", baseUrl); if (action != null) { uriVariables.put("action", action); } return UriComponentsBuilder.fromUriString(clientRegistration.getRedirectUriTemplate()) .buildAndExpand(uriVariables) .toUriString(); } }
updateParamsForAuth(authNames, queryParams, headerParams); final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path); if (queryParams != null) { builder.queryParams(queryParams);
private boolean connectionAvailable() { UriComponents components = UriComponentsBuilder.fromHttpUrl(baseUrl).build(); String host = components.getHost(); int port = components.getPort(); logger.info("Testing connectivity for " + baseUrl); try (Socket socket = new Socket(host, port)) { logger.info("Connectivity test succeeded for " + baseUrl); return true; } catch (IOException e) { logger.warn("Connectivity test failed for " + baseUrl, e); return false; } } }
/** * Redirect the user according to the specified exception. * * @param e * The user redirect exception. * @param request * The request. * @param response * The response. */ protected void redirectUser(UserRedirectRequiredException e, HttpServletRequest request, HttpServletResponse response) throws IOException { String redirectUri = e.getRedirectUri(); UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(redirectUri); Map<String, String> requestParams = e.getRequestParams(); for (Map.Entry<String, String> param : requestParams.entrySet()) { builder.queryParam(param.getKey(), param.getValue()); } if (e.getStateKey() != null) { builder.queryParam("state", e.getStateKey()); } this.redirectStrategy.sendRedirect(request, response, builder.build() .encode().toUriString()); }
protected String getHealthUrl() { if (instance.getHealthUrl() != null) { return instance.getHealthUrl(); } return UriComponentsBuilder.fromHttpUrl(getManagementBaseUrl()) .path("/") .path(getHealthEndpointPath()) .toUriString(); }
private String expandRedirectUri(HttpServletRequest request, ClientRegistration clientRegistration) { String baseUrl = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) .replaceQuery(null) .replacePath(request.getContextPath()) .build() .toUriString(); Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("baseUrl", baseUrl); uriVariables.put("action", "login"); uriVariables.put("registrationId", clientRegistration.getRegistrationId()); return UriComponentsBuilder.fromUriString(clientRegistration.getRedirectUriTemplate()) .buildAndExpand(uriVariables) .toUriString(); }
public void testAutologinFlow(String contentType, Map body) throws Exception { webDriver.get(baseUrl + "/logout.do"); HttpHeaders headers = getAppBasicAuthHttpHeaders(); headers.add(HttpHeaders.CONTENT_TYPE, contentType); ResponseEntity<Map> autologinResponseEntity = restOperations.exchange(baseUrl + "/autologin", HttpMethod.POST, new HttpEntity<>(body, headers), Map.class); String autologinCode = (String) autologinResponseEntity.getBody().get("code"); String authorizeUrl = UriComponentsBuilder.fromHttpUrl(baseUrl) .path("/oauth/authorize") .queryParam("redirect_uri", appUrl) .queryParam("response_type", "code") .queryParam("scope", "openid") .queryParam("client_id", "app") .queryParam("code", autologinCode) .build().toUriString(); webDriver.get(authorizeUrl); webDriver.get(baseUrl); Assert.assertEquals(testAccounts.getUserName(), webDriver.findElement(By.cssSelector(".header .nav")).getText()); IntegrationTestUtils.validateAccountChooserCookie(baseUrl, webDriver); }
String redirectUri = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) .replaceQuery(null) .build()
@Test public void testClientIdMustBeConsistent() throws Exception { webDriver.get(baseUrl + "/logout.do"); HttpHeaders headers = getAppBasicAuthHttpHeaders(); Map<String, String> requestBody = new HashMap<>(); requestBody.put("username", testAccounts.getUserName()); requestBody.put("password", testAccounts.getPassword()); ResponseEntity<Map> autologinResponseEntity = restOperations.exchange(baseUrl + "/autologin", HttpMethod.POST, new HttpEntity<>(requestBody, headers), Map.class); String autologinCode = (String) autologinResponseEntity.getBody().get("code"); String authorizeUrl = UriComponentsBuilder.fromHttpUrl(baseUrl) .path("/oauth/authorize") .queryParam("redirect_uri", appUrl) .queryParam("response_type", "code") .queryParam("scope", "openid") .queryParam("client_id", "stealer_of_codes") .queryParam("code", autologinCode) .build().toUriString(); try { restOperations.exchange(authorizeUrl, HttpMethod.GET, null, Void.class); } catch (HttpClientErrorException e) { assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode()); } }
@Override protected String getManagementBaseUrl() { String baseUrl = instance.getManagementBaseUrl(); if (!StringUtils.isEmpty(baseUrl)) { return baseUrl; } if (isManagementPortEqual()) { return UriComponentsBuilder.fromHttpUrl(getServiceUrl()) .path("/") .path(getDispatcherServletPrefix()) .path(getManagementContextPath()) .toUriString(); } Ssl ssl = management.getSsl() != null ? management.getSsl() : server.getSsl(); return UriComponentsBuilder.newInstance() .scheme(getScheme(ssl)) .host(getManagementHost()) .port(getLocalManagementPort()) .path(getManagementContextPath()) .toUriString(); }
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString()); String redirectUri = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) .replaceQuery(null) .build()