PlaceholderResolutionException

채정윤·2025년 5월 13일

Error

목록 보기
4/9

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.secretjwt.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 클래스에 JwtAuthenticationFilterUsernamePasswordAuthenticationFilter 앞에 추가

      .addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider),
                              UsernamePasswordAuthenticationFilter.class);
    • JWT 기반 인증을 활성화

    • JwtTokenProviderSecurityConfig에 주입받아 필터 생성 시 사용

정리

애플리케이션이 JWT 관련 프로퍼티를 로드하지 못해 발생한 오류를 해결하기 위해

  1. application.yml에 해당 설정을 추가하고,
  2. 관련 클래스(JwtTokenProvider, SecurityConfig)에서 이를 사용하도록 수정

0개의 댓글