|
| 1 | +package com.ftm.server.infrastructure.security; |
| 2 | + |
| 3 | +import com.ftm.server.infrastructure.security.handler.PermissionDeniedHandler; |
| 4 | +import com.ftm.server.infrastructure.security.handler.UnauthenticatedAccessHandler; |
| 5 | +import java.util.List; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 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.config.annotation.web.builders.HttpSecurity; |
| 11 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 12 | +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; |
| 13 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 14 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 15 | +import org.springframework.security.web.SecurityFilterChain; |
| 16 | +import org.springframework.web.cors.CorsConfiguration; |
| 17 | +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; |
| 18 | + |
| 19 | +@Configuration |
| 20 | +@EnableWebSecurity(debug = true) |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class SecurityConfig { |
| 23 | + |
| 24 | + private final UnauthenticatedAccessHandler unauthenticatedAccessHandler; |
| 25 | + private final PermissionDeniedHandler permissionDeniedHandler; |
| 26 | + |
| 27 | + // CORS 에서 허용할 HTTP 메서드 목록 |
| 28 | + public static final List<HttpMethod> CORS_ALLOWED_METHODS = |
| 29 | + List.of( |
| 30 | + HttpMethod.GET, |
| 31 | + HttpMethod.POST, |
| 32 | + HttpMethod.PUT, |
| 33 | + HttpMethod.PATCH, |
| 34 | + HttpMethod.DELETE, |
| 35 | + HttpMethod.HEAD); |
| 36 | + |
| 37 | + // CORS 에서 허용할 도메인 목록 |
| 38 | + public static final List<String> CORS_ALLOWED_ORIGINS = |
| 39 | + List.of( |
| 40 | + "http://localhost:8080", // 로컬 환경 서버 도메인 |
| 41 | + "https://dev-api.fittheman.site", // 개발 환경 서버 도메인 |
| 42 | + "https://fittheman.site"); // 개발 환경 클라이언트 도메인 |
| 43 | + |
| 44 | + @Bean |
| 45 | + public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
| 46 | + http |
| 47 | + // csrf 비활성화 |
| 48 | + .csrf(AbstractHttpConfigurer::disable) |
| 49 | + // http basic 비활성화 |
| 50 | + .httpBasic(AbstractHttpConfigurer::disable) |
| 51 | + // 폼로그인 비활성화 |
| 52 | + .formLogin(AbstractHttpConfigurer::disable) |
| 53 | + // 로그아웃 비활성화 |
| 54 | + .logout(AbstractHttpConfigurer::disable) |
| 55 | + // 세션 관리 |
| 56 | + .sessionManagement( |
| 57 | + session -> |
| 58 | + session.sessionFixation() |
| 59 | + .migrateSession() // 세션 고정 보호 |
| 60 | + .maximumSessions(1) // 동시 로그인 1개 제한 |
| 61 | + .maxSessionsPreventsLogin(false)) // 기존 세션 만료 후 새 로그인 허용 |
| 62 | + // 예외 핸들링 |
| 63 | + .exceptionHandling( |
| 64 | + exception -> |
| 65 | + exception |
| 66 | + // 인증되지 않은 요청 예외 처리 |
| 67 | + .authenticationEntryPoint(unauthenticatedAccessHandler) |
| 68 | + // 접근 권한 부족 예외 처리 |
| 69 | + .accessDeniedHandler(permissionDeniedHandler)) |
| 70 | + // cors 설정 |
| 71 | + .cors(cors -> cors.configurationSource(corsConfigurationSource())) |
| 72 | + // 경로 인가 설정 |
| 73 | + .authorizeHttpRequests( |
| 74 | + authorize -> { |
| 75 | + authorize |
| 76 | + // 정적 리소스 경로 허용 |
| 77 | + .requestMatchers("/docs/**") |
| 78 | + .permitAll(); |
| 79 | + |
| 80 | + // TODO: 요청 허용 특정 API 추가 (회원가입, 로그인 등) |
| 81 | + |
| 82 | + // 그 외 모든 요청은 인증 필요 |
| 83 | + authorize.anyRequest().authenticated(); |
| 84 | + }); |
| 85 | + |
| 86 | + return http.build(); |
| 87 | + } |
| 88 | + |
| 89 | + // Password 암호화 설정 |
| 90 | + @Bean |
| 91 | + public PasswordEncoder passwordEncoder() { |
| 92 | + return new BCryptPasswordEncoder(); |
| 93 | + } |
| 94 | + |
| 95 | + // CORS 설정 |
| 96 | + @Bean |
| 97 | + public UrlBasedCorsConfigurationSource corsConfigurationSource() { |
| 98 | + CorsConfiguration config = new CorsConfiguration(); |
| 99 | + config.setAllowCredentials(true); |
| 100 | + config.addAllowedHeader("*"); |
| 101 | + CORS_ALLOWED_ORIGINS.forEach(config::addAllowedOriginPattern); |
| 102 | + CORS_ALLOWED_METHODS.forEach(config::addAllowedMethod); |
| 103 | + |
| 104 | + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| 105 | + source.registerCorsConfiguration("/**", config); |
| 106 | + |
| 107 | + return source; |
| 108 | + } |
| 109 | +} |
0 commit comments