【问题标题】:Spring data rest disable CORS on base-path [duplicate]Spring data rest在基本路径上禁用CORS [重复]
【发布时间】:2023-03-29 22:26:01
【问题描述】:

我正在 localhost:3000 上开发一个 REACT 应用程序,并在 localhost:8080 上运行一个 Spring Data Rest 后端。我发现了如何禁用特定 RestRepositories 的 CORS 策略,例如:

@CrossOrigin("*")
public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {
}

现在可以访问以下 URL:localhost:8080/api/employees

但是,我正在尝试对基本路径 url 本身执行 GET 请求:localhost:8080/api。这仍然被 CORS 策略阻止。我想禁用该 URL 上的 CORS 策略。

我找到了以下文档:https://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.configuring-cors 这适用于存储库本身,但在基本 URL 上没有结果。

非常感谢任何想法。

【问题讨论】:

    标签: cors spring-data-rest base-path


    【解决方案1】:

    如果您使用 create-react-app 创建了 react 应用,请将以下内容添加到 package.json:

    "proxy":"http://localhost:8080",
    

    这会将您的所有请求代理到后端服务器。这仅用于开发。

    【讨论】:

    • 我已将代理放在“package.json”中,但是出现了相同的 CORS 策略阻塞。还有其他想法吗?
    猜你喜欢
    • 2014-03-28
    • 2016-04-18
    • 2018-10-28
    • 2015-10-21
    • 2019-09-13
    • 2019-08-03
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多