260421_01_ENH: Swagger를 대상으로 한 IP 필터링에 대한 중복처리 제거
This commit is contained in:
@@ -4,7 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
//버전 변경은 여기서
|
||||
def commonVer = "beta.1.1.15"
|
||||
def commonVer = "beta.1.1.16"
|
||||
|
||||
group = 'com.kri.common'
|
||||
version = commonVer
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.kri.common.autoconfigure.webmvc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigurationProperties(prefix = "kri.webmvc.ip")
|
||||
public class KriIpAccessProperties {
|
||||
|
||||
/**
|
||||
* 공통에서 고정으로 들고 갈 기본 CIDR 들 → setter 없음 (외부에서 못 바꿈)
|
||||
*/
|
||||
private final List<String> defaultCidrs = List.of(
|
||||
"127.0.0.1/32", // 로컬 IPv4
|
||||
"::1/128", // 로컬 IPv6
|
||||
"10.10.13.0/24", //석오 내부망
|
||||
"211.254.145.128/25" //석오 내부망 외부 IP
|
||||
);
|
||||
|
||||
/**
|
||||
* 서비스에서 추가로 넣고 싶은 CIDR 목록 application.yml / properties 로 바인딩되는 리스트
|
||||
* <p>
|
||||
* 예) kri.webmvc.ip.additional-cidrs[0]=10.10.0.0/16
|
||||
*/
|
||||
private List<String> additionalCidrs = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 기본 + 추가를 합친 최종 목록
|
||||
*/
|
||||
public List<String> getEffectiveCidrs() {
|
||||
return Stream.concat(defaultCidrs.stream(), additionalCidrs.stream())
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.kri.common.autoconfigure.webmvc;
|
||||
|
||||
import com.kri.common.feature.auth.jwt.JwtService;
|
||||
import com.kri.common.runtime.webmvc.IpAccessControlInterceptor;
|
||||
import com.kri.common.runtime.webmvc.IpAccessControlService;
|
||||
import com.kri.common.runtime.webmvc.RequestUserContextArgumentResolver;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
@@ -10,36 +8,18 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(KriIpAccessProperties.class)
|
||||
@ConditionalOnProperty(name = "kri.webmvc.enabled", matchIfMissing = true)
|
||||
@RequiredArgsConstructor
|
||||
public class KriWebMvcAutoConfiguration {
|
||||
|
||||
private final KriIpAccessProperties ipProps; // 생성자 주입 (RequiredArgsConstructor)
|
||||
// 선택 주입: 있으면 쓰고, 없으면 안 씀
|
||||
private final ObjectProvider<IpAccessControlInterceptor> ipAccessControlInterceptorProvider;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IpAccessControlService ipAccessControlService(KriIpAccessProperties ipProps) {
|
||||
return new IpAccessControlService(ipProps);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IpAccessControlInterceptor ipAccessControlInterceptor(IpAccessControlService service) {
|
||||
return new IpAccessControlInterceptor(service);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(JwtService.class)// JwtService 있을 때만 동작
|
||||
@ConditionalOnBean(JwtService.class)
|
||||
@ConditionalOnMissingBean
|
||||
public RequestUserContextArgumentResolver requestUserContextArgumentResolver(
|
||||
JwtService jwtService
|
||||
@@ -47,19 +27,13 @@ public class KriWebMvcAutoConfiguration {
|
||||
return new RequestUserContextArgumentResolver(jwtService);
|
||||
}
|
||||
|
||||
/**
|
||||
* 실제로 WebMvc 설정을 하는 WebMvcConfigurer Bean
|
||||
*/
|
||||
@Bean
|
||||
public WebMvcConfigurer kriCommonWebMvcConfigurer(
|
||||
KriIpAccessProperties properties,
|
||||
ObjectProvider<IpAccessControlInterceptor> ipAccessControlInterceptorProvider,
|
||||
ObjectProvider<RequestUserContextArgumentResolver> requestUserContextArgumentResolverProvider
|
||||
) {
|
||||
return new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
// 기본 CORS – 필요하면 나중에 조정
|
||||
registry.addMapping("/**")
|
||||
.allowedOriginPatterns("*")
|
||||
.allowedMethods("*")
|
||||
@@ -69,11 +43,6 @@ public class KriWebMvcAutoConfiguration {
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
ipAccessControlInterceptorProvider.ifAvailable(interceptor ->
|
||||
registry.addInterceptor(interceptor)
|
||||
.order(1)
|
||||
.addPathPatterns("/swagger-ui/**", "/v3/api-docs/**")
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.kri.common.runtime.webmvc;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class IpAccessControlInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final IpAccessControlService ipAccessControlService;
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Object handler) throws Exception {
|
||||
|
||||
String clientIpAddress = IpUtil.getClientIp(request);
|
||||
if (!ipAccessControlService.isAccessible(clientIpAddress)) {
|
||||
response.sendError(HttpServletResponse.SC_FORBIDDEN);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.kri.common.runtime.webmvc;
|
||||
|
||||
import com.kri.common.autoconfigure.webmvc.KriIpAccessProperties;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.web.util.matcher.IpAddressMatcher;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class IpAccessControlService {
|
||||
|
||||
private final KriIpAccessProperties properties;
|
||||
|
||||
public boolean isAccessible(String ipAddress) {
|
||||
List<String> cidrs = properties.getEffectiveCidrs();
|
||||
|
||||
return cidrs.stream()
|
||||
.map(IpAddressMatcher::new)
|
||||
.anyMatch(matcher -> matcher.matches(ipAddress));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user