Enable HTTPS in Spring Boot
Enable HTTPS in Spring Boot
This weekend I answered a question about enabling HTTPS in JHipster onstackoverflow that caught a lot of interest on Twitter so I decided to put a short post on it with some more useful details.
JHipster is a Spring Boot application with a lot of neat features and other frameworks completely integrated. The configuration is exactly the same like any other Spring Boot application, including the SSL settings. If you are interested to get a quick introduction on JHipster, feel free to take a look at my Start a modern Java web application with JHipster
If you are using Spring Boot and want to enable SSL (https) for your application on the embedded Tomcat there a few short steps you will need to take.
- Get yourself a SSL certificate: generate a self-signed certifcate or get one from a Certificate Authority
- Enable HTTPS in Spring Boot
- Redirect HTTP to HTTPS (optional)
Step 1: Get a SSL certificate
If you want to use SSL and serve your Spring Boot application over HTTPS you will need to get a certificate.
You have two options to get one. You can generate a self-signed certificate, which will most likely be what you’ll want to do in development since it’s the easiest option. This usually isn’t a good option in production since it will display a warning to the user that your certificate is not trusted.
The other (production) option is to request one from a Certificate Authority. I’ve heard good things about SSLMate to buy your certificate for a reasonable price with excellent support. There are some providers that are able to give out free certificates but usually you’ll have problems down the line if you have any issues or problems (revocations).
Since we are developers, let’s generate a self-signed certificate to get started quickly with development of our application. Every Java Runtime Environment (JRE) comes bundled with a certificate management utility,keytool. This can be used to generate our self-signed certificate. Let’s have a look:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650Enter keystore password: Re-enter new password:What is your first and last name? [Unknown]: What is the name of your organizational unit? [Unknown]: What is the name of your organization? [Unknown]: What is the name of your City or Locality? [Unknown]: What is the name of your State or Province? [Unknown]: What is the two-letter country code for this unit? [Unknown]: Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct? [no]: yes |
This will generate a PKCS12 keystore called keystore.p12 with your newly generate certificate in it, with certificate alias tomcat. You will need to reference keystore in a minute when we start to configure Spring Boot.
Step 2: Enable HTTPS in Spring Boot
By default your Spring Boot embedded Tomcat container will have HTTP on port 8080 enabled. Spring Boot lets you configure HTTP or HTTPS in the application.properties, but not both at once. If you want to enable both you will need to configure at least one programmatically. The Spring Boot reference documentation recommends configuring HTTPS in the application.properties since it’s the more complicated than HTTP.
Using configuration like the example above means the application will no longer support plain HTTP connector at port 8080. Spring Boot doesn’t support the configuration of both an HTTP connector and an HTTPS connector via application.properties. If you want to have both then you’ll need to configure one of them programmatically. It’s recommended to useapplication.properties to configure HTTPS as the HTTP connector is the easier of the two to configure programmatically. See the spring-boot-sample-tomcat-multi-connectors sample project for an example.
Funny enough despite their recommendation to configure HTTPS in the application.properties, their example does the exact opposite.
Let’s configure HTTPS in the default application.properties file undersrc/main/resources of your Spring Boot application:
server.port: 8443server.ssl.key-store: keystore.p12server.ssl.key-store-password: mypasswordserver.ssl.keyStoreType: PKCS12server.ssl.keyAlias: tomcat |
That’s all you need to do to make your application accessible over HTTPS on https://localhost:8443, pretty easy right?
Step 3: Redirect HTTP to HTTPS (optional)
In some cases it might be a good idea to make your application accessible over HTTP too, but redirect all traffic to HTTPS.
To achieve this we’ll need to add a second Tomcat connector, but currently it is not possible to configure two connector in the application.properties like mentioned before. Because of this we’ll add the HTTP connector programmatically and make sure it redirects all traffic to our HTTPS connector.
For this we will need to add theTomcatEmbeddedServletContainerFactory bean to one of our@Configuration classes.
That’s all you need to do to make sure your application is always used over HTTPS!
Enable HTTPS in Spring Boot的更多相关文章
- Spring Boot Admin Reference Guide
1. What is Spring Boot Admin? Spring Boot Admin is a simple application to manage and monitor your S ...
- 区块链使用Java,以太坊 Ethereum, web3j, Spring Boot
Blockchain is one of the buzzwords in IT world during some last months. This term is related to cryp ...
- Spring Boot Cookbook 中文笔记
Spring Boot Cookbook 一.Spring Boot 入门 Spring Boot的自动配置.Command-line Runner RESTful by Spring Boot wi ...
- 《Spring Boot Cook Book》阅读笔记
最近一个月一直在学习Spring Boot框架,在阅读<Spring Boot Cook Book>一书的过程中,记录了一些学习笔记,在这里整理出一篇目录供大家参考. 一.Spring B ...
- Spring Boot Admin 的使用 2
http://blog.csdn.net/kinginblue/article/details/52132113 ******************************************* ...
- Spring Boot 支持 HTTPS 如此简单,So easy!
这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置. ...
- Spring Boot 支持 HTTPS 如此简单,So easy!
这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置. ...
- Spring Boot @Enable*注解源码解析及自定义@Enable*
Spring Boot 一个重要的特点就是自动配置,约定大于配置,几乎所有组件使用其本身约定好的默认配置就可以使用,大大减轻配置的麻烦.其实现自动配置一个方式就是使用@Enable*注解,见其名知 ...
- 在Spring Boot中使用Https
本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...
随机推荐
- FZU2150 Fire Game BFS搜索
题意:就是选两个点出发,只能走草坪,看能不能走完所有的草坪 分析:由于数据范围很小,所有枚举这两个点,事先将所有的草坪点存起来,然后任选两个点走,(两个点可以是同一个点) 然后BFS就行了 注:无解的 ...
- ie 提示浏览器升级信息 干掉ie
<!--[]> <div id=</a> 或以下浏览器: <a href="http://www.mozillaonline.com/"> ...
- 基于寄存器的VM
jvm是基于栈的,基于栈的原因是:实现简单,考虑的就是两个地方,局部变量和操作数栈 http://ifeve.com/javacode2bytecode/这几篇文章相当不错. http://redna ...
- wuzhicms 模块开发
首先,模块开发需要了解五指cms的目录结构: 然后,我们需要新增加一个模块目录: 再app下面创建 如:content 下面包含文件: 前台文件的创建: 看下 index.php 的内容: <? ...
- 50道经典的JAVA编程题 (6-10)
50道经典的JAVA编程题 (6-10),今晚做了10道了,累死了...感觉难度不是很大,就是不知道是不是最好的实现方法啊!希望大神们能给指点哈... [程序6]GCDAndLCM.java 题目:输 ...
- 【Hadoop学习】Apache HBase项目简介
正在撰写,稍后来访……
- Raspberry Pi无线路由器篇
RaspberryPi可以折腾的方法很多,我将会吧自己的折腾经验与大家分享. 作为无线路由器,需要提供dhcp的功能和无线ap的能力,我们分别通过isc-dhcp-server和hostapd这两 ...
- 查找进程对应的PID和对应的端口号
第一步:首先打开任务管理器.之后左键单击查看,点击下面的选择列. 第二步:之后进入如下界面,把PID勾上.这是我们就可以查看到进程的PID(process id)号了 第三步:首先我们打开DOS窗口, ...
- ASP.NET基础系列
一.HttpContext概述 1).如何获取对象: 在WebForm或类库(包括MVC)项目中,通过Current静态属性,就能够获得HttpContext的对象: HttpContext cont ...
- 自定义文件上传的按钮的样式css+js
核心就是一段css遮住了原生的input框,然后用js将文件的值传入到另一个指定的input框中 原文链接 http://geniuscarrier.com/how-to-style-a-html-f ...