angular - Spring boot - 预检响应没有 HTTP ok 状态
问题描述
我正在使用 Angular 5 制作网页,每次尝试发出GET
请求时都会收到此错误。我在这里阅读了大量的答案,但没有一个对我有用。
正如我所读的那样,因为我正在向这个请求添加自定义标头,这需要完成,因为我使用的是我认为导致问题的 Spring Security。这是我当前的 Spring Security 配置,我通过阅读问题制作但仍然无法正常工作,我不知道我是否做错了什么:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors()
.and()
.authorizeRequests().anyRequest().permitAll()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.csrf().disable();
}
@Override
public void configure(WebSecurity web ) throws Exception
{
web.ignoring().antMatchers( HttpMethod.OPTIONS, "/**" );
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
configuration.setExposedHeaders(Arrays.asList("x-auth-token"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
希望你能帮上忙,因为我已经为此苦苦挣扎了几天。我认为显然这里的问题是CORS
,我的GET
请求被转换为OPTIONS
自定义的一个 becheaders
和Spring Security
。
另外我想提一下,我正在使用带有 Jersey 的 Spring Boot。
谢谢。
解决方案
我能够像这样解决这个问题:
我的 WebMvc 配置:
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**");
}
}
我的安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().disable()
.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.anyRequest()
.fullyAuthenticated()
.and()
.httpBasic()
.and()
.csrf().disable();
}
推荐阅读
- r - foreach %dopar% 写入同一个文件
- bash - 后台进程与子shell的 SIGINT 传播
- signtool - Signtool SignerSign() 错误 (-1073700864/0xc000a000)
- c# - 我可以将 Entity Framework Core 与 ASP.NET MVC(非核心)一起使用吗?
- python - Python:在 torch.utils.data.DataLoader 对象上调用 iter 时出现 BrokenPipeError
- r - R - 提取时间以及作为字符串一部分的时区
- python - matplotlib xkcd and black figure background
- python-3.x - ServerSelectionTimeoutError: basicdatabase-w4eg3.mongodb.net:27017: [Errno 11001] getaddrinfo 失败
- php - 无论如何都不显示注销
- reactjs - 在 React 项目中包含一个 css 文件