美文网首页
SpringBoot设置Cors跨域的四种方式

SpringBoot设置Cors跨域的四种方式

作者: 笨比乔治 | 来源:发表于2020-12-07 14:04 被阅读0次

前言:CorsFilter / WebMvcConfigurer / @CrossOrigin 需要SpringMVC 4.2 以上的版本才支持,对应SpringBoot 1.3 版本以上都支持这些CORS特性。不过,使用SpringMVC4.2 以下版本的小伙伴也不用慌,直接使用方式4通过手工添加响应头来授权CORS跨域访问也是可以的。

首先一点:跨域问题,后端解决,有如下四种方式。

方式1:返回新的CorsFilter

@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setMaxAge(3600L);
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }
 
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }
}

方式2:重写WebMvcConfigurer


@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
 
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                //当**Credentials为true时,**Origin不能为星号,需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
                .allowedOrigins("http://localhost:9527", "http://127.0.0.1:9527", "http://127.0.0.1:8082", "http://127.0.0.1:8083")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }
}

方式3:使用注解(@CrossOrigin)

@Controller
@RequestMapping("/admin/sysLog")
@CrossOrigin
public class SysLogController {
 
}

方式4:手工设置响应头(HttpServletResponse )

这种方式,可以自己手工加到,具体的controller,inteceptor,filter等逻辑里。

@RequestMapping("/test")
@ResponseBody
public String test(){
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8080");
return "success";
}

总结:以上是设置cors跨域后端解决的四种方式,本质都是类似最后一种设置响应头,不过都是各自包实现了不同的封装逻辑。

相关文章

  • SpringBoot2.x整合CORS解决跨域问题

    跨域问题系列文章 1. 同源策略与CORS(跨域请求的起源)2. SpringBoot2.x整合CORS解决跨域问...

  • 跨域【详解】

    本篇有四种方法跨域:CORS、JSONP、降域、window.postMessage() 1. CORS CORS...

  • ajax-cors跨域设置Access-Control-Allo

    CORS跨域设置 CORS(Cross-originresourcesharing),跨域资源共享,是一份浏览器技...

  • 如何解决跨域问题

    使用 Java 配置的方式 使用注解的方式 使用 CORS(跨资源共享)解决跨域问题 在header里面设置(在 ...

  • Ajax 请求和跨域

    跨域的几种方式: cors方式 cross-orign-resource-shareing(跨域)参考:http:...

  • 前端跨域

    CORS跨域 1.CORS跨域-服务端设置,前端直接调用说明:后台允许前端某个站点进行访问 2.JSONP跨域-前...

  • 响应头设置跨域和Spring注解跨域

    CORS跨域原理详解Spring解决跨域 响应头设置跨域 Spring注解跨域@CrossOrigin 可添加到方...

  • 跨域的解决方案

    1通过cors设置cors头解决跨域,也可以针对一个接口使用cors()中间件解决跨域 2通过设置响应头访问允许控...

  • Vue实现跨域请求

    一般解决跨域问题可以通过CORS跨域、JSONP和反向代理跨域。下面分别介绍这三种跨域方式: 1、CORS 以ne...

  • springboot设置cors跨域请求

    新建filter处理类 在SecurityConfig配置类中使用

网友评论

      本文标题:SpringBoot设置Cors跨域的四种方式

      本文链接:https://www.haomeiwen.com/subject/grvkiktx.html