260701_01_ENH: IP 화이트 리스트 기능 추가 없으면 모든 IP허용

This commit is contained in:
2026-07-01 14:03:33 +09:00
parent 11eaca9197
commit 83b098dc91
6 changed files with 755 additions and 180 deletions

View File

@@ -2,6 +2,7 @@ package com.kri.common.autoconfigure.security;
import com.kri.common.runtime.auth.apikey.ApiKeyAuthFilter;
import com.kri.common.feature.auth.jwt.JwtService;
import com.kri.common.runtime.security.IpAllowlistAuthorizationManager;
import com.kri.common.runtime.security.JwtAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectProvider;
@@ -11,12 +12,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authorization.AuthenticatedAuthorizationManager;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@AutoConfiguration(before = SecurityAutoConfiguration.class)
@@ -49,6 +53,9 @@ public class KriSecurityAutoConfiguration {
JwtAuthenticationFilter jwtAuthenticationFilter
) throws Exception {
IpAllowlistAuthorizationManager ipAllowlist =
new IpAllowlistAuthorizationManager(securityProperties.getAllowedIps());
http
.httpBasic(AbstractHttpConfigurer::disable)
.csrf(AbstractHttpConfigurer::disable)
@@ -60,11 +67,27 @@ public class KriSecurityAutoConfiguration {
// ✅ yaml에서 정의한 public-paths 전부 permitAll
if (securityProperties.getPublicPaths() != null && !securityProperties.getPublicPaths()
.isEmpty()) {
auth.requestMatchers(securityProperties.getPublicPaths().toArray(String[]::new))
.permitAll();
if (ipAllowlist.isConfigured()) {
auth.requestMatchers(securityProperties.getPublicPaths().toArray(String[]::new))
.access(ipAllowlist);
} else {
auth.requestMatchers(securityProperties.getPublicPaths().toArray(String[]::new))
.permitAll();
}
}
// ✅ 나머지는 전부 인증 필요
auth.anyRequest().authenticated();
if (ipAllowlist.isConfigured()) {
auth.anyRequest().access((authentication, context) -> {
AuthorizationDecision ipDecision = ipAllowlist.check(authentication, context);
if (ipDecision == null || !ipDecision.isGranted()) {
return new AuthorizationDecision(false);
}
return AuthenticatedAuthorizationManager.<RequestAuthorizationContext>authenticated()
.check(authentication, context);
});
} else {
auth.anyRequest().authenticated();
}
});
// ✅ API Key 필터가 존재하면 먼저 태움

View File

@@ -23,4 +23,9 @@ public class KriSecurityProperties {
"/swagger-ui.html",
"/swagger-ui/**"
);
}
/**
* IP allowlist applied by the default SecurityFilterChain. Empty means no IP restriction.
*/
private List<String> allowedIps = List.of();
}

View File

@@ -0,0 +1,61 @@
package com.kri.common.runtime.security;
import com.kri.common.runtime.web.IpUtil;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.function.Supplier;
import org.springframework.security.authorization.AuthorizationDecision;
import org.springframework.security.authorization.AuthorizationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import org.springframework.security.web.util.matcher.IpAddressMatcher;
import org.springframework.util.StringUtils;
public class IpAllowlistAuthorizationManager implements AuthorizationManager<RequestAuthorizationContext> {
private final List<AllowedIp> allowedIps;
public IpAllowlistAuthorizationManager(List<String> allowedIps) {
this.allowedIps = allowedIps == null ? List.of() : allowedIps.stream()
.filter(StringUtils::hasText)
.map(String::trim)
.map(AllowedIp::new)
.toList();
}
public boolean isConfigured() {
return !allowedIps.isEmpty();
}
@Override
public AuthorizationDecision check(
Supplier<Authentication> authentication,
RequestAuthorizationContext context
) {
HttpServletRequest request = context.getRequest();
String clientIp = IpUtil.getClientIp(request);
boolean granted = allowedIps.stream().anyMatch(allowedIp -> allowedIp.matches(clientIp));
return new AuthorizationDecision(granted);
}
private static class AllowedIp {
private final boolean localhost;
private final IpAddressMatcher matcher;
private AllowedIp(String value) {
this.localhost = "localhost".equalsIgnoreCase(value);
this.matcher = localhost ? null : new IpAddressMatcher(value);
}
private boolean matches(String clientIp) {
if (localhost) {
return "localhost".equalsIgnoreCase(clientIp)
|| "127.0.0.1".equals(clientIp)
|| "0:0:0:0:0:0:0:1".equals(clientIp)
|| "::1".equals(clientIp);
}
return matcher.matches(clientIp);
}
}
}