在 azure 上部署的 Angular 和 Spring Boot 项目未按预期工作

编程


我已经在它自己的应用程序服务中部署了 Angular 项目,还在它自己的应用程序服务中部署了 Spring Boot,这两个应用程序在本地工作正常,但问题出现在生产中我收到此错误“跨源请求被阻止:同源策略不允许读取远程资源位于 https://mineralsportal-api.azurewebsites.us/auth_api/authenticate。(原因:CORS 标头“Access-Control-Allow-Origin”丢失)。状态代码:503”我不知道错误来自哪里自从我配置跨源类以在 Spring Boot 中启用资源共享和 corsFilter 类以来,但我在 azure(生产)上收到此错误,即使它在本地工作得很好。

我尝试过的:

这是我的 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")
                    .allowedOrigins("*")
                    .allowedHeaders("*");
            }
        };
    }
}

这是我的 corsFilter 类

@Component
public class CorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    response.setHeader("Access-Control-Expose-Headers", "Location");
    chain.doFilter(req, res);
    }
    
    @Override
    public void init(FilterConfig filterConfig) {}

    @Override
    public void destroy() {}

}

这是我的 securityConfig 类

@Configuration
@EnableWebSecurity
@SuppressWarnings("deprecation")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserService userService;

    @Autowired
    private RequestFilter requestFilter;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.cors().disable();
    http.csrf().disable();
    http.authorizeRequests().antMatchers("/auth_api/authenticate").permitAll().anyRequest().authenticated()
    .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    http.addFilterBefore(requestFilter, UsernamePasswordAuthenticationFilter.class);
    }
}

经过大量谷歌搜索后,我无法获得可以帮助我解决问题的最佳答案,任何建议我都将非常感激。

在azure中有什么方法可以部署两个应用程序以在同一个应用程序服务(Angular + Spring Boot)上运行,而无需将它们打包在同一个jar文件上?

解决方案1

问题不在于您的代码,而在于您的部署。 您已声明已将应用程序部署到 Azure,这带来了自己的一系列期望。 基本上,CORS 请求被 Azure 阻止,而不是被您的应用程序阻止。 你需要做的是自己管理 CORS 能力 – 查看文档 这里[^] 了解如何在 Azure 中管理 CORS。

[Edit – I posted this response without seeing that this was an old question. The answer still stands though, and could be useful to others in the future.]

コメント

タイトルとURLをコピーしました