이번 강의에서는 인증 정보를 URL 파라미터가 아닌 HTTP Header로 옮기는 작업을 진행했다.
실무에서 왜 인증 정보를 Header에 담는지, 그리고 스프링에서는 이를 어떻게 받는지까지 정리해보자.
이전까지는 글 작성, 댓글 조회 등 API 요청 시 아래처럼 인증 정보를 URL 파라미터로 전달했다.
POST /api/v1/posts?apiKey=xxxx-xxxx
이 방식도 동작은 하지만 몇 가지 문제가 있다.
그래서 인증 정보는 Header로 옮기는 것이 일반적이다.
HTTP Header는 키-값(key-value) 형태의 데이터 구조이다.
사실상 해시맵(HashMap) 과 거의 동일한 개념이다.
예시:
Authorization: Bearer token123
Content-Type: application/json
관례적으로 인증 정보는 Authorization 헤더에 담는다.

Authorization: Bearer {apiKey}
👉 즉, 토큰 기반 인증의 핵심 개념이다.
AuthorizationBearer {{apiKey}}이제 URL 뒤에 ?apiKey=... 를 붙일 필요가 없다.
스프링에서는 HTTP 헤더 값을 아래 어노테이션으로 받을 수 있다.
@RequestHeader("Authorization") String authorization
@PostMapping
public RsData<PostDto> write(
@Valid @RequestBody PostWriteReqBody reqBody,
@RequestHeader("Authorization") String authorization
) {
String apiKey = authorization.replace("Bearer ", "");
Member actor = memberService.findByApiKey(apiKey)
.orElseThrow(() -> new ServiceException("401-1", "존재하지 않는 apiKey 입니다."));
Post post = postService.write(actor, reqBody.title, reqBody.content);
return new RsData<>("201-1", "글이 작성되었습니다.", new PostDto(post));
}
"Bearer xxxx" 형태"Bearer " 제거기존 테스트 코드:
post("/api/v1/posts?apiKey=" + actorApiKey)
변경 후:
post("/api/v1/posts")
.header("Authorization", "Bearer " + actorApiKey)
👉 실제 API 사용 방식과 동일해졌다.
@RequestHeader로 간단하게 값 수신 가능