org.springframework.util.PlaceholderResolutionException: Could not resolve placeholder 'jwt.secret' in value "${jwt.secret}" 오류가 발생
오류의 원인:
JWT(JSON Web Token) 생성 및 검증에 필요한 jwt.secret 프로퍼티 값을 Spring 환경설정에서 찾을 수 없기 때문
application.yml 에 jwt secret 수정jwt: (예시)
secret: synergymSecretKey2024SynergymProjectJwtTokenSecretKey
expiration: 86400000
JwtTokenProvider 클래스 수정:JwtTokenProvider 클래스에서 @Value 어노테이션을 사용application.yml에 정의된 jwt.secret과 jwt.expiration 값을 주입받도록 수정@PostConstruct 어노테이션을 사용하여 secretKey를 Base64로 인코딩하여 초기화하는 로직을 추가import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Base64;
// ... (기타 import)
@Component
public class JwtTokenProvider {
@Value("${jwt.secret}")
private String secretKey;
@Value("${jwt.expiration}")
private long validityInMilliseconds;
@PostConstruct
protected void init() {
secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes());
}
// ... (토큰 생성, 검증 등 나머지 메서드)
}
SecurityConfig 클래스 수정:SecurityConfig 클래스에 JwtAuthenticationFilter를 UsernamePasswordAuthenticationFilter 앞에 추가
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider),
UsernamePasswordAuthenticationFilter.class);
JWT 기반 인증을 활성화
JwtTokenProvider를 SecurityConfig에 주입받아 필터 생성 시 사용
애플리케이션이 JWT 관련 프로퍼티를 로드하지 못해 발생한 오류를 해결하기 위해
application.yml에 해당 설정을 추가하고, JwtTokenProvider, SecurityConfig)에서 이를 사용하도록 수정