SpringBoot2.0如何启用https协议

2025-05-29 0 17

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringBoot2.0如何启用https协议 https://www.kuaiidc.com/111258.html

相关文章

发表评论
暂无评论