251117_02_ENH : Security, Swagger, MVC 기본 설정 추가
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.kri.common.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.kri.common.webmvc;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.web.util.matcher.IpAddressMatcher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@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));
|
||||
}
|
||||
}
|
||||
27
src/main/java/com/kri/common/webmvc/IpUtil.java
Normal file
27
src/main/java/com/kri/common/webmvc/IpUtil.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.kri.common.webmvc;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
public final class IpUtil {
|
||||
|
||||
private IpUtil() {
|
||||
}
|
||||
|
||||
public static String getClientIp(HttpServletRequest request) {
|
||||
// 1. Proxy 환경 고려: X-Forwarded-For 헤더 우선
|
||||
String xff = request.getHeader("X-Forwarded-For");
|
||||
if (xff != null && !xff.isBlank()) {
|
||||
// X-Forwarded-For: client, proxy1, proxy2 ...
|
||||
return xff.split(",")[0].trim();
|
||||
}
|
||||
|
||||
// 2. 그 다음 X-Real-IP
|
||||
String realIp = request.getHeader("X-Real-IP");
|
||||
if (realIp != null && !realIp.isBlank()) {
|
||||
return realIp;
|
||||
}
|
||||
|
||||
// 3. 최후 fallback
|
||||
return request.getRemoteAddr();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.kri.common.webmvc;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@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 로 바인딩되는 리스트
|
||||
*
|
||||
* 예) 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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.kri.common.webmvc;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
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 = "common.webmvc.enabled", matchIfMissing = true)
|
||||
@RequiredArgsConstructor
|
||||
public class KriWebMvcAutoConfiguration {
|
||||
|
||||
private final KriIpAccessProperties ipProps; // 생성자 주입 (RequiredArgsConstructor)
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IpAccessControlService ipAccessControlService(KriIpAccessProperties ipProps) {
|
||||
return new IpAccessControlService(ipProps);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IpAccessControlInterceptor ipAccessControlInterceptor(IpAccessControlService service) {
|
||||
return new IpAccessControlInterceptor(service);
|
||||
}
|
||||
|
||||
// 선택 주입: 있으면 쓰고, 없으면 안 씀
|
||||
private final ObjectProvider<IpAccessControlInterceptor> ipAccessControlInterceptorProvider;
|
||||
|
||||
/**
|
||||
* 실제로 WebMvc 설정을 하는 WebMvcConfigurer Bean
|
||||
*/
|
||||
@Bean
|
||||
public WebMvcConfigurer kriCommonWebMvcConfigurer() {
|
||||
return new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
// 기본 CORS – 필요하면 나중에 조정
|
||||
registry.addMapping("/**")
|
||||
.allowedOriginPatterns("*")
|
||||
.allowedMethods("*")
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
ipAccessControlInterceptorProvider.ifAvailable(interceptor ->
|
||||
registry.addInterceptor(interceptor)
|
||||
.order(1)
|
||||
.addPathPatterns("/swagger-ui/**", "/v3/api-docs/**")
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user