Spring Boot 테스트 시 로그인 페이지가 나올 때 (Postman 인증 문제 해결법)

molamola·2025년 4월 30일

Spring Boot

목록 보기
1/1

최근에 Spring Boot로 Webhook API를 만들고, Postman을 이용해 아래와 같이 테스트를 진행했습니다.

POST http://localhost:8001/webhook/save?param=hello

그런데 응답 결과가 의외였습니다:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Please sign in</title>
    ...
  </head>
  <body>
    <h2>Login with OAuth 2.0</h2>
  </body>
</html>

Spring Boot 프로젝트에서 REST API를 만들었는데, 갑자기 로그인 페이지?

📌 원인

이건 Spring Boot에서 spring-boot-starter-security 또는 spring-boot-starter-oauth2-client를 사용 중일 때 발생하는 기본 동작입니다.

  • Spring Security는 기본적으로 모든 요청에 인증을 요구합니다.
  • Postman이나 외부 요청이 인증되지 않은 상태로 들어오면 → 자동으로 로그인 페이지를 반환합니다.

🛠 해결 방법: Security 설정 커스터마이징

이 문제를 해결하려면 특정 엔드포인트(/webhook/**)는 인증 없이 접근할 수 있도록 SecurityFilterChain을 설정해야 합니다.

✅ 예제 코드: 모든 요청 허용

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .csrf().disable() // 테스트용으로만 사용! 운영 시 주의
            .authorizeHttpRequests()
                .anyRequest().permitAll(); // 모든 요청을 인증 없이 허용
        return http.build();
    }
}

✅ 일부 경로만 허용 (예: /webhook/**)

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeHttpRequests()
                .requestMatchers("/webhook/**").permitAll()
                .anyRequest().authenticated();
        return http.build();
    }
}

⚠ 주의사항

  • csrf().disable()POST 요청을 테스트용으로만 허용할 때 사용하는 설정입니다.
  • 운영환경에서는 보안을 강화하기 위해 CSRF 보호나 세션 관리 정책도 함께 설정해줘야 합니다.

✅ 결론

Spring Boot에 보안 의존성이 포함되면, Postman이나 외부 시스템에서 API를 호출할 때 기본 로그인 페이지 응답이 나올 수 있습니다.
이 문제는 SecurityFilterChain 설정으로 간단하게 해결할 수 있으니, 개발 초기에는 필수로 설정해두는 걸 추천합니다.

0개의 댓글