An open API service indexing awesome lists of open source software.

https://github.com/tfkfan/mtls_jwt_demo

spring-boot mtls+stateless JWT authentication demo
https://github.com/tfkfan/mtls_jwt_demo

jwt mtls spring-boot

Last synced: 8 months ago
JSON representation

spring-boot mtls+stateless JWT authentication demo

Awesome Lists containing this project

README

          

# Mutual TLS + JWT authentication spring boot example

Not for development/production use
This project is designed to better understand how tls protocol
and JWT tokens processing work. JWT/auth components, filters etc are already implemented in
spring-boot-starter-oauth2-resource-server and spring-boot-starter-oauth2-client

## Keys generation

Because MTLS is bi-directional secured communication
we need to generate keypairs for client and server both as well to encrypt each other's data

### Ca keypair generation

sh generate-ca.sh

### Server keypair, keystore, truststore generation

sh generate-ssl.sh ./ca server ssl-server.cnf

### Client keypair, keystore, truststore generation

sh generate-ssl.sh ./ca client ssl-client.cnf

## Spring boot application settings

Use /config/application.yml with already defined settings for your app
Put already generated keystore.jks and truststore.jks into src/main/resources

Run from project root

mvn clean package
java -jar ./target/MTLS_JWT_DEMO-1.0-SNAPSHOT.jar

## Requests

First of all, we need to authenticate via JWT controller over HTTPs/MTLS and get bearer token:

curl -v --cert client.crt --key client.key --cacert ca.crt --header "Content-Type: application/json" --request POST https://localhost:8081/authenticate --data '{ "username":"user1", "password":"password"}'

Then you receive JWT token like

eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyMSIsImlhdCI6MTcwMTE5MDExMCwiZXhwIjoxNzAxMjA4MTEwfQ.3VOYkN5JbaxX7sxY5WV0L0wiaWhdIaXvY8BIV6dIdxnOMmh9R0UDFUIXXu10rIPyTfZmbCIdNYd3b5aWNyGL-g

You should put this token as bearer header to other further requests

Now you can run test method

curl -v --cert client.crt --key client.key --cacert ca.crt --header "Content-Type: application/json" --header "Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ1c2VyMSIsImlhdCI6MTcwMTE5MDI0OSwiZXhwIjoxNzAxMjA4MjQ5fQ.Nx-ZUTRaVMbonc_PrJro11Qi3Ab4PGmttbmfYA9baoQC67te07-j-iH7PjYbTV9JpaW2lw0C7_dI2PXBhoNvrA" --request GET https://localhost:8081/api/test

## Security disable

To disable JWT security authentication set parameter

security.enabled: false

in your config/application.yml

Try to make following request

curl -v --cert client.crt --key client.key --cacert ca.crt --header "Content-Type: application/json" --header --request GET https://localhost:8081/api/test

As you can see server and client have exchanged with public keys/certificates and the connection becames secured and encrypted on both sides