服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - SpringBoot2.0如何启用https协议

SpringBoot2.0如何启用https协议

2021-05-12 14:20wallimn Java教程

这篇文章主要介绍了SpringBoot2.0如何启用https协议,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://wallimn.iteye.com/blog/2425837

延伸 · 阅读

精彩推荐