簡體   English   中英

Spring 引導在 aws fargate 上運行但不在本地時被 cors 阻止

[英]Spring boot blocked by cors when running on aws fargate but not locally

這是我的第一篇文章,如果我沒有包含您需要幫助我的所有正確信息,請原諒我。

我創建了一個 spring 引導后端服務器 Rest API 使用 Z2A2D595E6ED96A0B24F0ZFZFZ 安全性。 當我通過我的反應項目調用此 api 時,我最初收到 500 狀態響應,告訴我請求已被阻止為 CORS。 我解決了以下 corsConfig 文件的問題:

@Configuration
public class CorsConfig  {

    @Bean
    public WebMvcConfigurer corsConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedMethods("GET", "POST", "PUT", "DELETE")
                        .allowedHeaders("*")
                        .allowedOrigins("*");
            }
        };
    }

}

當我運行反應項目(本地主機:3000)和 spring api(本地主機:8080)時,一切正常。 我什至可以從標題中看到它返回“Access-Control-Allow-Origin:*”。

下面也是我的 spring 安全配置:

@Override
    protected void configure(HttpSecurity http) throws Exception {

        http.cors().and();
        http.csrf().disable().authorizeRequests()
                .antMatchers("/api/auth/**").permitAll()
                .anyRequest().authenticated()
        .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.cors();
        http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }

The issue i am having is that i have now deployed my spring boot api on a docker container running on aws ecs fargate, and i now get the 500 status response again and i can also see that the header "Access-Control-Allow-Origin : *" 不再出現在響應中。

所以快速總結一下。 在本地一切正常,只有當我調用運行 AWS 的后端 api 時,問題才會出現。

我還嘗試在我的restController class 上使用@CrossOrigin(origins = "*")。

我在那里發現了類似的問題,但它們似乎都是當人們在本地運行他們的應用程序時,並且已經被我已經使用的解決方案修復了。

在我看來,這個問題與 AWS 有關,但我不確定。 我希望有人可以在這里為我指明正確的方向,如果我需要提供更多截圖,請告訴我。 謝謝你。

有時隱藏在 CROS 后面的其他內部服務器錯誤,請嘗試檢查應用程序日志,如果可能將它們重定向到 cloudWatch 日志。

我懷疑Access-Control-Allow-Origin會導致您的 500 錯誤,這不是問題的主要來源,您的代碼的其他部分會導致問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM