Springboot Cors 配置适用于 Get 请求,但不适用于 Post 请求

问题描述 投票:0回答:1

我遇到以下情况,我配置了cors。我使用 tomcat10 托管我的应用程序,前端是 React。有了Springboot的开发服务器,本地一切正常。但当在生产中托管时,它应该起作用的地方却不起作用。这是我的配置

 @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception{
logger.info(keySetUri);

      httpSecurity.oauth2ResourceServer(
              c -> c.jwt(
                      j -> j.jwkSetUri(keySetUri)
              )
      );

        httpSecurity.cors(c -> {
            CorsConfigurationSource source = request -> {
                CorsConfiguration config = new CorsConfiguration();
                config.setAllowedOrigins(
                        List.of("http://192.000.0.000:3000", "http://localhost:3000", "http://www.exemple.com:3001", "http://www.example.com:8080", "http://www.example.com:8081")
                );
                config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS"));
                config.setAllowedHeaders(List.of("*"));

                return config;
            };
            c.configurationSource(source);
        });



        httpSecurity.csrf(c -> c.disable());

        httpSecurity.authorizeRequests()
               // .requestMatchers("/api/admin/**").authenticated() //cors não funciona para post
                .anyRequest().permitAll();
         ;

        return httpSecurity.build();

    }

我得到了标题 Access-Control-Allow-Origin http://www.example.com:3001 用于获取、选项,但不用于发布请求。我真的很困惑,因为代码就在那里,如果它不按照我编程的方式执行,我该怎么办?

这是我的获取请求标头

enter image description here

这是我的选项请求标头

enter image description here

这是我的 Java bug 发布请求的事情

enter image description here

spring-boot tomcat cors
1个回答
0
投票

原来我的帖子收到了一个在发送到S3之前临时存储的文件。 Tomcat 没有权限,Tomcat 发送了“cors 错误”。因此,如果您在不应该出现的地方出现了 Cors,请检查您的代码是否有错误。

© www.soinside.com 2019 - 2024. All rights reserved.