403在Spring RestAPI开发期间请求POST或PUT时出错
我试图在一个应用了Spring5+SpringSecurity5的项目中实现和使用RESTAPI 在本地运行Tomcat后与Postman一起测试时403在Spring RestAPI开发期间请求POST或PUT时出错,spring,spring-security,cors,http-status-code-403,Spring,Spring Security,Cors,Http Status Code 403,我试图在一个应用了Spring5+SpringSecurity5的项目中实现和使用RESTAPI 在本地运行Tomcat后与Postman一起测试时 url:http://localhost:8080/api~ 已确认get、post和put等请求正常工作 我把这个项目上传到https服务器上,在本地邮递员的服务器上测试api时 请求(如put和post)中发生403禁止的错误,get请求除外 当我在谷歌上搜索时,它说Spring Security的csrf问题+cors处理问题 所以我就这样
url:http://localhost:8080/api~
已确认get、post和put等请求正常工作
我把这个项目上传到https服务器上,在本地邮递员的服务器上测试api时
请求(如put和post)中发生403禁止的错误,get请求除外
当我在谷歌上搜索时,它说Spring Security的csrf问题+cors处理问题
所以我就这样修改了代码
- 网络安全配置
- WebMvcConfig
- 控制器
哪一部分错了?如果您让我知道,我将不胜感激:)在您的配置中通过HttpSecurity builder添加Cors过滤器。 i、 e 这将启用PUT和POST使用的cors飞行前请求。
您不需要包括CorsConfiguration源代码,只需确保控制器中也有@CrossOrigin注释。嗨,mandy,欢迎使用StackOverflow。这不是一个真正的答案。如果您想添加更多信息或提供其他示例,请编辑原始问题
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/").permitAll()
~
.anyRequest().permitAll()
.and()
.formLogin()
~
.and()
.logout()
~
.and()
.httpBasic().disable().cors().configurationSource(corsConfigurationSource())
.and()
.sessionManagement()
~
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setAllowCredentials(true);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfiguration);
return source;
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedOrigins("*");
}
@CrossOrigin("*")
Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.........