SpringMVC跨域问题处理
1.创建crossFilter类
package com.cbsinfo.core.controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
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.springframework.stereotype.Component;
import com.cbsinfo.core.utils.soap.soapentity.SoapError;
import com.cbsinfo.core.utils.soap.soapentity.SoapRequest;
import net.sf.json.JSONObject;
@Component
public class CrossFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("跨域请求开始");
SoapRequest sReq=new SoapRequest();
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
httpServletRequest.getSession();
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setHeader("Access-Control-Allow-Origin", "*");
httpResponse.setHeader("Access-Control-Allow-Methods", "*");
httpResponse.setHeader("Access-Control-Max-Age", "3600");
httpResponse.setHeader("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
httpResponse.setHeader("Content-type", "application/json");
httpResponse.setHeader("Cache-Control", "no-cache, must-revalidate");
String responseSoap="";
StringBuilder reqSoapXml = new StringBuilder();
try { InputStream in = request.getInputStream();
InputStreamReader inReader = new InputStreamReader(in, "utf-8");
BufferedReader bufReader;
String line = (
bufReader = new BufferedReader(inReader))
.readLine();
while (line != null) {
reqSoapXml.append(line);
line = bufReader.readLine();
}
System.out.println("reqSoapXml.toString()="+reqSoapXml.toString());
JSONObject soapHead1 =JSONObject.fromObject(reqSoapXml.toString());
Map<String, String> a=new HashMap<String, String>();
JSONObject bodyjs =JSONObject.fromObject(soapHead1.get("body"));
System.out.println("js=="+bodyjs);
a= bodyjs;
sReq.setBody(a);
System.out.println("sReq="+sReq.toString());
System.out.println("请求参数="+soapHead1.toString());
request.setAttribute("sReq", sReq);
request.setAttribute("reqJson", reqSoapXml.toString());
chain.doFilter(request, httpResponse);
}catch (Exception e) {
SoapError soapError = new SoapError();
soapError.setErrorCode("300");
soapError.setErrorMsg("解析报文头异常:" + e.getMessage());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/soap+xml;charset=UTF-8");
PrintWriter writer;
(
writer = response.getWriter())
.write(responseSoap);
writer.flush();
}
}
}
2.web.xml中加载crossFilter
<filter>
<filter-name>cors</filter-name>
<filter-class>com.cbsinfo.core.controller.CrossFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>cors</filter-name>
<url-pattern>/cbs/*</url-pattern> <!--设置哪些接口需要做跨域处理-->
</filter-mapping>