최근에 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를 사용 중일 때 발생하는 기본 동작입니다.
이 문제를 해결하려면 특정 엔드포인트(/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 요청을 테스트용으로만 허용할 때 사용하는 설정입니다.Spring Boot에 보안 의존성이 포함되면, Postman이나 외부 시스템에서 API를 호출할 때 기본 로그인 페이지 응답이 나올 수 있습니다.
이 문제는 SecurityFilterChain 설정으로 간단하게 해결할 수 있으니, 개발 초기에는 필수로 설정해두는 걸 추천합니다.