过滤器:
package com.shenke.filter; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; @Component public class CorsFilter implements Filter { public CorsFilter() { super(); // TODO Auto-generated constructor stub System.out.println("过滤器启动..."); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletResponse response = (HttpServletResponse) servletResponse; HttpServletRequest req = (HttpServletRequest) servletRequest; // 注意:如果想要允许发送Cookie,那么Access-Control-Allow-Origin不能为"*" response.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin")); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT"); response.setHeader("Access-Control-Max-Age", "3628800"); response.setHeader("Access-Control-Allow-Headers", "*"); response.setHeader("Access-Control-Expose-Headers", "Content-disposition"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } }
标签:Control,Access,跨域,public,问题,解决,import,response,setHeader From: https://www.cnblogs.com/liweimingbk/p/17066116.html