我托管在http://localhost:4200的Angular Web应用程序发出了一个请求http://localhost:8888/mydomain/path 对于我的Spring Boot Java后端,如下所述:

棱角前端面:

const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json');
headers.set('Access-Control-Allow-Origin', '*');
headers.set('Access-Control-Allow-Methods', 'GET,POST,OPTIONS,DELETE,PUT');
this.http.get('http://localhost:8888/mydomain/path', { headers: headers })
    .subscribe(result => console.log(result));

在互联网上搜索,我在Java/Spring Boot后端找到了一个解决CORS来源问题的设置

@EnableWebSecurity
@Configuration
public class SecurityConfigurations extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers(HttpMethod.GET, "/*").permitAll()
            .antMatchers(HttpMethod.POST, "/*").permitAll()
            .antMatchers(HttpMethod.PUT, "/*").permitAll()
            .antMatchers(HttpMethod.DELETE, "/*").permitAll()
            .and().csrf().disable().cors().disable();
    }
    
    @Bean
    @Qualifier("corsFilter")
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("http://localhost:4200");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
    

}

但是GET调用返回了下面的错误.会有什么问题呢?我忘了做什么或做错了什么??非常感谢

'http://localhost:8888/mydomain/path' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

推荐答案

@J1s描述的解决方案解决了我的问题,我只需做一些小改动就可以让它在我的情况下发挥作用. 我将描述如下:

我在项目的根目录中创建了proxy.config.json文件

{
    "/api": {
        "target": "http://localhost:8888",
        "pathRewrite": {
            "^/api": "/mydomain"
          },
        "secure": false,
        "logLevel": "info"
    }
}

我在angular.json中添加了配置

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "myapp:build:production"
            },
            "development": {
              "browserTarget": "myapp:build:development",
              "proxyConfig": "proxy.config.json"
            }
          },
          "defaultConfiguration": "development"

      

最后,我对API的调用需要

http://localhost:4200/api/path

那样的话,一切都运行得很好. 感谢@J1s的解决方案!

Java相关问答推荐

Intellij显示项目语言级别最高为12,尽管有java版本17 SDK

Java函数式编程中的双值单值映射

使用联接和分页的SpringBoot Spring数据JPA

如何解释Java中for-each循环中对Iterable的强制转换方法引用?

自定义批注的外推属性值

尽管通过中断请求线程死亡,但线程仍将继续存在

Log4j与jdk21兼容吗?

解析方法";javax/imageio/metadata/IIOMetadata.getAsTree(Ljava/lang/String;)Lorg/w3c/dom/Node时加载约束冲突

如何使用Criteria Builder处理一对多关系中的空值?

FETCH类型设置为LAZY,但它仍会发送第二个请求

AbstractList保证溢出到其他方法

通过Java列表中的某些字段搜索值

在缺少字段时使用Jackson With Options生成Optional.Empty()

Java嵌套流查找任意值

当我将JTextField的getText函数与相等的String进行比较时;t返回true

始终使用Spring Boot连接mongodb上的测试数据库

关于正则表达式的一个特定问题,该问题与固定宽度负向后看有关

Keycloak + Spring Boot 角色认证不起作用

如何在 BlueJ 中自动关闭控制台

无法在 Android Studio 中访问 Record