[CorsConfig 구성] cors 처리에 대한 보완 후, 주석처리. nginx 프록시로 해당 처리 중
This commit is contained in:
		@@ -1,24 +1,55 @@
 | 
				
			|||||||
package com.bio.bio_backend.global.config;
 | 
					package com.bio.bio_backend.global.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Value;
 | 
				
			||||||
import org.springframework.context.annotation.Bean;
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
import org.springframework.context.annotation.Configuration;
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
import org.springframework.web.cors.CorsConfiguration;
 | 
					import org.springframework.web.cors.CorsConfiguration;
 | 
				
			||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 | 
					import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
 | 
				
			||||||
import org.springframework.web.filter.CorsFilter;
 | 
					import org.springframework.web.filter.CorsFilter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Configuration
 | 
					@Configuration
 | 
				
			||||||
public class CorsConfig {
 | 
					public class CorsConfig {
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    @Value("${cors.allowed-origins:http://localhost:3000,http://localhost:8080}")
 | 
				
			||||||
 | 
					    private String allowedOrigins;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    @Bean
 | 
					    @Bean
 | 
				
			||||||
    public CorsFilter corsFilter() {
 | 
					    public CorsFilter corsFilter() {
 | 
				
			||||||
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
 | 
					        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
 | 
				
			||||||
        CorsConfiguration config = new CorsConfiguration();
 | 
					        CorsConfiguration config = new CorsConfiguration();
 | 
				
			||||||
        config.addAllowedOriginPattern("*");
 | 
					        
 | 
				
			||||||
        config.addAllowedHeader("*");
 | 
					        // 허용할 Origin 설정
 | 
				
			||||||
        config.addAllowedMethod("*");
 | 
					        List<String> origins = Arrays.asList(allowedOrigins.split(","));
 | 
				
			||||||
 | 
					        config.setAllowedOrigins(origins);
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // 허용할 헤더 설정
 | 
				
			||||||
 | 
					        config.setAllowedHeaders(Arrays.asList(
 | 
				
			||||||
 | 
					            "Authorization",
 | 
				
			||||||
 | 
					            "Content-Type",
 | 
				
			||||||
 | 
					            "X-Requested-With",
 | 
				
			||||||
 | 
					            "Accept",
 | 
				
			||||||
 | 
					            "Origin",
 | 
				
			||||||
 | 
					            "Access-Control-Request-Method",
 | 
				
			||||||
 | 
					            "Access-Control-Request-Headers"
 | 
				
			||||||
 | 
					        ));
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // 허용할 HTTP 메서드 설정
 | 
				
			||||||
 | 
					        config.setAllowedMethods(Arrays.asList(
 | 
				
			||||||
 | 
					            "GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"
 | 
				
			||||||
 | 
					        ));
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // 인증 정보 포함 허용 (JWT 토큰 등)
 | 
				
			||||||
        config.setAllowCredentials(true);
 | 
					        config.setAllowCredentials(true);
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // Preflight 요청 캐시 시간 (초)
 | 
				
			||||||
 | 
					        config.setMaxAge(3600L);
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // 모든 경로에 적용
 | 
				
			||||||
        source.registerCorsConfiguration("/**", config);
 | 
					        source.registerCorsConfiguration("/**", config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return new CorsFilter(source);
 | 
					        return new CorsFilter(source);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,7 @@ package com.bio.bio_backend.global.security;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import com.bio.bio_backend.global.filter.JwtTokenIssuanceFilter;
 | 
					import com.bio.bio_backend.global.filter.JwtTokenIssuanceFilter;
 | 
				
			||||||
import com.bio.bio_backend.global.filter.JwtTokenValidationFilter;
 | 
					import com.bio.bio_backend.global.filter.JwtTokenValidationFilter;
 | 
				
			||||||
 | 
					import com.bio.bio_backend.global.config.CorsConfig;
 | 
				
			||||||
import org.springframework.context.annotation.Bean;
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
import org.springframework.context.annotation.Configuration;
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
import org.springframework.core.annotation.Order;
 | 
					import org.springframework.core.annotation.Order;
 | 
				
			||||||
@@ -15,6 +16,7 @@ import org.springframework.security.config.http.SessionCreationPolicy;
 | 
				
			|||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 | 
					import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 | 
				
			||||||
import org.springframework.security.web.SecurityFilterChain;
 | 
					import org.springframework.security.web.SecurityFilterChain;
 | 
				
			||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 | 
					import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 | 
				
			||||||
 | 
					import org.springframework.web.filter.CorsFilter;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
					import com.fasterxml.jackson.databind.ObjectMapper;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -37,6 +39,12 @@ public class WebSecurity {
 | 
				
			|||||||
    private final ObjectMapper objectMapper;
 | 
					    private final ObjectMapper objectMapper;
 | 
				
			||||||
    private final HttpUtils httpUtils;
 | 
					    private final HttpUtils httpUtils;
 | 
				
			||||||
    private final MemberMapper memberMapper;
 | 
					    private final MemberMapper memberMapper;
 | 
				
			||||||
 | 
					    private final CorsConfig corsConfig; // CorsConfig 주입
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // CORS 필터 빈 가져오기
 | 
				
			||||||
 | 
					    private CorsFilter getCorsFilter() {
 | 
				
			||||||
 | 
					        return corsConfig.corsFilter();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private JwtTokenIssuanceFilter getJwtTokenIssuanceFilter(AuthenticationManager authenticationManager) {
 | 
					    private JwtTokenIssuanceFilter getJwtTokenIssuanceFilter(AuthenticationManager authenticationManager) {
 | 
				
			||||||
        JwtTokenIssuanceFilter filter = new JwtTokenIssuanceFilter(authenticationManager, jwtUtils, objectMapper, memberService, httpUtils, memberMapper);
 | 
					        JwtTokenIssuanceFilter filter = new JwtTokenIssuanceFilter(authenticationManager, jwtUtils, objectMapper, memberService, httpUtils, memberMapper);
 | 
				
			||||||
@@ -86,6 +94,7 @@ public class WebSecurity {
 | 
				
			|||||||
            .sessionManagement(session -> 
 | 
					            .sessionManagement(session -> 
 | 
				
			||||||
                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
 | 
					                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
 | 
				
			||||||
            );
 | 
					            );
 | 
				
			||||||
 | 
					            //.addFilterBefore(getCorsFilter(), UsernamePasswordAuthenticationFilter.class);    // cors를 nginx 통해 처리 중
 | 
				
			||||||
        return http.build();
 | 
					        return http.build();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -117,6 +126,7 @@ public class WebSecurity {
 | 
				
			|||||||
                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
 | 
					                session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
            .logout(AbstractHttpConfigurer::disable)
 | 
					            .logout(AbstractHttpConfigurer::disable)
 | 
				
			||||||
 | 
					            //.addFilterBefore(getCorsFilter(), UsernamePasswordAuthenticationFilter.class)     // cors를 nginx 통해 처리 중
 | 
				
			||||||
            .addFilterBefore(getJwtTokenIssuanceFilter(authenticationManager), UsernamePasswordAuthenticationFilter.class)  // 토큰 발급
 | 
					            .addFilterBefore(getJwtTokenIssuanceFilter(authenticationManager), UsernamePasswordAuthenticationFilter.class)  // 토큰 발급
 | 
				
			||||||
            .addFilterBefore(getJwtTokenValidationFilter(), UsernamePasswordAuthenticationFilter.class);  // 토큰 검증
 | 
					            .addFilterBefore(getJwtTokenValidationFilter(), UsernamePasswordAuthenticationFilter.class);  // 토큰 검증
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user