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 ...
随机推荐
- lodop和c-lodop通过打印状态和任务不在队列获取打印成功
之前的博文有通过判断pirnt的返回值,判断加入队列算打印成功,建议使用这种简单的判断方法.此外还有其他判断方法,例如通过PRINT_STATUS_OK判断,但是这个状态不是所有打印机能支持这种判断成 ...
- js判断json对象是否为空
if("{}" == JSON.stringify(json对象)) { // 满足条件就是空 }
- htm5 css学习笔记整理
点击链接后退页面: <a href="javascript:history.go(-1)">回到上一个网页</a> ——修改placeholder提示的样式 ...
- P1993 小K的农场(差分约束)
小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了 ...
- Java program to find the largest element in array
Java program to find the largest element in array Given an array of numbers, write a java program to ...
- [转帖]AMD:Zen 2霄龙处理器每美元性能可达英特尔至强5.6倍
AMD:Zen 2霄龙处理器每美元性能可达英特尔至强5.6倍 2019-10-20 6:35:38来源:IT之家作者:孤城责编:孤城评论:32 https://www.ithome.com/0/451 ...
- [转帖]crontab每小时运行一次
crontab每小时运行一次 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错误的!比如对于下面的这种写法: 00 * * * * #每隔一小时执行一 ...
- Spring AOP日志实现(三)--获取访问者用户名
通过Security获取访问者用户名: 也可以通过session来获取: 整体思路:
- io.lettuce.core.protocol.ConnectionWatchdog - Reconnecting, last destination was ***
一.问题 redis起来后一直有重连的日志,如下图: 二.分析 参考lettuce-core的github上Issues解答https://github.com/lettuce-io/lettuce- ...
- vue npm run build 失败
之前删除过 node-moudel 文件夹,然后 npm install 重新安装,一切OK.打包的时候,报错,找不到caniuse什么的.再删除node-moudel,重新cnpm install ...