zoukankan      html  css  js  c++  java
  • Springboot设置跨域的三种方式

    原文地址:https://www.cnblogs.com/cchilei/p/13685606.html

    方式一(精细配置)

    在需要跨域的整个Controller或者单个方法上添加@CrossOrigin注解

    方式二(全局配置)

    @Configuration
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                    .maxAge(3600)
                    .allowCredentials(true);
        }
    }
    

    方式三(通过filter)

    @Component
    @WebFilter(urlPatterns = "/*", filterName = "authFilter") //这里的“/*” 表示的是需要拦截的请求路径
    public class PassHttpFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException { }
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;
            httpResponse.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
            httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
            httpResponse.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8080");
            filterChain.doFilter(servletRequest, httpResponse);
        }
        @Override
        public void destroy() { }
  • 相关阅读:
    JavaEE编程实验 实验1 Java常用工具类编程(未完成)
    设计模式(一)Chain Of Responsibility责任链模式
    JavaSE习题 第八章 线程
    JavaSE习题 第七章 常用实用类
    JavaSE习题 第六章 字符串和正则表达式
    JavaSE 字符串和正则表达式
    HDFS的Shell操作
    HDFS概述
    Hadoop完全分布式模式
    免密登陆
  • 原文地址:https://www.cnblogs.com/eyesfree/p/15127790.html
Copyright © 2011-2022 走看看