CORS Origin Spring Boot Jhipster-飞行前失败 [英] CORS Origin Spring Boot Jhipster - pre-flight fails

查看:122
本文介绍了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);
}

但是当我请求访问令牌时,仍然看到此错误


http:// localhost:8080 / oauth / token?username = admin& password = admin& grant_type = password& scope = read
对预检请求的响应未通过访问控制检查:所请求的
资源上没有
标头 Access-Control-Allow-Origin。因此,不允许
访问原始地址‘ http:// localhost:9090 。响应的HTTP状态代码为401。



解决方案

类似于默认的 SecurityConfiguration ,它不会跳过OPTIONS的安全检查。



尝试将以下 antMatcher 添加到受保护的void configure(HttpSecurity http) SecurityConfiguration.java

中的code>方法。$ ant $$$$$$$$$ <$$$$ org.springframework.http.HttpMethod.OPTIONS, / api / **)。permitAll()


I am using jhipster v2.27.2 I 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

http://localhost:8080/oauth/token?username=admin&password=admin&grant_type=password&scope=read. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9090' is therefore not allowed access. The response had HTTP status code 401.

解决方案

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-飞行前失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆