【问题标题】:Is there a way to configure cross-origin scripting in Spring Boot from a properties file?有没有办法从属性文件中配置 Spring Boot 中的跨域脚本?
【发布时间】:2020-11-09 02:30:15
【问题描述】:

我正在使用 Spring Boot 2 和 Java 11。在本地开发时(前端是 React 应用程序),我通过创建配置文件并将其添加到其中来配置允许跨域请求

@Configuration
public class AppConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:3000")
                .allowedMethods("GET","POST","OPTIONS","PATCH","DELETE");
    }
}

是否有更简单的方法可以从属性文件或启用其他选项来执行此操作?我不喜欢仅仅为了适应我的本地环境而添加代码的想法。

【问题讨论】:

    标签: spring spring-security cors spring-boot-2


    【解决方案1】:

    您可以对处理程序方法以及控制器进行@CrossOrigin 注释以启用跨源。欲了解更多信息,请点击以下链接。

    https://spring.io/guides/gs/rest-service-cors/

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 2015-01-16
      相关资源
      最近更新 更多