문제
Nginx에서 8080포트로
8081서버와 8082 서버를 연결해주고있는상황
curl localhost:8082/api/health-check
위 명령어를 입력했을때
security config에서 아래와같이 추가했기떄문에
// white list (Spring Security 체크 제외 목록)
MvcRequestMatcher[] permitAllWhiteList = {
mvc.pattern("/api/health-check")
};
아래와같이 인증없이 UP 뜨는 게 정상
curl localhost:8082/api/health-check
{"status":"UP"}
근데 똑같이 nginx 연결포트인 8080으로 했을땐 인증요구하는 문제발생
curl localhost:8082
{"errorCode":"E0002","errorMsg":"인증되지 않은 사용자입니다."}
원인
spring security 에 아래가 추가되어있음
spring security 전에 jwtAuthenticationFilter를 먼저 거치는데
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
직접 8082에 요청을 하게되면 헤더를 명시적으로 작성하지않는이상 헤더없이 요청을 보낸다
curl http://localhost:8082/api/users
그럼 헤더에 Authorization 헤더가 없으므로 필터는 아무것도 하지 않고 통과
그다음 SecurityConfig 에서 검사
이후 /api/health-check -> permitAll() => 통과
다른 허용되지않은 것들은 인증안돼서 401
그런데 8080(nginx) 으로 하게되면
8080 -> nginx -> 8082 로 프록시 될때,
nginx는 기본적으로 클라이언트가 보낸 모든 헤더(특히 Authorization)를 그대로 전달
그러면 8082(Spring Boot 서버)는 Authorization: Bearer xxx 헤더가 존재하는 요청으로 인식
토큰이 있으면 모를까 health-check할때는 토큰이 없기때문에 jwtauthenticationfilter에서 걸림
고로 401이 뜸
해결
nginx conf 수정
아래 내용을 추가해준다
location = /api/health-check {
include /etc/nginx/conf.d/myapp-url.inc;
proxy_pass $myapp_backend/api/health-check;
# ✅ Authorization 헤더 제거
proxy_set_header Authorization "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
sudo nginx -t
sudo systemctl reload nginx
반응형
'웹 개발 > 🍃 SpringBoot' 카테고리의 다른 글
Spring Boot | 패키지 구조(계층형 vs 도메인형) (1) | 2025.05.29 |
---|---|
findAll()에서 Optional<List<T>>를 쓰지 않아도 되는 이유 (1) | 2025.05.20 |
Springboot | 생성자관련 어노테이션 (0) | 2025.03.13 |
SpringBoot + MyBatis + Postgres 완전정복 (0) | 2025.03.11 |
JPA | DAO 설계 (0) | 2025.01.13 |