Contents
see List작성일 2015. 06. 29.
Spring에서 HTTP Request Header 가져오기
Spring MVC에서 HTTP 요청 헤더 값을 가져오는 다양한 방법입니다.
방법 1: @RequestHeader 어노테이션
@RequestMapping("/example")
public String example(@RequestHeader(value="User-Agent") String userAgent) {
System.out.println("User-Agent: " + userAgent);
return "example";
}
// 필수가 아닌 경우
@RequestMapping("/example")
public String example(
@RequestHeader(value="X-Custom-Header", required=false) String customHeader,
@RequestHeader(value="Accept-Language", defaultValue="ko") String lang) {
return "example";
}방법 2: HttpServletRequest 사용
@RequestMapping("/example")
public String example(HttpServletRequest request) {
String userAgent = request.getHeader("User-Agent");
String contentType = request.getHeader("Content-Type");
String authorization = request.getHeader("Authorization");
return "example";
}방법 3: 모든 헤더 가져오기
@RequestMapping("/example")
public String example(@RequestHeader HttpHeaders headers) {
// 특정 헤더
String userAgent = headers.getFirst("User-Agent");
// 모든 헤더 출력
headers.forEach((key, value) -> {
System.out.println(key + ": " + value);
});
return "example";
}
// 또는 Map으로 받기
@RequestMapping("/example")
public String example(@RequestHeader Map headers) {
headers.forEach((key, value) -> {
System.out.println(key + ": " + value);
});
return "example";
} 방법 4: RequestContextHolder 사용
// Service 레이어에서 사용 가능
public void someMethod() {
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();
String userAgent = request.getHeader("User-Agent");
}자주 사용하는 헤더
| User-Agent | 클라이언트 브라우저/앱 정보 |
| Authorization | 인증 토큰 (Bearer, Basic 등) |
| Content-Type | 요청 본문 타입 |
| Accept | 클라이언트가 원하는 응답 타입 |
| Accept-Language | 선호 언어 |
| X-Forwarded-For | 프록시 경유 시 원본 IP |
| Referer | 이전 페이지 URL |
클라이언트 IP 가져오기
public String getClientIP(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.isEmpty()) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.isEmpty()) {
ip = request.getRemoteAddr();
}
return ip;
}spring
| No | 작성일 | Title |
|---|---|---|
| 2881 | 2026. 05. 25. | Spring API 오류 응답 표준화: ProblemDetail과 @RestControllerAdvice 실전 가이드 |
| 2813 | 2026. 05. 17. | Spring Boot 무중단 배포를 위한 Readiness·Liveness·Graceful Shutdown 실전 가이드 |
| 2611 | 2026. 04. 25. | Spring Security 7 Passkey(패스키) 완전 가이드: WebAuthn 기반 비밀번호 없는 인증 구현 |
| 2526 | 2026. 04. 17. | Spring Boot 3.4/3.5 + Spring Framework 7 핵심 신기능 실전 가이드: 구조적 로깅·Virtual Threads·API 버전닝 |
| 2492 | 2026. 04. 14. | Spring Boot 3.4 완벽 가이드: RestClient, OAuth2 개선, Virtual Threads 통합 |
| 2471 | 2026. 04. 13. | Spring Boot 3.5 핵심 기능 완벽 가이드: Structured Logging, Virtual Threads, SSL 설정 |
| 2451 | 2026. 04. 12. | Spring Boot 3.4 + Spring AI 실전 가이드: Virtual Threads와 MCP 통합 |
| 2427 | 2026. 04. 11. | Spring Boot 3.4 SSL 인증서 모니터링과 Graceful Shutdown 설정 가이드 |
| 2391 | 2026. 04. 09. | Spring Boot 3.5 실전 가이드 - 구조화 로깅, SSL 자동 설정, GraalVM 최적화 |
| 2370 | 2026. 04. 08. | Spring Boot 3.4/3.5 실전 가이드: Virtual Threads, GraalVM Native, 구조화 로깅 완전 정복 |