




如何在java web应用中实现跨域内容代理(非重定向的url重写)
Tuckey URLRewriteFilter 是一个功能强大的 Servlet 过滤器,适用于同应用上下文内的请求路径重写(如 /old → /new 或 /api/* → /internal/api/*),但它完全不具备跨域代理能力——它无法发起 HTTP 请求、转发响应体、修改响应头或处理远程服务器返回的内容。因此,像将 https://provider.com/BK/ 的资源透明映射到 https://myapp.com/(且浏览器地址栏不跳转、无重定向痕迹)这类需求,UrlRewriteFilter 本质上无法实现。
✅ 正确方案:使用反向代理(Reverse Proxy)
推荐以下两种生产级可行方式:
location / {
proxy_pass https://provider.com/BK/;
proxy_set_header Host provider.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Fo
rwarded-Proto $scheme;
# 修复响应中 Location、Content-Location 等含 provider.com 的绝对 URL(需配合 proxy_redirect)
proxy_redirect https://provider.com/BK/ /;
# 若后端返回相对路径则无需此步;若返回 /BK/css/app.css,可用 sub_filter 重写
sub_filter '="/BK/' '="/';
sub_filter_types *;
sub_filter_once off;
}⚠️ 注意:启用 sub_filter 需编译时包含 --with-http_sub_module;HTTPS 代理需确保 Nginx 信任 provider.com 的证书(或配置 proxy_ssl_verify off 仅限测试环境)。
@RestController
public class ProxyController {
private final WebClient webClient = WebClient.builder()
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(5 * 1024 * 1024))
.build();
@RequestMapping(value = "/**", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE})
public Mono> proxy(HttpServletRequest request, ServerHttpResponse response) {
String path = request.getRequestURI().substring(request.getContextPath().length());
String targetUrl = "https://provider.com/BK" + path; // 映射 / → /BK/
return webClient.method(HttpMethod.valueOf(request.getMethod()))
.uri(targetUrl)
.headers(headers -> {
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
headers.set(name, request.getHeader(name));
}
headers.remove("Host");
})
.body(BodyInserters.fromDataBuffers(
Flux.fromStream(() -> {
try {
return request.getInputStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}).map(DataBufferUtils::wrap)))
.exchange()
.flatMap(clientResponse -> {
// 复制响应头(排除不安全头)
clientResponse.headers().forEach((k, v) -> {
if (!"Transfer-Encoding".equalsIgnoreCase(k) && !"Content-Encoding".equalsIgnoreCase(k)) {
response.getHeaders().set(k, String.join(",", v));
}
});
return clientResponse.bodyToMono(byte[].class)
.map(body -> ResponseEntity.status(clientResponse.statusCode()).body(body));
})
.onErrorResume(e -> Mono.just(ResponseEntity.status(502).body("Proxy failed".getBytes())));
}
} ? 关键提醒:
总结:URL 重写 ≠ 反向代理。当目标是“隐藏源服务路径并复用当前域名”时,请放弃 Tuckey,坚定选择 Nginx、Apache HTTPD、Spring Cloud Gateway 或 Envoy 等专业反向代理方案。