본문 바로가기

TIL/트러블슈팅

[SpringBoot] Your security configuration must be updated before running your application in production. 403

👉 문제 상황

MockHttpServletResponse:
           Status = 403

sing generated security password: 7191ef02-9d66-440a-825a-faaeafa09245

This generated password is for development use only. Your security configuration must be updated before running your application in production.

👉 원인

org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

Spring Security에서 인증(Authentication)이 필요하지만 MockMvc 요청에 로그인된 사용자 정보가 없어서 발생


👉 해결책1

SecurityAutoConfiguration을 자동 설정에서 제외하여 Spring Security 설정을 비활성화

  • 영향:
    • Spring Security가 아예 설정되지 않음
    • 인증 및 인가 관련 필터가 로드되지 않음
@ExcludeAutoConfiguration(SecurityAutoConfiguration.class) // Spring Security 자동 설정 제외

 

✅ 언제 사용해야 할까?

1. Spring Security가 포함된 프로젝트에서, 보안 설정 없이 Controller 테스트를 진행하고 싶을 때

2. 기본적인 Security 설정도 필요하지 않은 경우


👉 해결책2

MockMvc의 자동 구성을 설정하면서 Security Filter를 비활성화

  • 영향:
    • Spring Security는 유지되지만 인증/인가 필터가 비활성화됨
    • @WithMockUser 없이도 보안이 적용된 API를 테스트할 수 있음
    • 보안 필터만 제외하고, 다른 Spring Security 기능은 그대로 유지됨
@AutoConfigureMockMvc(addFilters = false) // MockMvc에서 Security 필터를 비활성화

 

✅ 언제 사용해야 할까?

1. Spring Security가 활성화된 프로젝트에서 Security 필터를 무시하고 컨트롤러 단위 테스트를 수행하고 싶을 때

2. 기본적인 Security 설정은 유지하면서, MockMvc로 인증 없이 API를 호출하고 싶을 때

3. @WithMockUser 없이 테스트를 실행하고 싶을 때

 

🔥 차이점 정리

excludeAutoConfiguration = SecurityAutoConfiguration.class@AutoConfigureMockMvc(addFilters = false)

Security 기능 Spring Security 자체를 제거 Security 유지, 하지만 필터 비활성화
필터 적용 여부 인증/인가 필터 없음 Security 필터만 비활성화
인증 없이 API 테스트 가능? 가능 가능
보안 관련 빈(PasswordEncoder, UserDetailsService) 사용 가능? ❌ (Security 자체가 사라짐) ✅ (Security 빈은 유지됨)

 

 

따라서 @AutoConfigureMockMvc(addFilters = false)를 먼저 적용해 보고, 그래도 해결되지 않는다면, excludeAutoConfiguration을 적용해보길 권합니다.


👉 해결책3

테스트에서 사용자의 인증을 모킹하여 특정 역할과 권한을 가진 사용자로 시뮬레이션으로 실제 Spring Security 환경을 반영

dependencies {
    // 다른 의존성들...
    testImplementation 'org.springframework.security:spring-security-test' // 이 줄을 추가하세요
}
@Test
@WithMockUser(username = "testUser", roles = {"USER"})

 

✅ 언제 사용해야 할까?

1. 특정 역할이 필요한 엔드포인트를 테스트