fix: Cors 적용

dev
gitea-관리자 1 year ago
parent 4cbd0ea1cc
commit cd883a6bf5

@ -18,6 +18,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.web.filter.CommonsRequestLoggingFilter; import org.springframework.web.filter.CommonsRequestLoggingFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -27,7 +29,6 @@ import kr.xit.core.spring.config.auth.AuthentificationInterceptor;
import kr.xit.core.spring.config.properties.CorsProperties; import kr.xit.core.spring.config.properties.CorsProperties;
import kr.xit.core.spring.filter.LoggingFilter; import kr.xit.core.spring.filter.LoggingFilter;
import kr.xit.core.spring.filter.ReadableRequestWrapperFilter; import kr.xit.core.spring.filter.ReadableRequestWrapperFilter;
import kr.xit.core.spring.filter.SimpleCORSFilter;
import kr.xit.core.spring.resolver.CustomArgumentResolver; import kr.xit.core.spring.resolver.CustomArgumentResolver;
import kr.xit.core.spring.resolver.PageableArgumentResolver; import kr.xit.core.spring.resolver.PageableArgumentResolver;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -39,7 +40,7 @@ import lombok.RequiredArgsConstructor;
* - AuthentificationInterceptor : * - AuthentificationInterceptor :
* - CommonsRequestLoggingFilter : request logging * - CommonsRequestLoggingFilter : request logging
* - ReadableRequestWrapperFilter : post logging * - ReadableRequestWrapperFilter : post logging
* - SimpleCORSFilter : cors * - CORS
* packageName : kr.xit.core.spring.config * packageName : kr.xit.core.spring.config
* fileName : WebMvcConfig * fileName : WebMvcConfig
* author : julim * author : julim
@ -53,11 +54,11 @@ import lombok.RequiredArgsConstructor;
* @see AuthentificationInterceptor * @see AuthentificationInterceptor
* @see CommonsRequestLoggingFilter * @see CommonsRequestLoggingFilter
* @see ReadableRequestWrapperFilter * @see ReadableRequestWrapperFilter
* @see SimpleCORSFilter
* @see LoggingFilter * @see LoggingFilter
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
@Configuration @Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer { public class WebMvcConfig implements WebMvcConfigurer {
/** /**
* logging exclude path * logging exclude path
@ -73,7 +74,6 @@ public class WebMvcConfig implements WebMvcConfigurer {
.addPathPatterns("/**/*") .addPathPatterns("/**/*")
.excludePathPatterns( .excludePathPatterns(
"/api/core/*" "/api/core/*"
// "/api/v1/kakaopay/*"
); );
} }
@ -92,7 +92,6 @@ public class WebMvcConfig implements WebMvcConfigurer {
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new CustomArgumentResolver()); resolvers.add(new CustomArgumentResolver());
resolvers.add(new PageableArgumentResolver()); resolvers.add(new PageableArgumentResolver());
//WebMvcConfigurer.super.addArgumentResolvers(resolvers);
} }
/** /**
@ -161,44 +160,16 @@ public class WebMvcConfig implements WebMvcConfigurer {
return bean; return bean;
} }
/** @Override
* CORS Filter public void addCorsMappings(CorsRegistry registry) {
* @return registry.addMapping("/api/**")
*/ .allowedOrigins(corsProperties.getAllowedOrigins().split(","))
@Bean .allowedMethods(corsProperties.getAllowedMethods().split(","))
public FilterRegistrationBean simpleCorsFilter() { .allowedHeaders(corsProperties.getAllowedHeaders().split(","))
.allowCredentials(corsProperties.getAllowCredentials())
SimpleCORSFilter corsFilter = new SimpleCORSFilter(); .maxAge(corsProperties.getMaxAge())
FilterRegistrationBean bean = new FilterRegistrationBean(corsFilter); .exposedHeaders(corsProperties.getExposeHeader());
bean.setOrder(Ordered.LOWEST_PRECEDENCE); }
bean.addUrlPatterns(Constants.API_URL_PATTERNS);
return bean;
// CorsConfiguration config = new CorsConfiguration();
// config.setAllowedOrigins(Collections.singletonList(corsProperties.getAllowedOrigins()));
// config.setAllowedMethods(Collections.singletonList(corsProperties.getAllowedMethods()));
// config.setAllowedHeaders(Collections.singletonList(corsProperties.getAllowedHeaders()));
// config.setAllowCredentials(corsProperties.getAllowCredentials());
// config.setMaxAge(corsProperties.getMaxAge());
// config.setExposedHeaders(Collections.singletonList(corsProperties.getExposeHeader()));
//
// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// source.registerCorsConfiguration("/**", config);
//
// FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
// bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
// return bean;
}
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// //WebMvcConfigurer.super.addCorsMappings(registry);
// registry.addMapping("/**")
// .allowedOrigins(String.valueOf(Collections.singletonList(corsProperties.getAllowedOrigins())))
// .allowedMethods(String.valueOf(Collections.singletonList(corsProperties.getAllowedMethods())));
// }
// /** // /**

@ -51,7 +51,7 @@ logging:
# Spring Security cors 설정 :: CorsConfiguration 설정 값 # Spring Security cors 설정 :: CorsConfiguration 설정 값
cors: cors:
allowed-origins: http://localhost:8080 allowed-origins: http://localhost:8080,http://localhost:8082
# ================================================================================================================== # ==================================================================================================================
# SQL logging lib setting # SQL logging lib setting

@ -95,7 +95,7 @@ logging:
cors: cors:
# 헤더에 작성된 출처만 브라우저가 리소스를 접근할 수 있도록 허용함. # 헤더에 작성된 출처만 브라우저가 리소스를 접근할 수 있도록 허용함.
# * 이면 모든 곳에 공개 # * 이면 모든 곳에 공개
allowed-origins: http://localhost:8080,http:127.0.0.1:8080 #allowed-origins: http://localhost:8080,http:127.0.0.1:8080
# 리소스 접근을 허용하는 HTTP 메서드를 지정 # 리소스 접근을 허용하는 HTTP 메서드를 지정
allowed-methods: GET,POST,PUT,DELETE allowed-methods: GET,POST,PUT,DELETE
# 요청을 허용하는 해더 # 요청을 허용하는 해더

Loading…
Cancel
Save