content: package com.bbgcine.overview.config;
  
  import com.bbgcine.overview.jwt.JwtAuthorizationFilter;
  import com.bbgcine.overview.jwt.JwtUserDetailsService;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.HttpMethod;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
  import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.http.SessionCreationPolicy;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.web.SecurityFilterChain;
  import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  import org.springframework.web.cors.CorsConfiguration;
  import org.springframework.web.cors.CorsConfigurationSource;
  import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  import org.springframework.beans.factory.annotation.Value;
  
  import java.util.Arrays;
  import java.util.List;
  
  @EnableMethodSecurity
  @Configuration
  public class SpringSecurityConfig {
  
      @Value("${frontend.origin}")
      private String frontendOrigin;
  
      
  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http, JwtUserDetailsService userDetailsService) throws Exception {
      return http
              .cors(cors -> cors.configurationSource(corsConfigurationSource()))
              .csrf(csrf -> csrf.disable())
              .formLogin(form -> form.disable())
              .httpBasic(basic -> basic.disable())
              .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
              .authorizeHttpRequests(auth -> {
                  auth.requestMatchers(
                          "/swagger-ui/**",
                          "/v3/api-docs/**",
                          "/docs-bbg-cine-api.html", 
                              
                          "/docs-bbg-cine-api/**"    
                  ).permitAll();
  
                  // PERMITE ACESSO PÚBLICO aos endpoints de autenticação e registro
                  auth.requestMatchers(HttpMethod.POST, "/api/v1/auth/**").permitAll(); 
                  auth.requestMatchers(HttpMethod.POST, "/api/register/**").permitAll(); 
                      auth.requestMatchers(HttpMethod.GET, "/api/register/ping**").permitAll();
                  
                  // EXIGE AUTENTICAÇÃO para todas as outras requisições
                  auth.anyRequest().authenticated();
              })
              .addFilterBefore(new JwtAuthorizationFilter(userDetailsService), UsernamePasswordAuthenticationFilter.class) //
              .build();
  }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
              throws Exception {
          return authenticationConfiguration.getAuthenticationManager();
      }
  
      @Bean
      public CorsConfigurationSource corsConfigurationSource() {
          CorsConfiguration configuration = new CorsConfiguration();
          // configuration.setAllowedOrigins(Arrays.asList("http://localhost:3000"));
          configuration.setAllowedOrigins(List.of(frontendOrigin));
          configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
          configuration.setAllowedHeaders(Arrays.asList("*"));
          configuration.setAllowCredentials(true);
          UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
          source.registerCorsConfiguration("/**", configuration);
          return source;
      }
  }