很多人都在询问如何从客户端更改de IP,但我需要从服务器端执行此操作(如果可能的话)。
问题是我从HttpServletRequest.getRemoteAddr()获取IP,但现在我们在IHS之前使用代理,这个IP总是带有代理IP,而不是真正的客户端IP。
代理在请求中的新特定标头中提供真实IP。
好的,我知道这听起来很简单,我只改变了获取IP的方式,并从代理提供的特定Header中获取IP。这是一个很好的解决方案,问题是我们必须改变很多类,并且我可以用另一种可能的方式思考,例如,在BaseClass中的Request中使用真实的IP更改一次IP,然后是每个人(谁扩展了BaseClass)正确地更新了IP。
那么,我能以某种方式这样做吗?
答案 0 :(得分:0)
您需要添加一个创建HttpServletRequestWrapper的Filter。调整部署描述符。
部分:
ProxyFilter:
package testingThings.wrapperAndFilter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet Filter implementation class ProxyFilter
*/
public class ProxyFilter implements Filter {
/**
* Default constructor.
*/
public ProxyFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// delegate to the Http related doFilter method
doFilter((HttpServletRequest)request, (HttpServletResponse)response, chain);
}
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
// create a wrapper
ProxyRequestWrapper replacementRequest = new ProxyRequestWrapper(request);
// pass the request along the filter chain
chain.doFilter(replacementRequest, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
ProxyRequestWrapper:
package testingThings.wrapperAndFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class ProxyRequestWrapper extends HttpServletRequestWrapper {
public ProxyRequestWrapper(HttpServletRequest request) {
super(request);
}
@Override
public String getRemoteAddr() {
return getHeader("REAL-IP");
}
}
<强>的web.xml 强>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<filter>
<filter-name>ProxyFilter</filter-name>
<filter-class>testingThings.wrapperAndFilter.ProxyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ProxyFilter</filter-name>
<url-pattern>/ServletBehindProxy</url-pattern>
</filter-mapping>
</web-app>
<强> ServletBehindProxy:强>
package testingThings.wrapperAndFilter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletBehindProxy
*/
@WebServlet("/ServletBehindProxy")
public class ServletBehindProxy extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// show ip
resp.getWriter().append(req.getRemoteAddr());
}
}
要测试它,您可以使用ModifyHeaders Browser-Plugin: