[英]CORS issue in Spring Boot + Spring Data Rest Repositories annotated with @CrossOrigin
I am having an issue with my spring boot + spring data rest application due to CORS.我的 spring 启动 + spring 数据 rest 应用程序出现问题,原因是 Z5A8FE.4B7B4BDE3BEEC29D This application exposes endpoints automatically created using
@RepositoryRestResource
on the JpaRepository
interface.此应用程序公开使用
@RepositoryRestResource
接口上的JpaRepository
自动创建的端点。
This is one example:这是一个例子:
@CrossOrigin
@RepositoryRestResource
public interface UserRepository extends JpaRepository<User, Long> {
....
}
Unfortunately, I am not getting the expected Access-Control-Allow-Origin header in the response.不幸的是,我在响应中没有得到预期的 Access-Control-Allow-Origin header。 Instead I am getting this header using Postman:
相反,我使用 Postman 得到这个 header:
I also tried configuring CORS in the RepositoryRestConfigurer
:我还尝试在
RepositoryRestConfigurer
中配置 CORS :
@Configuration
public class SpringDataRestCustomization implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.getCorsRegistry()
.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "PUT", "DELETE")
.exposedHeaders("Access-Control-Allow-Origin")
.allowCredentials(false).maxAge(3600);
}
}
Anybody having the same issue?有人有同样的问题吗?
Issue resolved!问题解决了!
I realised that you need to include the header "Origin" inside your request.我意识到您需要在请求中包含 header “Origin”。 If not, the response returned will not contain the header "Access-Control-Allow-Origin"
如果不是,返回的响应将不包含 header "Access-Control-Allow-Origin"
Stupid mistake but maybe it could avoid wasting time for somebody else.愚蠢的错误,但也许它可以避免为别人浪费时间。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.