RequestProvider.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.gateway.provider;
  18. import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
  19. import org.springframework.http.server.reactive.ServerHttpRequest;
  20. import org.springframework.util.MultiValueMap;
  21. import org.springframework.web.server.ServerWebExchange;
  22. import org.springframework.web.util.UriComponentsBuilder;
  23. import java.net.URI;
  24. import java.util.LinkedHashSet;
  25. /**
  26. * RequestProvider
  27. *
  28. * @author Chill
  29. */
  30. public class RequestProvider {
  31. /**
  32. * 获取原始url
  33. *
  34. * @param exchange
  35. * @return
  36. */
  37. public static String getOriginalRequestUrl(ServerWebExchange exchange) {
  38. ServerHttpRequest request = exchange.getRequest();
  39. LinkedHashSet<URI> uris = exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
  40. URI requestUri = uris.stream().findFirst().orElse(request.getURI());
  41. MultiValueMap<String, String> queryParams = request.getQueryParams();
  42. return UriComponentsBuilder.fromPath(requestUri.getRawPath()).queryParams(queryParams).build().toUriString();
  43. }
  44. }