|
| 1 | +package ftn.security.minikms.config; |
| 2 | + |
| 3 | +import ftn.security.minikms.service.auth.JwtAuthenticationFilter; |
| 4 | +import ftn.security.minikms.service.auth.MiniKmsUserDetailsService; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.http.HttpMethod; |
| 10 | +import org.springframework.security.authentication.AuthenticationManager; |
| 11 | +import org.springframework.security.config.Customizer; |
| 12 | +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; |
| 13 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 14 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 15 | +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
| 16 | +import org.springframework.security.config.http.SessionCreationPolicy; |
| 17 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 18 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 19 | +import org.springframework.security.web.SecurityFilterChain; |
| 20 | +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; |
| 21 | +import org.springframework.web.cors.CorsConfiguration; |
| 22 | +import org.springframework.web.cors.CorsConfigurationSource; |
| 23 | +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +@Configuration |
| 28 | +@EnableWebSecurity |
| 29 | +public class SecurityConfig { |
| 30 | + private final String jwtSecret; |
| 31 | + private final MiniKmsUserDetailsService service; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + public SecurityConfig( |
| 35 | + @Value("${jwt.secret}") String jwtSecret, |
| 36 | + MiniKmsUserDetailsService service) { |
| 37 | + this.jwtSecret = jwtSecret; |
| 38 | + this.service = service; |
| 39 | + } |
| 40 | + |
| 41 | + @Bean |
| 42 | + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 43 | + return http |
| 44 | + // Stateless, token-only API: no cookies => CSRF not applicable |
| 45 | + .csrf(AbstractHttpConfigurer::disable) |
| 46 | + .sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) |
| 47 | + .cors(Customizer.withDefaults()) |
| 48 | + |
| 49 | + .formLogin(AbstractHttpConfigurer::disable) |
| 50 | + .httpBasic(AbstractHttpConfigurer::disable) |
| 51 | + .logout(AbstractHttpConfigurer::disable) |
| 52 | + |
| 53 | + .authorizeHttpRequests(auth -> auth |
| 54 | + .requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() |
| 55 | + .requestMatchers("/api/v1/auth/**").permitAll() |
| 56 | + .requestMatchers(HttpMethod.GET, "/api/v1/keys/**").authenticated() // Allow all roles to GET |
| 57 | + .requestMatchers("/api/v1/keys/**").hasRole("MANAGER") |
| 58 | + .anyRequest().authenticated() |
| 59 | + ) |
| 60 | + |
| 61 | + .userDetailsService(service) |
| 62 | + .addFilterBefore(new JwtAuthenticationFilter( |
| 63 | + jwtSecret, |
| 64 | + service |
| 65 | + ), UsernamePasswordAuthenticationFilter.class) |
| 66 | + .build(); |
| 67 | + } |
| 68 | + |
| 69 | + @Bean |
| 70 | + public PasswordEncoder passwordEncoder() { |
| 71 | + return new BCryptPasswordEncoder(); |
| 72 | + } |
| 73 | + |
| 74 | + @Bean |
| 75 | + public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { |
| 76 | + return configuration.getAuthenticationManager(); |
| 77 | + } |
| 78 | + |
| 79 | + @Bean |
| 80 | + public CorsConfigurationSource corsConfigurationSource() { |
| 81 | + var config = new CorsConfiguration(); |
| 82 | + config.setAllowedOrigins(List.of("http://localhost:4200", "http://localhost")); |
| 83 | + config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "OPTIONS")); |
| 84 | + config.setAllowedHeaders(List.of("Authorization", "Content-Type")); |
| 85 | + config.setAllowCredentials(false); |
| 86 | + |
| 87 | + var source = new UrlBasedCorsConfigurationSource(); |
| 88 | + source.registerCorsConfiguration("/**", config); |
| 89 | + return source; |
| 90 | + } |
| 91 | +} |
0 commit comments