springboot三种方式优雅解决web跨域访问cors问题,Filter,WebMvcConfiguration,@CrossOrigin

跨域访问时很多需求,所以springboot的跨域解决也是【必须的】
先建立测试过程,模拟cors问题发生,maven,springboot项目启动
http://127.0.0.1:8080/dd访问项目
返回的js会访问本机ip来模拟cors问题
springboot三种方式优雅解决web跨域访问cors问题,Filter,WebMvcConfiguration,@CrossOrigin_第1张图片

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhanghui
 * @date 2019/5/24
 */
@RestController
public class TestController {

    @RequestMapping("/dd")
    public Object dd(){

        String js="";

        return js;
    }

    @RequestMapping("/aaa")
    public Object dd(){
        return "aaa3";
    }

    @RequestMapping("/bbb")
    public Object ff(){
        return "fff3";
    }

}

springboot三种方式优雅解决,用最后一种,最【装逼】,以显示你熟悉springboot

  • 过滤器方式
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhanghui
 * @date 2019/7/2
 */
@Component
public class CorsFilter implements Filter {

    private final Logger log = LoggerFactory.getLogger(CorsFilter.class);

    public CorsFilter() {
        log.info("SimpleCORSFilter init");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        if (request.getHeader("Origin") != null) {
            response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        }

        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }
}
  • Web MVC Configuration
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author zhanghui
 * @date 2019/7/2
 */
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebMvcConfiguration.class);

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        LOGGER.info("WebMVC configuration : addCorsMappings");
        registry.addMapping("/**").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS");
    }
}
  • springboot自带跨域注解,可以放在RestController的类上或者方法上,还能自定义那些域名可以跨域,非常灵活
    @CrossOrigin
//    @CrossOrigin(origins = "*", allowedHeaders = "*")

springboot的三种方式优雅解决web跨域访问cors问题,你学会了吗?
springboot三种方式优雅解决web跨域访问cors问题,Filter,WebMvcConfiguration,@CrossOrigin_第2张图片

你可能感兴趣的:(springcloud)