260626_03_ENH: admin용 swagger 안뜨던 문제 수정
This commit is contained in:
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 버전 변경은 여기서
|
// 버전 변경은 여기서
|
||||||
def commonVer = "beta.1.1.31"
|
def commonVer = "beta.1.1.32"
|
||||||
|
|
||||||
group = 'com.kri.common'
|
group = 'com.kri.common'
|
||||||
version = commonVer
|
version = commonVer
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
@AutoConfiguration
|
@AutoConfiguration(after = ApiKeyJpaAutoConfiguration.class)
|
||||||
@EnableConfigurationProperties(ApiKeyProperties.class)
|
@EnableConfigurationProperties(ApiKeyProperties.class)
|
||||||
@ConditionalOnProperty(prefix = "kri.auth.apikey", name = "enabled", havingValue = "true")
|
@ConditionalOnProperty(prefix = "kri.auth.apikey", name = "enabled", havingValue = "true")
|
||||||
public class ApiKeyPreloadAutoConfiguration {
|
public class ApiKeyPreloadAutoConfiguration {
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||||||
@ConfigurationProperties(prefix = "kri.auth.apikey")
|
@ConfigurationProperties(prefix = "kri.auth.apikey")
|
||||||
public class ApiKeyProperties {
|
public class ApiKeyProperties {
|
||||||
|
|
||||||
|
public static final List<String> DEFAULT_ADMIN_ALLOW_IPS = List.of(
|
||||||
|
"127.0.0.1/32",
|
||||||
|
"localhost",
|
||||||
|
"0:0:0:0:0:0:0:1"
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API Key 인증 허용 여부
|
* API Key 인증 허용 여부
|
||||||
*/
|
*/
|
||||||
@@ -38,10 +44,7 @@ public class ApiKeyProperties {
|
|||||||
/**
|
/**
|
||||||
* 내부 관리 API 접근 허용 IP(CIDR)
|
* 내부 관리 API 접근 허용 IP(CIDR)
|
||||||
*/
|
*/
|
||||||
private List<String> adminAllowIps = List.of(
|
private List<String> adminAllowIps = DEFAULT_ADMIN_ALLOW_IPS;
|
||||||
"127.0.0.1/32",
|
|
||||||
"0:0:0:0:0:0:0:1"
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* apikey 전용 DB 설정(기본값 포함)
|
* apikey 전용 DB 설정(기본값 포함)
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ import com.kri.common.feature.auth.dto.JwtUserClaimsDto;
|
|||||||
import com.kri.common.runtime.auth.apikey.PreloadedApiKeyUserClaimsProvider;
|
import com.kri.common.runtime.auth.apikey.PreloadedApiKeyUserClaimsProvider;
|
||||||
import com.kri.common.runtime.web.IpUtil;
|
import com.kri.common.runtime.web.IpUtil;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.web.util.matcher.IpAddressMatcher;
|
import org.springframework.security.web.util.matcher.IpAddressMatcher;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -21,12 +24,18 @@ public class ApiKeyAdminController {
|
|||||||
|
|
||||||
private final ApiKeyProperties props;
|
private final ApiKeyProperties props;
|
||||||
private final PreloadedApiKeyUserClaimsProvider provider;
|
private final PreloadedApiKeyUserClaimsProvider provider;
|
||||||
private final List<IpAddressMatcher> allow;
|
private final List<String> allow;
|
||||||
|
|
||||||
public ApiKeyAdminController(ApiKeyProperties props, PreloadedApiKeyUserClaimsProvider provider) {
|
public ApiKeyAdminController(ApiKeyProperties props, PreloadedApiKeyUserClaimsProvider provider) {
|
||||||
this.props = props;
|
this.props = props;
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.allow = props.getAdminAllowIps().stream().map(IpAddressMatcher::new).toList();
|
Set<String> allowIps = new LinkedHashSet<>(ApiKeyProperties.DEFAULT_ADMIN_ALLOW_IPS);
|
||||||
|
if (props.getAdminAllowIps() != null) {
|
||||||
|
allowIps.addAll(props.getAdminAllowIps());
|
||||||
|
}
|
||||||
|
this.allow = allowIps.stream()
|
||||||
|
.filter(StringUtils::hasText)
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/reload")
|
@PostMapping("/reload")
|
||||||
@@ -53,12 +62,21 @@ public class ApiKeyAdminController {
|
|||||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
String remote = IpUtil.getClientIp(req);
|
String remote = IpUtil.getClientIp(req);
|
||||||
boolean ok = allow.stream().anyMatch(m -> m.matches(remote));
|
boolean ok = allow.stream().anyMatch(allowed -> matches(remote, allowed));
|
||||||
if (!ok) {
|
if (!ok) {
|
||||||
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "IP not allowed: " + remote);
|
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "IP not allowed: " + remote);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean matches(String remote, String allowed) {
|
||||||
|
if ("localhost".equalsIgnoreCase(allowed)) {
|
||||||
|
return "localhost".equalsIgnoreCase(remote)
|
||||||
|
|| "127.0.0.1".equals(remote)
|
||||||
|
|| "0:0:0:0:0:0:0:1".equals(remote);
|
||||||
|
}
|
||||||
|
return new IpAddressMatcher(allowed).matches(remote);
|
||||||
|
}
|
||||||
|
|
||||||
private String mask(String raw) {
|
private String mask(String raw) {
|
||||||
if (raw == null) {
|
if (raw == null) {
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
Reference in New Issue
Block a user