260804_01_ENH: api key에 특정 경로 허용 제외 추가

This commit is contained in:
2026-08-04 16:56:42 +09:00
parent 2954565fc7
commit 1ac8dab45a
6 changed files with 112 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import com.kri.common.runtime.auth.apikey.PreloadedApiKeyUserClaimsProvider;
import com.kri.common.runtime.auth.apikey.persistence.repository.ApiKeyRepository;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -12,6 +13,7 @@ import org.springframework.context.annotation.Bean;
@AutoConfiguration(after = ApiKeyJpaAutoConfiguration.class)
@EnableConfigurationProperties(ApiKeyProperties.class)
@ConditionalOnClass(name = "org.springframework.data.jpa.repository.JpaRepository")
@ConditionalOnProperty(prefix = "kri.auth.apikey", name = "enabled", havingValue = "true")
public class ApiKeyPreloadAutoConfiguration {

View File

@@ -26,6 +26,16 @@ public class ApiKeyProperties {
*/
private String headerName = "X-API-Key";
/**
* API Key 인증을 시도할 URL 패턴. 미설정, null 또는 빈 목록이면 모든 요청에서 인증을 시도한다.
*/
private List<String> allowedPaths = List.of();
/**
* API Key 인증에서 제외할 URL 패턴. 허용 패턴보다 우선한다.
*/
private List<String> excludedPaths = List.of();
/**
* 이 서비스의 코드 (서비스별 preload 범위 결정)
*/

View File

@@ -8,18 +8,62 @@ import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
@RequiredArgsConstructor
public class ApiKeyAuthFilter extends OncePerRequestFilter {
private final ApiKeyProperties apiKeyProperties;
private final ApiKeyUserClaimsProvider apiKeyUserClaimsProvider;
private final JwtService jwtService;
private final List<RequestMatcher> allowedPathMatchers;
private final List<RequestMatcher> excludedPathMatchers;
public ApiKeyAuthFilter(
ApiKeyProperties apiKeyProperties,
ApiKeyUserClaimsProvider apiKeyUserClaimsProvider,
JwtService jwtService
) {
this.apiKeyProperties = apiKeyProperties;
this.apiKeyUserClaimsProvider = apiKeyUserClaimsProvider;
this.jwtService = jwtService;
this.allowedPathMatchers = createPathMatchers(apiKeyProperties.getAllowedPaths());
this.excludedPathMatchers = createPathMatchers(apiKeyProperties.getExcludedPaths());
}
private static List<RequestMatcher> createPathMatchers(List<String> paths) {
if (paths == null || paths.isEmpty()) {
return List.of();
}
PathPatternRequestMatcher.Builder matcherBuilder = PathPatternRequestMatcher.withDefaults();
return paths.stream()
.filter(StringUtils::hasText)
.map(String::trim)
.map(matcherBuilder::matcher)
.map(RequestMatcher.class::cast)
.toList();
}
@Override
protected boolean shouldNotFilter(HttpServletRequest request) {
if (matchesAny(excludedPathMatchers, request)) {
return true;
}
return !allowedPathMatchers.isEmpty() && !matchesAny(allowedPathMatchers, request);
}
private static boolean matchesAny(
List<RequestMatcher> matchers,
HttpServletRequest request
) {
return matchers.stream().anyMatch(matcher -> matcher.matches(request));
}
@Override
protected void doFilterInternal(