springboot2.0之后,启用https协议的方式与1.*时有点儿不同,贴一下代码。
我的代码能够根据配置参数中的condition.http2https,确定是否启用https协议,如果启用https协议时,会将所有http协议的访问,自动转到https协议上。
一、启动程序
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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协议时的配置
?
|
1
|
server.port=80
|
2.使用https及http协议时的配置
?
|
1
2
3
4
5
6
7
|
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
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://wallimn.iteye.com/blog/2425837
相关文章
猜你喜欢
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 96
-
2025-05-25 26
-
2025-05-25 76
-
2025-06-04 46
-
2025-05-27 108
热门评论

