【发布时间】:2021-02-13 05:28:45
【问题描述】:
我在 Spring Boot 和 Spring Security 中编写了一个 API,该 API 由 React 前端访问,get 方法运行良好。但是当谈到我们有一个选项作为预检的帖子时,它会返回 401 http 状态。随着我的发展,现在我只希望 cors 不会阻止任何东西。 Insomnia 或邮递员不会出现此错误,我可以在没有此错误的情况下执行请求。发生此错误的主要端点是 /oauth/token,即 Spring Boot 获取 Bearer 令牌的默认端点,我通过 react 应用发送到那里的帖子给出了此错误:
Access to XMLHttpRequest at 'http://localhost:8080/oauth/token' from origin
'http://localhost:3000' has been blocked by CORS policy: Response to preflight
request doesn't pass access control check: It does not have HTTP ok status.
我的弹簧配置:
网络配置
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "TRACE", "CONNECT");
}
}
@EnableWebSecurity
@EnableAuthorizationServer
@EnableResourceServer
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.csrf().disable();
}
}
在进行了一些搜索后,我认为这是 cors 或我未获得的某些许可,但会考虑其他方法。
【问题讨论】:
标签: java spring spring-boot spring-mvc spring-security