251119_01_ENH : Junie로 소스코드 및 README 정리

This commit is contained in:
2025-11-19 08:43:05 +09:00
parent e5501e1542
commit 2a35b62a48
4 changed files with 568 additions and 125 deletions

View File

@@ -27,10 +27,13 @@ public class JwtService {
@PostConstruct
public void init() {
// 시크릿 검증: HS256 권장 최소 길이(32바이트 이상)
String secret = properties.getSecret();
if (secret == null || secret.getBytes(StandardCharsets.UTF_8).length < 32) {
throw new IllegalStateException("kri.auth.jwt.secret must be at least 32 bytes (UTF-8) for HS256");
}
// secret 문자열을 HMAC 키로 전환
this.key = Keys.hmacShaKeyFor(
properties.getSecret().getBytes(StandardCharsets.UTF_8)
);
this.key = Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
}
// ======================================================
@@ -65,21 +68,6 @@ public class JwtService {
// 토큰 생성 (DTO 기반)
// ======================================================
/**
* JwtUserClaimsDto 기반 Access Token 생성
* - subject = userId
* - claims:
* userId, userTeam, userName, role
*/
public String generateSystemToken(JwtUserClaimsDto userClaims) {
Map<String, Object> claims = buildClaims(userClaims);
return buildToken(
claims,
userClaims.getUserId(),
properties.getAccessTokenValiditySeconds()
);
}
/**
* JwtUserClaimsDto 기반 Access Token 생성
* - subject = userId

View File

@@ -1,69 +0,0 @@
package com.kri.common.security;
import com.kri.common.auth.apikey.ApiKeyProperties;
import com.kri.common.auth.apikey.ApiKeyUserClaimsProvider;
import com.kri.common.auth.jwt.JwtService;
import com.kri.common.auth.jwt.JwtUserClaimsDto;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;
import java.io.IOException;
import java.util.Optional;
@RequiredArgsConstructor
public class ApiKeyAuthFilter extends OncePerRequestFilter {
private final ApiKeyProperties properties;
private final ApiKeyUserClaimsProvider userClaimsProvider;
private final JwtService jwtService; // DTO -> Authentication 변환 용도
@Override
protected void doFilterInternal(
HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain
) throws ServletException, IOException {
// 기능이 꺼져 있으면 바로 패스
if (!properties.isEnabled()) {
filterChain.doFilter(request, response);
return;
}
// 이미 다른 필터에서 Authentication 을 세팅했다면 건너뜀
if (SecurityContextHolder.getContext().getAuthentication() != null) {
filterChain.doFilter(request, response);
return;
}
String headerName = properties.getHeaderName();
String apiKey = request.getHeader(headerName);
if (apiKey == null || apiKey.isBlank()) {
filterChain.doFilter(request, response);
return;
}
// 서비스 쪽 구현체에서 apiKey -> JwtUserClaimsDto 매핑
Optional<JwtUserClaimsDto> userOpt = userClaimsProvider.findUserByApiKey(apiKey);
if (userOpt.isPresent()) {
JwtUserClaimsDto dto = userOpt.get();
// 공통 JwtService 가 제공하는 DTO -> Authentication 변환 사용
Authentication auth = jwtService.toAuthentication(dto);
SecurityContextHolder.getContext().setAuthentication(auth);
} else {
// 유효하지 않은 API-Key → 그냥 Authentication 세팅 안 함
// 이후 anyRequest().authenticated() 에서 401/403 처리
}
filterChain.doFilter(request, response);
}
}