本文介绍了CORS Origin Spring Boot Jhipster-飞行前失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用jhipster v2.27.2
,我已通过取消注释应用程序中的行来启用cors。yml
jhipster:
异步:
corePoolSize:2
maxPoolSize:50
queueCapacity:10000
cors:#默认情况下未启用CORS。取消注释即可启用。
允许的来源: *
的允许方法:GET,PUT,POST,DELETE,OPTIONS
允许的标题: *
公开的标题:
allow-credentials:真实的
最大年龄:1800
在 WebConfigurer中
@Bean
public CorsFilter corsFilter(){
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = props.getCors();
if(config.getAllowedOrigins()!= null&&!config.getAllowedOrigins()。isEmpty()){
source.registerCorsConfiguration( / api / **,config);
source.registerCorsConfiguration( / v2 / api-docs,config);
source.registerCorsConfiguration( / oauth / **,config);
}
返回新的CorsFilter(source);
}
但是当我请求访问令牌时,仍然看到此错误
解决方案
类似于默认的 SecurityConfiguration
,它不会跳过OPTIONS的安全检查。
尝试将以下 antMatcher
添加到受保护的void configure(HttpSecurity http)
SecurityConfiguration.java
I am using jhipster v2.27.2I have enabled cors by uncommenting the lines in the application.yml
jhipster:
async:
corePoolSize: 2
maxPoolSize: 50
queueCapacity: 10000
cors: #By default CORS are not enabled. Uncomment to enable.
allowed-origins: "*"
allowed-methods: GET, PUT, POST, DELETE, OPTIONS
allowed-headers: "*"
exposed-headers:
allow-credentials: true
max-age: 1800
In the "WebConfigurer"
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = props.getCors();
if (config.getAllowedOrigins() != null && !config.getAllowedOrigins().isEmpty()) {
source.registerCorsConfiguration("/api/**", config);
source.registerCorsConfiguration("/v2/api-docs", config);
source.registerCorsConfiguration("/oauth/**", config);
}
return new CorsFilter(source);
}
But still when I request for the access token, I see this error
解决方案
Looks like in the default SecurityConfiguration
, its not skipping security check for OPTIONS.
Try adding the following antMatcher
to the protected void configure(HttpSecurity http)
method in SecurityConfiguration.java
.antMatchers(org.springframework.http.HttpMethod.OPTIONS, "/api/**").permitAll()
这篇关于CORS Origin Spring Boot Jhipster-飞行前失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!