Configure SSL in Angular & Spring Boot application using Nginx

Viewed 222

I have Spring Boot (backend) application which is running on Tomcat (port 8080) and Angular app (frontend) (port 4200).

Frontend consumes rest API from backend. How can I secure my applications using Nginx with SSL?

I have heard i could configure my Spring Boot as reverse proxy in nginx and then secure it with SSL but i dont know how to do it.

1 Answers

both spring boot and nginx should configure to get https. for java and spring boot you should create certificate file using keytool that you can find it in jdk/bin.

keytool -genkey -alias <desired certificate alias> 
-keystore <path to keystore.pfx>
-storetype PKCS12 
-keyalg RSA 
-storepass <password> 
-validity 730 
-keysize 2048 

then put .p12 file beside application.properties.

for spring boot you should add this in your application.properties or application.yml:

 server.ssl.enabled=true
 server.ssl.key-store=classpath:client.p12
 server.ssl.key-store-password=<password>
 server.ssl.key-store-type=PKCS12
 server.ssl.key-alias=<desired certificate alias>

after that we should create certificate file for angular and nginx and add some configuration to nginx.conf. for creating certificate file you can use openssl tool. create openssl-custom.cnf file add put blow code in that:

    [req]
    default_bits = 2048
    prompt = no
    default_md = sha256
    x509_extensions = v3_req
    distinguished_name = dn

    [dn]
    C = US
    ST = utah
    L = US
    O = IT
    OU = IT Section
    emailAddress = info@example.com
    CN = localhost

    [v3_req]
    subjectAltName = @alt_names

    [alt_names]
    DNS.1 = *.localhost
    DNS.2 = localhost

and then open terminal or cmd and run this command:

#!/bin/bash

openssl req \
    -newkey rsa:2048 \
    -x509 \
    -nodes \
    -keyout server.key \
    -new \
    -out server.crt \
    -config ./openssl-custom.cnf \
    -sha256 \
    -days 365

then put server.key and server.crt in you angular project and add these code to nginx.conf:

listen                  443 ssl;
ssl_certificate           <path to .crt file>/server.crt;
ssl_certificate_key       <path to .key file>/server.key;
ssl_session_cache  builtin:1000  shared:SSL:10m;
ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;

! for using ssl in nginx you should use port 443 not 80; ! for testing your angular open new tab and don't use your old tab

Related