SpringBoot Web Https 配置

不管是游戏服务器开发,还是其它服务开发,越来越多的平台都要求服务端必须支持https的访问。以增加安全性。比如目前火热的小程序,要求服务端必须支持https,苹果商店也有说http请求要修改为https。所以https将会是游戏服务器的普遍需求。

一,证书生成

   证书可以自己使用jdk生成进行测试。但是在正常使用的时候,需要去第三方机构购买,网上也有免费的。不过有效期有限制。具体获取证书的方法这里不再详细说明了。一般拿到证书之后会得到这几个文件:

   cert.pem chain.pem   fullchain.pem  privkey.pem

二,将pem文件转化为keystore文件

如果使用nginx跳转的话,上面的证书文件可以直接使用,但是在tomcat中,证书的配置文件格式必须是.keystore的文件。所以需要做一下转化。

1、生成pkcs12格式的密钥文件:

$ openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out my.pk12 -name mykey

(注:此过程中需要输入密码:123456)

2、生成keystore:

$ keytool -importkeystore -deststorepass 123456 -destkeypass 123456 -destkeystore my.keystore -srckeystore my.pk12 -srcstoretype PKCS12 -srcstorepass 123456 -alias shkey

成功之后会获得my.keystore文件。

三,在Spring boot web中配置https

首先是在application.properties中添加配置

1

2

3

4

5

server.port=  8446

server.ssl.key-store=/user/cert/my.keystore

server.ssl.key-store-password= 123456

这样配置之后,启动服务,就可以https访问了。

四,同时支持http和https访问

1,http请求不跳转成https访问

这种方式是http请求单独走一个端口,https请求单独走一个端口。但是spring boot 的appplication.properties只能配置一个端口,这就需要我们手动再添加一个Connector了。

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

@Configuration

public  class  TomcatConfig {

@Bean

public  EmbeddedServletContainerFactory servletContainerFactory(){

TomcatEmbeddedServletContainerFactory tomcatConfig =  new  TomcatEmbeddedServletContainerFactory();

tomcatConfig.addAdditionalTomcatConnectors( this .newHttpConnector());

return  tomcatConfig;

}

private  Connector newHttpConnector() {

Connector connector =  new  Connector( "org.apache.coyote.http11.Http11NioProtocol" );

connector.setScheme( "http" );

connector.setPort( 8080 );

connector.setSecure( false );

return  connector;

}

}

这样普通 的http请求,可以访问8080端口了。

2,将http请求强制跳转到https

有时候我们的一些旧业务是使用的http,但是新业务以及将来的框架都必须强制使用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

@Configuration

public  class  TomcatConfig {

@Bean

public  EmbeddedServletContainerFactory servletContainerFactory(){

TomcatEmbeddedServletContainerFactory tomcatConfig =  new  TomcatEmbeddedServletContainerFactory(){

@Override

protected  void  postProcessContext(Context context) {

SecurityConstraint securityConstraint =  new  SecurityConstraint();

securityConstraint.setUserConstraint( "CONFIDENTIAL" );

SecurityCollection collection =  new  SecurityCollection();

// 这里不知道为什么,只能配置以/*结尾的path。这样配置表示全部请求使用安全模式,必须走https

collection.addPattern( "/*" );

//另外还可以配置哪些请求必须走https,这表示以/home/开头的请求必须走https

collection.addPattern( "/home/*" );

securityConstraint.addCollection(collection);

context.addConstraint(securityConstraint);

}

};

tomcatConfig.addAdditionalTomcatConnectors( this .newHttpConnector());

return  tomcatConfig;

}

private  Connector newHttpConnector() {

Connector connector =  new  Connector( "org.apache.coyote.http11.Http11NioProtocol" );

connector.setScheme( "http" );

connector.setPort( 8080 );

connector.setSecure( false );

// 如果只需要支持https访问,这里把收到的http请求跳转到https的端口

connector.setRedirectPort( 8446 );

return  connector;

}

}

以上跳转也可以使用nginx实现。

评论

暂无评论