LoginSignup
22
20

More than 5 years have passed since last update.

[小ネタ]Basic 認証の際に必要な Authorization ヘッダーを作成する

Posted at

小ネタです。

結論

Mac で試しましたが、以下のような形式で出力可能。
username/password は実際に値に置き換え下さい。

$echo "Basic $(echo -n 'username:passoword' | base64)"

あとはこれを Authorization ヘッダーに追加すればOK.

curl https://xxxxx -H 'Authorization: Basic MjhkdH

Postman を使うと GUI で直接ユーザー名・パスワード入力で Authorization ヘッダーの追加などもしてくれるのでこれを使うのもあり。

そもそも Basic 認証の手順とは

Basic 認証を通すためにはクライアントでは以下のようにする必要があります。

Basic access authentication

The username and password are combined with a single colon. (:)
The resulting string is encoded into an octet sequence.[7]
The resulting string is encoded using a variant of Base64.[8]
The authorization method and a space (e.g. "Basic ") is then prepended to the encoded string, separated with a space.

最終的にAuthorization: Basic QWxhZGRpbjpPcGVuU2VzYW1lという文字列を作ります。

22
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
20