gpt4 book ai didi

java - 已被 CORS 策略阻止 : Response to preflight request doesn't pass

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:18 24 4
gpt4 key购买 nike

我已经使用 Angular 和 SpringBoot 创建了一个应用程序,用于 Spring Security 的基本身份验证,但我收到 401 错误..我是 SpringBoot 新手

@Configuration
@EnableWebSecurity
public class SpringSecurityConfigurationBasicAuth extends WebSecurityConfigurerAdapter{

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS,"/**").permitAll()
.anyRequest().authenticated()
.and()
//.formLogin().and()
.httpBasic();
}
}

"Access to XMLHttpRequest at 'http://localhost:8080/hello-world/path-variable/MSD' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status."

最佳答案

我在 anglat7 和 spring boot 上也遇到了同样的问题,我通过添加以下配置解决了

 @Configuration
public class SpringDataRestConfiguration extends RepositoryRestConfigurerAdapter {

@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {

config.getCorsRegistry().addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("GET", "POST", "PATCH", "PUT", "DELETE");
}

}

关于java - 已被 CORS 策略阻止 : Response to preflight request doesn't pass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56946280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com