pringBoot2.0启用https协议
SpringBoot2.0之后,启用https协议的方式与1.*时有点儿不同,贴一下代码。
我的代码能够根据配置参数中的condition.http2https,确定是否启用https协议,如果启用https协议时,会将所有http协议的访问,自动转到https协议上。
一、启动程序
- package com.wallimn.iteye.sp.asset;
- import org.apache.catalina.Context;
- import org.apache.catalina.connector.Connector;
- import org.apache.tomcat.util.descriptor.web.SecurityCollection;
- import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
- import org.springframework.context.annotation.Bean;
- /**
- * SpringBoot2.0启动程序
- * @author wallimn,http://wallimn.iteye.com
- *
- */
- @SpringBootApplication
- public class AssetApplication {
- public static void main(String[] args) {
- SpringApplication.run(AssetApplication.class, args);
- }
- //如果没有使用默认值80
- @Value("${http.port:80}")
- Integer httpPort;
- //正常启用的https端口 如443
- @Value("${server.port}")
- Integer httpsPort;
- // springboot2 写法
- @Bean
- @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)
- public TomcatServletWebServerFactory servletContainer() {
- TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
- @Override
- protected void postProcessContext(Context context) {
- SecurityConstraint constraint = new SecurityConstraint();
- constraint.setUserConstraint("CONFIDENTIAL");
- SecurityCollection collection = new SecurityCollection();
- collection.addPattern("/*");
- constraint.addCollection(collection);
- context.addConstraint(constraint);
- }
- };
- tomcat.addAdditionalTomcatConnectors(httpConnector());
- return tomcat;
- }
- @Bean
- @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)
- public Connector httpConnector() {
- System.out.println("启用http转https协议,http端口:"+this.httpPort+",https端口:"+this.httpsPort);
- Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
- connector.setScheme("http");
- //Connector监听的http的端口号
- connector.setPort(httpPort);
- connector.setSecure(false);
- //监听到http的端口号后转向到的https的端口号
- connector.setRedirectPort(httpsPort);
- return connector;
- }}
二、配置文件
1.使用http协议时的配置
server.port=80
2.使用https及http协议时的配置
server.port=443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=your-password
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=your-cert-alias
condition.http2https=true
http.port=80
pringBoot2.0启用https协议的更多相关文章
- 【转】Tomcat启用HTTPS协议配置过程
转载请注明出处: http://blog.csdn.net/gane_cheng/article/details/53001846 http://www.ganecheng.tech/blog/530 ...
- Tomcat启用HTTPS协议配置过程
Article1较为简洁,Article2较为详细,测试可行. Article1 概念简介 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问 ...
- 启用https协议的方法
提醒:启用https协议会降低服务器性能,如非必要不必启用 一.用openssl生成密钥.证书: 1.生成RSA密钥的方法 openssl genrsa -out privkey.pem 2048 建 ...
- mod_cluster启用https协议的步骤
1.生成SSL证书与私钥 Generate Private Key on the Server Running Apache + mod_ssl First, generate a private k ...
- JBoss集群中启用HTTPS协议
Generate server certificate Note: If you already have certificate created then this section can be i ...
- 4、Tomcat启用HTTPS协议配置
一.本地模拟 1.安装jdk,自行百度安装教程 2.打开mac终端,输入命令 keytool -genkeypair -alias "证书名字" -keyalg "RSA ...
- IIS中如何应用程序启用https协议
首先已经安装完了SSL证书 1.找到需要添加的站点,右击 ---> 编辑绑定-->添加--->选择 ""https"-->选择"SSL ...
- 阿里云服务器IIS启用HTTPS协议(转)
https://www.cnblogs.com/randytech/p/7017188.html
- tomcat 启用https协议
利用tomcat服务器配置https双向认证. 1.为服务器生成证书 打开cmd,进入jdk的bin目录下,输入下面的命令: keytool -genkey -v -alias tomcat -key ...
随机推荐
- 【Leetcode_easy】1025. Divisor Game
problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完
- 2019年Java面试题基础系列228道(5)
21.存在两个类,B 继承 A,C 继承 B,我们能将 B 转换为C 么?如 C = (C) B: 这属于强制类型转换,如果被转换的B实例不是C类型,会有异常 比如你的ABC分别对应动物,猫,黑猫. ...
- C++ 宏和模板简介
参考<21天学通C++>第14章节,对C++中的宏和模板进行了学习,总结起来其主要内容如下: (1) 预处理器简介 (2) 关键字#define与宏 (3) 模板简介 (4) 如何编写函数 ...
- java端同时获取批量上传图片及其他数据
Controller层: @ResponseBody @RequestMapping(value = "", method = RequestMethod.POST) public ...
- 稀疏数组(java实现)
1.稀疏数组 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 1.1记录数组一共有几行几列,有多少个不同的值 1.2把具有不同值的元素的行列 ...
- SQLLite日期时间函数
SQLLite包含了如下时间/日期函数:datetime().......................产生日期和时间date()...........................产生日期tim ...
- Django项目配置参数大全
数据库的配置 配置文件: settings.pyDATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NA ...
- C++打印杨辉三角形
#include <iostream> #include <iomanip> #include <Windows.h> using namespace std; # ...
- HCIA SWITCHING&ROUTTING 笔记——第一章 TCP/IP基础知识(2)
视频地址:https://ilearningx.huawei.com/courses/course-v1:HuaweiX+EBGTC00000336+Self-paced/courseware/abb ...
- spring的事务解决方案之@Transactional注解
首先此注解位于 org.springframework.transaction.annotation 这个包路径下面, 事务有两种类别,一种是编程式事务,另一种是声明式事务,显然此注解是声明式事务,这 ...