一、编写Filter
package com.xxx.frame.aop;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.filter.OncePerRequestFilter;
public class CrossAccessFilter extends OncePerRequestFilter {
protected final Logger logger = LoggerFactory.getLogger(getClass());
public void crossAllows(HttpServletRequest request, HttpServletResponse response) {
String o = request.getHeader("Origin");
/* 允许跨域 */
response.setHeader("Access-Control-Allow-Origin", o);
/* 允许带cookie */
response.setHeader("Access-Control-Allow-Credentials", "true");
/* 允许请求的方式 */
response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
/* 支持带头 */
response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, backToken, tgVersion, platform");
/* 预检有效期 */
response.addHeader("Access-Control-Max-Age", "172800");
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
crossAllows(request, response);
chain.doFilter(request, response);
}
}
二、配置Filter
<filter>
<filter-name>crossFilter</filter-name>
<filter-class>com.xxx.frame.aop.CrossAccessFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>crossFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>