1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

CORS issue when deploying to server

Discussão em 'Angular' iniciado por Luís Casqueiro, Novembro 5, 2024 às 19:02.

  1. I am developing a microservice java based project where an API forwards the front-end requests to the correct microservice. I have everything set up on docker and if I deploy it locally using docker my front end is able to do connect with backend and the app works as expected. However, when deploying to a server we can connect to the front end app however, when trying to authenticate the user(first step that connects to backend) the call is failing due to a CORS issue.

    Options

    Requests

    I can see its on the OPTIONS call but in my configuration I have allowed all requests in order to understand why it was happening but even with allow all origins I am still getting error.

    Here is my updated config that is still not working:

    CorsFilter:

    @Bean
    public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(false);
    config.setAllowedOrigins(Collections.singletonList("*"));
    config.setAllowedHeaders(Collections.singletonList("*"));
    config.setAllowedMethods(Arrays.asList(
    "GET",
    "POST",
    "DELETE",
    "PUT",
    "PATCH",
    "OPTIONS"
    ));
    source.registerCorsConfiguration("/**", config);
    return new CorsFilter(source);

    }


    I even added an explicit permission for options in order for it to be allowed but i still see the error:

    SecurityFilterChain:

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
    .cors(withDefaults())
    .csrf(AbstractHttpConfigurer::disable)
    .authorizeHttpRequests(req -> req.requestMatchers(
    "/auth/**",
    "/v2/api-docs",
    "/v3/api-docs",
    "/v3/api-docs/**",
    "/swagger-resources",
    "/swagger-resources/**",
    "/configuration/ui",
    "/configuration/security",
    "/swagger-ui/**",
    "/webjars/**",
    "/swagger-ui.html").permitAll()
    .requestMatchers(HttpMethod.OPTIONS).permitAll()
    .anyRequest()
    .authenticated())
    .sessionManagement(session -> session.sessionCreationPolicy(STATELESS))
    .authenticationProvider(authenticationProvider)
    .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);

    return http.build();
    }


    How should I proceed to solve this or to troubleshoot?

    Thanks in advance.

    Continue reading...

Compartilhe esta Página