Mastering Spring Cloud
上QQ阅读APP看书,第一时间看更新

Registering a secure service

Securing the server side is one thing; registering a secure application is something else. Let's look at how we can do this:

  1. To enable SSL for a Spring Boot application, we need to start with generating a self-signed certificate. I recommend you use keytool for that, which is available under your JRE root in the bin catalog:
keytool -genkey -alias client -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650
  1. Enter the required data and copy the generated keystore file keystore.p12 to your application's src/main/resources catalog. The next step is to enable HTTPS for Spring Boot using configuration properties in application.yml:
server: 
port: ${PORT:8081}
ssl:
key-store: classpath:keystore.p12
key-store-password: 123456
keyStoreType: PKCS12
keyAlias: client
  1. After running the application, you should be able to call the secure endpoint https://localhost:8761/info. We also need to perform some changes in the Eureka client instance configuration:
eureka:
instance:
securePortEnabled: true
nonSecurePortEnabled: false
statusPageUrl: https://${eureka.hostname}:${server.port}/info
healthCheckUrl: https://${eureka.hostname}:${server.port}/health
homePageUrl: https://${eureka.hostname}:${server.port}/