攻击是因为利用了Request.getServername(),故单独将url访问的IP、端口号等单独拎出来,在配置文件中进行配置,以防攻击。
eg:
<%@ page import="com.nj.util.IPPropertiesUtil" %>
<%@ page import="java.io.IOException" %>
<%
IPPropertiesUtil iPpro;
try{
//为了解决host头攻击,配置文件中配置ip地址:localhost=ip地址
iPpro = new IPPropertiesUtil();
String iplocalhost = iPpro.readValue("localhost");
session.setAttribute("iplocalhost",iplocalhost);
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+session.getAttribute("iplocalhost")+":"+request.getServerPort()+path+"/";
%>