SpringBoot2.0之后,启用https协议的方式与1.*时有点儿不同,贴一下代码。

  我的代码能够根据配置参数中的condition.http2https,确定是否启用https协议,如果启用https协议时,会将所有http协议的访问,自动转到https协议上。

一、启动程序

  1. package com.wallimn.iteye.sp.asset;
  2. import org.apache.catalina.Context;
  3. import org.apache.catalina.connector.Connector;
  4. import org.apache.tomcat.util.descriptor.web.SecurityCollection;
  5. import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.boot.SpringApplication;
  8. import org.springframework.boot.autoconfigure.SpringBootApplication;
  9. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  10. import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
  11. import org.springframework.context.annotation.Bean;
  12. /**
  13. * SpringBoot2.0启动程序
  14. * @author wallimn,http://wallimn.iteye.com
  15. *
  16. */
  17. @SpringBootApplication
  18. public class AssetApplication {
  19. public static void main(String[] args) {
  20. SpringApplication.run(AssetApplication.class, args);
  21. }
  22. //如果没有使用默认值80
  23. @Value("${http.port:80}")
  24. Integer httpPort;
  25. //正常启用的https端口 如443
  26. @Value("${server.port}")
  27. Integer httpsPort;
  28. // springboot2 写法
  29. @Bean
  30. @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)
  31. public TomcatServletWebServerFactory servletContainer() {
  32. TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
  33. @Override
  34. protected void postProcessContext(Context context) {
  35. SecurityConstraint constraint = new SecurityConstraint();
  36. constraint.setUserConstraint("CONFIDENTIAL");
  37. SecurityCollection collection = new SecurityCollection();
  38. collection.addPattern("/*");
  39. constraint.addCollection(collection);
  40. context.addConstraint(constraint);
  41. }
  42. };
  43. tomcat.addAdditionalTomcatConnectors(httpConnector());
  44. return tomcat;
  45. }
  46. @Bean
  47. @ConditionalOnProperty(name="condition.http2https",havingValue="true", matchIfMissing=false)
  48. public Connector httpConnector() {
  49. System.out.println("启用http转https协议,http端口:"+this.httpPort+",https端口:"+this.httpsPort);
  50. Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
  51. connector.setScheme("http");
  52. //Connector监听的http的端口号
  53. connector.setPort(httpPort);
  54. connector.setSecure(false);
  55. //监听到http的端口号后转向到的https的端口号
  56. connector.setRedirectPort(httpsPort);
  57. return connector;
  58. }}

二、配置文件

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

pringBoot2.0启用https协议的更多相关文章

  1. 【转】Tomcat启用HTTPS协议配置过程

    转载请注明出处: http://blog.csdn.net/gane_cheng/article/details/53001846 http://www.ganecheng.tech/blog/530 ...

  2. Tomcat启用HTTPS协议配置过程

    Article1较为简洁,Article2较为详细,测试可行. Article1 概念简介 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问 ...

  3. 启用https协议的方法

    提醒:启用https协议会降低服务器性能,如非必要不必启用 一.用openssl生成密钥.证书: 1.生成RSA密钥的方法 openssl genrsa -out privkey.pem 2048 建 ...

  4. mod_cluster启用https协议的步骤

    1.生成SSL证书与私钥 Generate Private Key on the Server Running Apache + mod_ssl First, generate a private k ...

  5. JBoss集群中启用HTTPS协议

    Generate server certificate Note: If you already have certificate created then this section can be i ...

  6. 4、Tomcat启用HTTPS协议配置

    一.本地模拟 1.安装jdk,自行百度安装教程 2.打开mac终端,输入命令 keytool -genkeypair -alias "证书名字" -keyalg "RSA ...

  7. IIS中如何应用程序启用https协议

    首先已经安装完了SSL证书 1.找到需要添加的站点,右击 ---> 编辑绑定-->添加--->选择  ""https"-->选择"SSL ...

  8. 阿里云服务器IIS启用HTTPS协议(转)

    https://www.cnblogs.com/randytech/p/7017188.html

  9. tomcat 启用https协议

    利用tomcat服务器配置https双向认证. 1.为服务器生成证书 打开cmd,进入jdk的bin目录下,输入下面的命令: keytool -genkey -v -alias tomcat -key ...

随机推荐

  1. 【Leetcode_easy】1025. Divisor Game

    problem 1025. Divisor Game 参考 1. Leetcode_easy_1025. Divisor Game; 完

  2. 2019年Java面试题基础系列228道(5)

    21.存在两个类,B 继承 A,C 继承 B,我们能将 B 转换为C 么?如 C = (C) B: 这属于强制类型转换,如果被转换的B实例不是C类型,会有异常 比如你的ABC分别对应动物,猫,黑猫. ...

  3. C++ 宏和模板简介

    参考<21天学通C++>第14章节,对C++中的宏和模板进行了学习,总结起来其主要内容如下: (1) 预处理器简介 (2) 关键字#define与宏 (3) 模板简介 (4) 如何编写函数 ...

  4. java端同时获取批量上传图片及其他数据

    Controller层: @ResponseBody @RequestMapping(value = "", method = RequestMethod.POST) public ...

  5. 稀疏数组(java实现)

    1.稀疏数组 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 稀疏数组的处理方法是: 1.1记录数组一共有几行几列,有多少个不同的值 1.2把具有不同值的元素的行列 ...

  6. SQLLite日期时间函数

    SQLLite包含了如下时间/日期函数:datetime().......................产生日期和时间date()...........................产生日期tim ...

  7. Django项目配置参数大全

    数据库的配置 配置文件: settings.pyDATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NA ...

  8. C++打印杨辉三角形

    #include <iostream> #include <iomanip> #include <Windows.h> using namespace std; # ...

  9. HCIA SWITCHING&ROUTTING 笔记——第一章 TCP/IP基础知识(2)

    视频地址:https://ilearningx.huawei.com/courses/course-v1:HuaweiX+EBGTC00000336+Self-paced/courseware/abb ...

  10. spring的事务解决方案之@Transactional注解

    首先此注解位于 org.springframework.transaction.annotation 这个包路径下面, 事务有两种类别,一种是编程式事务,另一种是声明式事务,显然此注解是声明式事务,这 ...