//ArgsIsValidFilter .java Filter code list:
package com.hety.uitl;
import java.io.IOException;
import java.util.Enumeration;
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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class ArgsIsValidFilter implements Filter {
private static Log log = LogFactory.getLog(ArgsIsValidFilter.class);
public void destroy() {
}
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
String servername_str = request.getServerName();
String currentURI = request.getRequestURI();
Enumeration headerValues = request.getHeaders("Referer");
String tmpHeaderValue = "";
boolean isValid = true;
// Specify the page address that you want to skip the interception, and if you need to add it, add it directly to the array.
// "Advice"
String [] ignoreURIS={"/back/",
"/Info.jsp",
"/pzxx.jsp"
};
while (headerValues.hasMoreElements()) {
// Get the full path: for example" http://www.domain.com.cn:8023/front/zwgk/zwgk.jsp?id=1283 "
tmpHeaderValue = (String) headerValues.nextElement();
}
if(log.isInfoEnabled()){
log.info(" Obtained parameters url for : " + tmpHeaderValue );
log.info(" Systemically acquired url To: "+ currentURI);
}
if ("".equals(tmpHeaderValue)) {
isValid = false;
if(log.isInfoEnabled()){
log.info(" Obtained parameters url for : empty");
log.info(" Systemically acquired url To: "+ currentURI);
log.info(" System tip: Request may come from outland! ");
}
} else {
if(log.isInfoEnabled()){
log.info(" The parameter length obtained is :"+tmpHeaderValue.length());
}
tmpHeaderValue = tmpHeaderValue.toLowerCase();
servername_str = servername_str.toLowerCase();
int len = 0;
if (tmpHeaderValue.startsWith("https://")) {
len = 8;
} else if (tmpHeaderValue.startsWith("http://")) {
len = 7;
}
if(log.isInfoEnabled()){
log.info(" The string before interception is: " + tmpHeaderValue );
log.info( " From the first " + len + " Bit interception begins, and the interception length is: " + servername_str.length());
}
String tmp = tmpHeaderValue.substring(len, servername_str.length() + len);
if(log.isInfoEnabled()){
log.info(" The intercepted string is: " + tmp);
}
if (tmp.length() < servername_str.length()) { // The length is not enough
isValid = false;
if(log.isInfoEnabled()){
log.info(" The truncated string is not long enough , Requests may come from outlands! ");
}
} else if (!tmp.equals(servername_str)) {// Compare whether the string (host name) is the same
isValid = false;
if(log.isInfoEnabled()){
log.info(" Domain name match failed. Request from outland! ");
}
}
}
// Skip over specifying the page address to intercept
for (String ignoreURI : ignoreURIS) {
if(currentURI.contains(ignoreURI)){
isValid=true;
if(log.isInfoEnabled()){
log.info(" The system has skipped the check below url : "+currentURI);
}
}
}
if (!isValid) {
if(log.isInfoEnabled()){
log.info(" System prompt message: URL For a cross-domain request, redirect to the first page. ");
}
response.sendRedirect("/index.html");
} else {
arg2.doFilter(arg0, arg1);
}
}
public void init(FilterConfig arg0) throws ServletException {
}
}