List of usage examples for org.springframework.http HttpHeaders remove
@Override
public List<String> remove(Object key)
From source file:org.cloudfoundry.identity.uaa.integration.util.IntegrationTestUtils.java
public static void clearAllButJsessionID(HttpHeaders headers) { String jsessionid = null;//from ww w .j a va 2s . c o m List<String> cookies = headers.get("Cookie"); if (cookies != null) { for (String cookie : cookies) { if (cookie.contains("JSESSIONID")) { jsessionid = cookie; } } } if (jsessionid != null) { headers.set("Cookie", jsessionid); } else { headers.remove("Cookie"); } }
From source file:org.cloudfoundry.identity.uaa.login.AbstractControllerInfo.java
protected HttpHeaders getRequestHeaders(HttpHeaders headers) { // Some of the headers coming back are poisonous apparently // (content-length?)... HttpHeaders outgoingHeaders = new HttpHeaders(); outgoingHeaders.putAll(headers);/*from ww w .j av a2 s . c om*/ outgoingHeaders.remove(HOST); outgoingHeaders.remove(HOST.toLowerCase()); outgoingHeaders.set(HOST, getUaaHost()); logger.debug("Outgoing headers: " + outgoingHeaders); return outgoingHeaders; }
From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaController.java
@RequestMapping(value = "/oauth/authorize", params = "response_type") public ModelAndView startAuthorization(HttpServletRequest request, @RequestParam Map<String, String> parameters, Map<String, Object> model, @RequestHeader HttpHeaders headers, Principal principal) throws Exception { String path = extractPath(request); MultiValueMap<String, String> map = new LinkedMaskingMultiValueMap<String, String>(); map.setAll(parameters);/*from w w w.j av a 2s . c om*/ String redirectUri = parameters.get("redirect-uri"); if (redirectUri != null && !redirectUri.matches("(http:|https:)?//.*")) { redirectUri = "http://" + redirectUri; map.set("redirect-uri", redirectUri); } if (principal != null) { map.set("source", "login"); map.setAll(getLoginCredentials(principal)); map.remove("credentials"); // legacy cf might break otherwise map.remove("password"); // request for token will not use password } else { throw new BadCredentialsException("No principal found in authorize endpoint"); } HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.putAll(getRequestHeaders(headers)); requestHeaders.remove(AUTHORIZATION.toLowerCase()); requestHeaders.remove(USER_AGENT); requestHeaders.remove(ACCEPT.toLowerCase()); requestHeaders.remove(CONTENT_TYPE.toLowerCase()); requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); requestHeaders.remove(COOKIE); requestHeaders.remove(COOKIE.toLowerCase()); @SuppressWarnings("rawtypes") ResponseEntity<Map> response; response = authorizationTemplate.exchange(getUaaBaseUrl() + "/" + path, HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(map, requestHeaders), Map.class); saveCookie(response.getHeaders(), model); @SuppressWarnings("unchecked") Map<String, Object> body = response.getBody(); if (body != null) { // User approval is required logger.debug("Response: " + body); model.putAll(body); model.put("links", getLinksInfo()); if (!body.containsKey("options")) { String errorMsg = "No options returned from UAA for user approval"; if (body.containsKey("error")) { throw OAuth2Exception.create((String) body.get("error"), (String) (body.containsKey("error_description") ? body.get("error_description") : errorMsg)); } else { throw new OAuth2Exception(errorMsg); } } logger.info("Approval required in /oauth/authorize for: " + principal.getName()); return new ModelAndView("access_confirmation", model); } String location = response.getHeaders().getFirst("Location"); if (location != null) { logger.info("Redirect in /oauth/authorize for: " + principal.getName()); // Don't expose model attributes (cookie) in redirect return new ModelAndView(new RedirectView(location, false, true, false)); } throw new IllegalStateException("Neither a redirect nor a user approval"); }
From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaController.java
@RequestMapping(value = { "/oauth/token" }, params = "grant_type=password") @ResponseBody/* w w w .ja v a 2s . c o m*/ public ResponseEntity<byte[]> passwordGrant(HttpServletRequest request, @RequestHeader("Authorization") String authorization, @RequestHeader HttpHeaders headers, @RequestBody MultiValueMap<String, String> originalBody, Map<String, Object> model, Principal principal) throws Exception { logger.info("Passing through password grant token request for " + request.getServletPath()); Set<String> maskedAttribute = new HashSet<>(); maskedAttribute.add("password"); maskedAttribute.add("client_secret"); LinkedMaskingMultiValueMap<String, String> body = new LinkedMaskingMultiValueMap<>(maskedAttribute); for (Map.Entry<String, List<String>> entry : originalBody.entrySet()) { body.put(entry.getKey(), entry.getValue()); } body.setAll(getLoginCredentials(principal)); //for grant_type=password, we want to do user authentication //in the login server rather than in UAA String[] basic = extractAndDecodeHeader(authorization); //create a modifiable list headers = getRequestHeaders(headers); headers.remove(AUTHORIZATION); headers.remove(AUTHORIZATION.toLowerCase()); body.remove("client_id"); body.add("client_id", basic[0]); body.add("client_secret", basic[1]); body.add("source", "login"); //remove multiple values as the UAA can't handle it body.remove("grant_type"); if (!extractPath(request).contains("grant_type")) { body.add("grant_type", "password"); } HttpEntity entity = new HttpEntity(body, headers); return passthru(request, entity, model, true); }
From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaController.java
protected ResponseEntity<byte[]> passthru(HttpServletRequest request, HttpEntity entity, Map<String, Object> model, boolean loginClientRequired) throws Exception { String path = extractPath(request); RestOperations template = loginClientRequired ? getAuthorizationTemplate() : getDefaultTemplate(); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.putAll(getRequestHeaders(entity.getHeaders())); requestHeaders.remove(COOKIE); requestHeaders.remove(COOKIE.toLowerCase()); // Get back end cookie if saved in session String cookie = (String) model.get(COOKIE_MODEL); if (cookie != null) { logger.debug("Found back end cookies: " + cookie); for (String value : cookie.split(";")) { requestHeaders.add(COOKIE, value); }//from w w w . ja v a2 s . c o m } ResponseEntity<byte[]> response = template.exchange(getUaaBaseUrl() + "/" + path, HttpMethod.valueOf(request.getMethod()), new HttpEntity(entity.getBody(), requestHeaders), byte[].class); HttpHeaders outgoingHeaders = getResponseHeaders(response.getHeaders()); return new ResponseEntity<byte[]>(response.getBody(), outgoingHeaders, response.getStatusCode()); }
From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaController.java
protected HttpHeaders getResponseHeaders(HttpHeaders headers) { // Some of the headers coming back are poisonous apparently // (content-length?)... HttpHeaders outgoingHeaders = new HttpHeaders(); outgoingHeaders.putAll(headers);/* w ww . ja v a 2 s . co m*/ if (headers.getContentLength() >= 0) { outgoingHeaders.remove(CONTENT_LENGTH); outgoingHeaders.remove(CONTENT_LENGTH.toLowerCase()); } if (headers.containsKey(TRANSFER_ENCODING)) { outgoingHeaders.remove(TRANSFER_ENCODING); outgoingHeaders.remove(TRANSFER_ENCODING.toLowerCase()); } return outgoingHeaders; }
From source file:org.cloudfoundry.identity.uaa.ServerRunning.java
public ResponseEntity<Void> postForRedirect(String path, HttpHeaders headers, MultiValueMap<String, String> params) { ResponseEntity<Void> exchange = postForResponse(path, headers, params); if (exchange.getStatusCode() != HttpStatus.FOUND) { throw new IllegalStateException( "Expected 302 but server returned status code " + exchange.getStatusCode()); }// w ww.j a va2s .c om headers.remove("Cookie"); if (exchange.getHeaders().containsKey("Set-Cookie")) { for (String cookie : exchange.getHeaders().get("Set-Cookie")) { headers.add("Cookie", cookie); } } String location = exchange.getHeaders().getLocation().toString(); return client.exchange(location, HttpMethod.GET, new HttpEntity<Void>(null, headers), Void.class); }