springBoot跨域问题

官方解决方案:https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

1.注解方式 (@CrossOrigin也可以放到类上)

@RestController
@RequestMapping("/account")
public class AccountController {

    @CrossOrigin
    @GetMapping("/{id}")
    public Account retrieve(@PathVariable Long id) {
        // ...
    }

    @DeleteMapping("/{id}")
    public void remove(@PathVariable Long id) {
        // ...
    }
}

2.全局配置方式 (Global CORS configuration)

@Configuration
public class CorsConfig implements WebMvcConfigurer {

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**");
  }
}

以上几种方式有可能会和自己的拦截器有冲突,所以官方提供了过滤器的解决方案

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Autowired
    private AuthInterceptor authInterceptor;

    @Autowired
    private LimitingInterceptor limitingInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*  鉴权拦截器 */
        registry.addInterceptor(authInterceptor).order(1).addPathPatterns("/toShortUrl", "/batchToShortUrl", "/test");
        /*  限流拦截器 */
        registry.addInterceptor(limitingInterceptor).order(2).addPathPatterns("/toShortUrl", "/batchToShortUrl", "/test");
    }

}

上面的拦截器配置方式因为优先级设置的过高(order(1)),但是因为业务需要不能更改,但是上面的跨域解决方式实际上也是使用拦截器实现的,官方实现如下:

所以可以使用下面方式来解决跨域问题

@Configuration
public class MyConfiguration {

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }
}

注意:请求时的参数Origin不能为null,以下测试方式时Origin就会为Null

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot 的问题可以通过配置来解决。下面是一种常见的解决方法: 在 Spring Boot 项目中,可以使用 `@CrossOrigin` 注解来处理问题。这个注解可以直接添加在 Controller 类或者方法上,用于指定允许的源。 示例代码如下: ```java @RestController @CrossOrigin(origins = "http://example.com") public class MyController { @GetMapping("/api/data") public String getData() { // 处理请求 return "data"; } } ``` 在上述示例中,`@CrossOrigin(origins = "http://example.com")` 指定了允许访问的源,即来自 http://example.com 的请求可以访问该接口。 如果需要允许多个源进行访问,可以使用数组形式指定多个源,如: ```java @CrossOrigin(origins = {"http://example.com", "http://another-domain.com"}) ``` 此外,还可以通过全局配置方式解决问题。在 Spring Boot 项目的配置类中添加如下配置: ```java @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://example.com") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } } ``` 上述配置中,`allowedOrigins` 指定允许访问的源,`allowedMethods` 指定允许的请求方法,`allowedHeaders` 指定允许的请求头,`allowCredentials` 表示是否允许发送身份凭证(如 cookie),`maxAge` 指定预检请求的有效期。 这是一种常见的解决 Spring Boot 问题的方法,你可以根据实际情况选择适合的方式来解决问题
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值