拦截器里配置 package com.agx.ims.interceptor;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletR
package com.agx.ims.interceptor; import java.io.IOException; 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.HttpServletResponse; import org.springframework.stereotype.Component; /** * * 设置跨域请求 * */ @Component public class responseFilter implements Filter { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; response.setHeader("Access-Control-Allow-Origin", "*");//设置允许跨域的域名,*代表所有都可跨域访问 response.setHeader("Access-Control-Allow-Methods", "*");//支持提交的方法// 多个拼接成串"POST, GET, OPTIONS, DELETE" // response.setHeader("Access-Control-Max-Age", "3600"); // response.setHeader("Access-Control-Allow-Headers", "x-requested-with"); response.setHeader("Access-Control-Allow-Credentials", "true"); chain.doFilter(req, res); } public void init(FilterConfig filterConfig) {} public void destroy() {} }web.xml中配置
如何模拟跨域cors com.agx.ims.interceptor.responseFilter cors /* encodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true encodingFilter /*
1. 新建一个web项目A, 2. 新建一个web项目B, 3. 在A项目的前端页面中利用ajax请求项目B的一个接口 4. 两个项目在各自的tomcat容器跑起来