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
分享到:
相关推荐
主要介绍了SpringBoot2.0如何启用https协议,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
可以使用一个@SpringBootApplication注解来启用这三个特性,即:@EnableAutoConfiguration:SpringBoot 2.0
SpringBoot 启用Https,本人亲自实践亲自整理的文档。
Springboot启用async异步调用Springboot启用async异步调用Springboot启用async异步调用
最近在使用hi3559需要开启usb2.0接口,当前的3.0接口可以正常使用,2.0无法使用.后面花费较长时间进行调试,现在将修改方法进行整理,亲测可行.
SAP SSL HTTPS 开启安全连接
主要介绍了startssl申请SSL证书 并且配置 iis 启用https协议,需要的朋友可以参考下
java 调用https发布的webservice接口实例及axis包及详细说明
SIP2协议,全称为:Standard Interchange Protocol Version 2.0,该协议由3M公司提出,用于图书馆的图书管理系统与各类硬件设备之间进行数据交换的标准通讯协议。RFID智能图书管理系统,通过该协议与图书馆的现有...
使用官方提供的 Docker 镜像部署 GitLab 非常方便,相关的安装配置文档也非常详细。本文主要是对一次成功的部署流程进行记录,方便下次快捷部署。
weblogic启用Https1
tomcat配置https,需要的拿走,里面有详细的截图说明!
主要介绍了Springboot启用多个监听端口代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
FP6601Q不仅支持USB BC兼容设备,还支持Apple/Samsung/Huawei设备,并在启用输出电压调整之前自动检测连接的供电设备是否具备QC 2.0/3.0或FCP功能。如果检测到PD不符合QC 2.0/3.0或FCP,则FP6601Q将禁用输出电压...
示例如何在Java 9+上运行的spring-boot 2 non netty(non webflux)应用程序中启用HTTP 2.0 证书 生成自签名证书 # server side: keytool -genkeypair -alias my-keystore -keyalg RSA -keystore src/main/...
weblogic启用https访问设置,内有设置截图,清晰明了
基于springboot下对mysql,mongodb,redis支持ipv6的改造(csdn)————程序
雷蛇雷云2.0是有雷蛇官方推出的一款驱动程序,支持所有的雷蛇产品,可以将游戏的偏好设置完全的进行保存,可随时进行调用,欢迎有需要的朋友下载使用!官方介绍RazerSynapse是一款由雷蛇官方出品的驱动云备份工具,...
它向您展示了如何使用 AOA 2.0 协议来启用 USB 音频和 HID 事件。 在目前的形式中,它只是打开手机的默认音乐应用程序。 编译。 您必须安装 libusb-1.0.0。 gcc -std=gnu99 -o driv android_device.c driver.c ` ...
此插件使序列化程序API可以在Ember Data 2.0中弃用运行。 在Ember Data 2.4中将删除对旧的序列化程序API的支持。 安装 灰烬CLI ember install ember-data-serializer-1x 全局模式 您可以在应用程序中包含文件。 ...