Skip to content

Instantly share code, notes, and snippets.

@RaphaelAudet
Forked from barmic/tuto.md
Created April 5, 2022 07:51
Show Gist options
  • Save RaphaelAudet/15961c3404cbd3cd52920c226f0eeef9 to your computer and use it in GitHub Desktop.
Save RaphaelAudet/15961c3404cbd3cd52920c226f0eeef9 to your computer and use it in GitHub Desktop.
Authentification par certificat avec nginx

Pour mettre en place une authentification par certificat avec nginx comme reverse proxy.

Création des certificats

Créer l'authorité de certification

openssl genrsa -des3 -out ca.key 4096
openssl req -new -x509 -days 365 -key ca.key -out ca.crt

À faire par le client, création de son certificat et création de la demande de signature (csr)

openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr

Signature du certificat client par l'authorité de certification créée plus haut.

openssl x509 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt

Configuration de nginx

La création des fichiers server.crt et server.key ne sont pas décrit ici. On met en place l'authorité de certification pour les clients et on oblige pas l'authentification.

ssl on;
ssl_certificate /etc/nginx/server.crt;
ssl_certificate_key /etc/nginx/server.key;
ssl_client_certificate /etc/nginx/ca.crt;

ssl_verify_client optional;

On ajoute les information d'authentification aux entêtes http pour l'envoi au serveur tomcat.

location / {
    proxy_set_header dn_cert $ssl_client_s_dn;
    proxy_set_header VERIFIED $ssl_client_verify;
    proxy_pass  http://127.0.0.1:4242;
}

Tests

Pour tenter des requêtes authentifiées facilement, on peut lancer la commande :

curl -v -s -k --key client.key --cert client.crt https://exemple.com/api/myapi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment