web项目中,跨域问题

web项目中,跨域问题

本来前后端项目是不存在跨域的,但是是前后端配合的,便有了跨域的问题
百度中遇到一个很好的帖子,并且正确可用,边记录下来,以便后用
【web项目中,跨域问题】感谢原博主
转载 https://blog.csdn.net/lmy86263/article/details/51724221
转载 https://www.cnblogs.com/slowcity/p/9293498.html
自己在几个项目中,解决跨域的方法就是添加一个filter: CorsFilter
package com.basetnt.bss.filter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class CorsFilter implements Filter { private final static Logger logger = LoggerFactory.getLogger(CorsFilter.class); @Override 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, PUT"); response.setHeader("Access-Control-Max-Age", "36000"); response.setHeader("Access-Control-Allow-Headers", "Authorization"); response.setHeader("Access-Control-Expose-Headers", "*"); logger.debug("*********************************过滤器被使用**************************"); chain.doFilter(req, res); }@Override public void init(FilterConfig filterConfig) {}@Override public void destroy() {} }

    推荐阅读