/**
* Project Name:cm2mmanage
* File Name:CrossSiteFilter.java
* Package Name:com.yoxnet.serverframework.base
* date:2016 April 27 4:52:51
* Copyright (c), [email protected] All rights Reserved.
*
*/
Package com.zhl.sms.filter;
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.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
/**
* Classname:crosssitefilter <br/>
* Function:todo ADD Function. <br/>
* Reason:todo ADD Reason. <br/>
* date:2016 April 27 pm 4:52:51 <br/>
* @author YYC
* @version
* @since JDK 1.6
* @see
*/
public class Crosssitefilter implements filter{
Private Logger logs = Loggerfactory.getlogger (This.getclass ());
@Override
public void init (Filterconfig filterconfig) throws Servletexception {
}
@Override
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
HttpServletRequest req = (httpservletrequest) request;
Enumeration E = Req.getheadernames ();
String headvalue = "";
while (E.hasmoreelements ()) {
Headvalue = (String) e.nextelement ();
String value = Req.getheader (Headvalue);
System.out.println (headvalue+ "=" +value);
// }
Logs.debug (">>>>>>>>>>>>>>> header information for this request is as follows:");
while (E.hasmoreelements ()) {
String name = (string) e.nextelement ();
String value = Req.getheader (name);
Logs.debug (name+ "=" +value);
}
Logs.debug (">>>>>>>>>>>>>>> End of header information for this request");
HttpServletResponse resp = (httpservletresponse) response;
HttpSession session = Req.getsession ();
Resp.addheader ("Access-control-allow-origin", "*");
Resp.addheader ("Access-control-allow-origin", "http://192.168.1.170");
Resp.addheader ("Access-control-allow-methods", "GET, POST, PUT, DELETE, OPTIONS");
Resp.addheader ("Access-control-allow-credentials", "true");
Resp.addheader ("Access-control-allow-headers", "X-requested-with,content-type");
Resp.addheader ("Access-control-max-age", "1800");//30 min
if (Req.getheader ("Access-control-request-method")! = null && "OPTIONS". Equals (Req.getmethod ())) {
}
Chain.dofilter (request, response);
}
@Override
public void Destroy () {
}
}
In the XML configuration:
<filter>
<filter-name>crossSiteFilter</filter-name>
<filter-class>com.zhl.sms.filter.CrossSiteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>crossSiteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
JSP Ajax Cross-domain problem how to deal with the reason: CORS header missing ' Access-control-allow-origin '). Ajax HTTP 415