SpringBoot 跨域解决方案

什么是跨域?       

 跨域资源共享(CORS) 是一种机制,它使用额外的 HTTP 头来告诉浏览器 让运行在一个 origin (domain) 上的Web应用被准许访问来自不同源服务器上的指定的资源。当一个资源从与该资源本身所在的服务器不同的域、协议或端口请求一个资源时,资源会发起一个跨域 HTTP 请求。

比如:http://cyun.top

http://cyun.top/home  不跨域

http://cyun.top:8080  跨域 端口不同

https://cyun.top  跨域   协议不同

http://admin.cyun.top   跨域   域名不同

常见跨域解决几种方式

1、使用@CrossOrigin

a.方法上加

@CrossOrigin(origins = "*") 

@PostMapping("/login") 

public String login(@RequestBody User user){

        return "";

}

b.类上加(origin="*" 代表所有域名都可访问  maxAge表示Cookie的有效期 单位为秒)

@CrossOrigin(origins = "*",maxAge = 1800)

public class UserController {

}

2、使用WebMvcConfigurer 配置

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOrigins("*")
            .allowCredentials(true)
            .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
            .maxAge(1800);
    }
}

3、使用CorsFilter

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);
        return new CorsFilter(configSource);
    }
}

4、网关配置(springcloud)

spring:
  application:
    name: gateway-service
  profiles:
    active: default

  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
              allowedOrigins: "*"   #跨域处理运行所有跨域
              allowedMethods:        #支持方法
                -GET
                -POST
                -PUT
                -DELETE
              allowedHeaders: "*"
              allowCredentials: true

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot提供了多种解决跨域问题的方法。其一种方法是使用@CrossOrigin注解来实现方法级别的细粒度的跨域控制。你可以在类或者方法上添加该注解。如果在类上添加该注解,该类下的所有接口都可以通过跨域访问。如果在方法上添加注解,那么仅仅只限于加注解的方法可以访问。示例代码如下: @RestController @RequestMapping("/user") @CrossOrigin public class UserController { @Autowired private UserService userService; @RequestMapping("/findAll") public Object findAll(){ return userService.list(); } } 另一种方法是实现WebMvcConfigurer接口。这种方式需要注意的是,CorFilter、WebMvcConfigurer和@CrossOrigin需要SpringMVC 4.2以上版本才支持,对应Spring Boot 1.3版本以上。上面前两种方式属于全局CORS配置,后两种属于局部CORS配置。如果使用了局部跨域,会覆盖全局跨域的规则,所以可以通过@CrossOrigin注解来进行细粒度更高的跨域资源控制。无论哪种方案,最终目的都是修改响应头,向响应头添加浏览器所要求的数据,进而实现跨域。 #### 引用[.reference_title] - *1* *3* [SpringBoot解决跨域问题的六种方式](https://blog.csdn.net/qq_46028126/article/details/123721540)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot项目针对跨域问题的三种解决方案](https://blog.csdn.net/weixin_45721835/article/details/124590962)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值