개발환경

intellij http 전송 기능으로 csrf 토큰을 포함하여 로그인 요청 테스트 하기

codinglog 2025. 1. 10. 10:23

IntelliJ에는 http 요청을 테스트 할 수 있는 http 플러그인을 제공한다.

단일 요청을 할 때는 간단하게 할 수 있지만 선행된 요청의 값을 참조하여 다음 요청을 해야할 경우,

예를 들어 csrf 토큰 값을 포함하여 요청해야 하는 경우는 번거로울 수 있다.

하지만 Http 플러그인은 스크립트를 지원해야 간단하게 해결 할 수 있다.

### 1. 로그인 페이지 요청 및 CSRF 토큰 추출
GET http://localhost:9000/login

> {%
    const csrfRegex = /<input name="_csrf" type="hidden" value="([^"]+)"/;
    const match = response.body.match(csrfRegex);
    let csrfToken;
    if (match) {
        csrfToken = match[1];
        client.global.set("csrf_token", csrfToken)
    } else {
        throw new Error("CSRF 토큰을 찾을 수 없습니다.");
    }
%}

### 로그인 시도
# @no-redirect
POST http://localhost:9000/login
accept: text/html
Content-Type: application/x-www-form-urlencoded

username = myuserid &
password = mypassword &
_csrf = {{csrf_token}}
반응형