|
1 | 1 | package com.orderflow.ecommerce.config; |
2 | 2 |
|
| 3 | +import java.util.List; |
| 4 | + |
3 | 5 | import org.springframework.context.annotation.Bean; |
4 | 6 | import org.springframework.context.annotation.Configuration; |
| 7 | +import org.springframework.http.HttpMethod; |
| 8 | +import org.springframework.http.HttpStatus; |
5 | 9 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
6 | 10 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
7 | 11 | import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
8 | 12 | import org.springframework.security.config.http.SessionCreationPolicy; |
9 | 13 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
10 | 14 | import org.springframework.security.crypto.password.PasswordEncoder; |
11 | 15 | import org.springframework.security.web.SecurityFilterChain; |
| 16 | +import org.springframework.security.web.authentication.HttpStatusEntryPoint; |
12 | 17 | import org.springframework.web.cors.CorsConfiguration; |
13 | 18 | import org.springframework.web.cors.CorsConfigurationSource; |
14 | 19 | import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
15 | 20 |
|
16 | | -import java.util.List; |
17 | | - |
18 | 21 | @Configuration |
19 | 22 | @EnableWebSecurity |
20 | 23 | public class SecurityConfig { |
21 | 24 |
|
22 | 25 | @Bean |
23 | 26 | public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
24 | 27 | http |
25 | | - .csrf(AbstractHttpConfigurer::disable) // Desabilita CSRF (comum em APIs REST) |
| 28 | + .csrf(AbstractHttpConfigurer::disable) |
26 | 29 | .cors(cors -> cors.configurationSource(corsConfigurationSource())) |
27 | | - .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // API sem estado |
| 30 | + .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) |
| 31 | + .httpBasic(AbstractHttpConfigurer::disable) |
| 32 | + .formLogin(AbstractHttpConfigurer::disable) |
| 33 | + .exceptionHandling(ex -> ex.authenticationEntryPoint( |
| 34 | + new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))) |
28 | 35 | .authorizeHttpRequests(auth -> auth |
29 | | - .anyRequest().permitAll() // Por enquanto, libera tudo para você não se travar |
| 36 | + .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() |
| 37 | + .requestMatchers( |
| 38 | + "/v3/api-docs/**", |
| 39 | + "/swagger-ui/**", |
| 40 | + "/swagger-ui.html" |
| 41 | + ).permitAll() |
| 42 | + .requestMatchers("/test/**").permitAll() |
| 43 | + .requestMatchers("/auth/**").permitAll() |
| 44 | + .requestMatchers(HttpMethod.GET, "/products/**", "/categories/**").permitAll() |
| 45 | + .requestMatchers("/products/**", "/categories/**").authenticated() |
| 46 | + .anyRequest().authenticated() |
30 | 47 | ); |
31 | | - |
| 48 | +// |
32 | 49 | return http.build(); |
33 | 50 | } |
34 | 51 |
|
35 | | - /** |
36 | | - * Origens do front local e do compose: Vite (5173) e nginx da web (4173). |
37 | | - * Evita {@code allowedOriginPatterns("*")} com {@code allowCredentials(true)}, combinação inválida na especificação CORS. |
38 | | - */ |
39 | 52 | @Bean |
40 | 53 | public CorsConfigurationSource corsConfigurationSource() { |
41 | 54 | CorsConfiguration configuration = new CorsConfiguration(); |
|
0 commit comments