我正在尝试使用 tomcat 10 url-rewrite 实现反向代理。我扩展了 RewriteValue 类并覆盖了 invoke()。
public class DynamicRewriteValue extends RewriteValve{
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
String requestURI = request.getRequestURI();
// Apply dynamic rules for redirection
if (isBlacklisted(requestURI)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (isWhitelisted(requestURI)) {
// Perform the whitelisted redirect
System.out.println("request context is : "+request.getContextPath());
response.sendRedirect("http://localhost:8888/new-url");
return;
}
super.invoke(request, response);
}
public boolean isBlacklisted(String url){
return url.contains("black");
}
public boolean isWhitelisted(String url){
return url.contains("white");
}
}
我已经在 server.xml 文件中指定了这个类。
<Valve className="org.example.rewrite.DynamicRewriteValue" />
并将 jar 导入到 lib 文件中。
我的问题是请求被发送到指定的端点但请求没有被代理。即如果我访问
http://localhost:8080/white
请求转发给
http://localhost:8888/new-url
但我需要的请求应该像
http://localhost:8080/new-url
它应该从“http://localhost:8888/new-url”得到结果。
谁能帮我理解我们如何使用 tomcat 10 url-rewrite 实现反向代理?