カテゴリー
【Angular】ローカル環境下のSPA(シングルページアプリケーション)を常時SSL(https)化に対応させる方法
※ 当ページには【広告/PR】を含む場合があります。
2020/03/29
2022/08/10
自社のWebサービスをクラウド化していくと、サードパーティのAPIエンドポイントを踏むときなどに、常時SSL化を要求されることもあります。
angularでSPAをローカル環境下で開発していく段階でも、
http://localhost:4200
本番用のドメインは既に
https
opensslのインストール
まずはお手元のOS周りにも依りますが、
各OSでのインストール手順は割愛させていただきます。
このブログの内容で動作させているOSは
alpine linux
$ openssl version
OpenSSL 1.1.1d 10 Sep 2019
が、今インストールされているバージョンのようです。
認証キーの生成
それではopensslで
server.key
カレントディレクトリ配下に、とりあえず
ssl
$ mkdir ssl
$ openssl genrsa -out ssl/server.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
..........................................................+++++
.............................................+++++
e is 65537 (0x010001)
$ ls ssl/
server.key
証明書署名要求ファイル(CSR)の作成
次に先程の認証キーから、証明書にあたるファイルを
server.csr
$ openssl req -new -key ssl/server.key -out ssl/server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Kumamoto
Locality Name (eg, city) []:Kumamoto
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Tacos Kingdom LLC
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:contact@tacoskingdom.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:*****************
An optional company name []:
上のような感じで対話的に聞いてくるので、埋められる箇所は埋めておきます。
何事もなければ、
$ ls ssl/
server.csr server.key
となって、CSRが出力されているはずです。
SSL証明書(CRT)の作成
最後に、上の2つのファイルで、SSL証明書であるCRTファイルを作成します。
このファイルは、正規には認可された証明局へ登録・発行されるファイルらしいです。
今回のような用途ではローカルで限定的に使う分には、自分で発行し、自分で認証する、ような使い方になります。
$ openssl x509 -days 365 -req -signkey ssl/server.key -in ssl/server.csr -out ssl/server.crt
Signature ok
subject=C = JP, ST = Kumamoto, O = Tacos Kingdom LLC, CN = localhost
Getting Private key
$ ls ssl/
server.crt server.csr server.key
angular.jsonの編集
さて、なにか任意のangularプロジェクトがあり、先程作成したsslファイルを
angular.json
tree
$ tree
.
├── angular.json
#...他の中身は中略
└── ssl
├── server.crt
├── server.csr
└── server.key
ここから、
angular.json
編集する箇所は以下のような
serve
//...中略
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "ng-playgound:build",
"sslKey": "./ssl/server.key",
"sslCert": "./ssl/server.crt"
},
"configurations": {
"production": {
"browserTarget": "ng-playgound:build:production"
}
}
},
//...以下略
これで
localhost:4200
serveしてみる
では早速、SPAを起動してみます。
起動には
--ssl
$ ng serve --ssl
まずは、
chrome

...いの一番になんだかデカデカと警告がきます。
なんだか期待していたのと違いますが、とにかくそれでも
localhost

となり、それ以降はデカデカとした警告画面なしで、
https://localhost:4200
保護されていない通信
そもそも、さっき作った自分発行のSSL証明は効いているんだろうか?とおもって設定を確認すると、以下のように
証明書(無効)

作成した自分専用の証明書はきっちりと読み込まれているようでした。
その後も

所詮は自分証明書ですので、ブラウザにフィッシーな認証局として扱われているのでしょうか...
これ以上opensslではバージョンごとやブラウザごとに挙動が違いすぎるので、細かい設定を弄ってみても埒が明かない...ので別アプローチを次回以降やってみたいとおもいます。
そうだ、mkcertにしてみよう
openssl
別方向で
openssl設定を用いたSPAのローカルのhttps対応は個人的にハードルが高く断念し、
mkcertのインストール
公式の手順通りに、手元のMacbookでbrewからイントールするのが最も早そうなので以下のコマンドでインストールします。
$ brew install mkcert
$ brew install nss
なおWindowsやlinuxなど他のOSでmkcertを使う場合には公式の手順をご参照ください。
brewでインストールしたら、早速自分認証を発行してみます。
$ mkcert -install
Created a new local CA at "/Users/********/Library/Application Support/mkcert" 💥
Sudo password:**************
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires browser restart)! 🦊
これでmkcertの準備が完了し、実際にSPAで起動したいドメイン名(複数名指定可能)を指定して以下のようにコマンドを叩くと、
% mkcert localhost 127.0.0.1
Using the local CA at "/Users/*******/Library/Application Support/mkcert" ✨
Created a new certificate valid for the following names 📜
- "localhost"
- "127.0.0.1"
The certificate is at "./localhost+1.pem" and the key at "./localhost+1-key.pem" ✅
現在の作業ディレクトリに
localhost+1-key.pem
localhost+1.pem
この2つのファイルは、上記の
openssl
server.key
server.crt
$ cp localhost+1-key.pem {angularのプロジェクトまでのパス}/server.key
$ cp localhost+1.pem {angularのプロジェクトまでのパス}/server.crt
前回の繰り返しになりますが、プロジェクトのフォルダ構造は以下のようになっていると思います。
$ tree
.
├── angular.json
#...他の中身は中略
└── ssl
├── server.crt
└── server.key
動作の確認
ここまで出来たら早速前回と同様SPAを起動してみます。
$ ng serve --ssl
Chromeでの挙動
まずはchromeからSPAの動作を確認してみます。

なんとあれだけの設定で、自分認証が有効になっています。

以下がmkcertで発行された証明書ですが、ローカルOSが発行元になっているようです。

Firefoxでの挙動
最後にfirefox上で確認です。

これもきちんとhttpsとして動作しております。
恐るべしmkcert、といったところです。


個人的には、諦めていたfirefoxでhttps化したSPAが動作するなんて感激です。
まとめ
玄人志向なツールであるopensslに比べて、mkcertを使うことでローカル環境でもお手軽にangularのSPAを常時SSL対応できることがわかりました。
これでよりセキュアなangularのSPA開発が捗りそうです。
参考サイト
記事を書いた人
ナンデモ系エンジニア
主にAngularでフロントエンド開発することが多いです。 開発環境はLinuxメインで進めているので、シェルコマンドも多用しております。 コツコツとプログラミングするのが好きな人間です。
カテゴリー