springboot+https+http
http访问不安全,使用https相对好些。
参考网址:https://blog.csdn.net/bock1984/article/details/90116965
操作如下:
- 1. 使用JDK自带keytool工具,创建本地SSL证书
启动命令行工具,进入jdk的bin目录执行以下命令:
keytool -genkey -v -alias tomcat -keyalg RSA -keystore F:\tomcat.keystore -validity 36500 1.-keyalg 生证书的算法名称,RSA是一种非对称加密算法 .-keystore 生成的证书文件的存储路径 .-validity 证书的有效期

- 2.将生成的tomcat.keystore文件拷贝到springboot项目根目录下:

- 3.修改application.properties文件
·
- 4.启动服务即可访问 https://localhost:8443。
看application.properties配置文件可知,后面只能用https协议访问了。
1) http访问自动转https
(用户前期用http协议,突然改成只用https访问,这样有的客户还用http访问时就访问不到服务器了,针对这种情况可做http访问自动转到https)
package com.nsoft.gkzp.syscore.config; 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.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; /**
* 监听http端口,如访问网址为http协议的,自动转换为Https
*/
@Configuration
@PropertySource(value="classpath:application.properties")
public class HttpsComponent { //读取application.properties配置文件配置的https访问端口号
@Value("${server.port}")
public int SYSTEM_HTTPS_PORT;
//读取application.properties配置文件配置的http监控端口(自动转换为https)
@Value("${server.http.port}")
public int SYSTEM_HTTP_PORT; @Bean
public Connector connector(){ Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(SYSTEM_HTTP_PORT);//Connector监听的http的端口号
connector.setSecure(false);
connector.setRedirectPort(SYSTEM_HTTPS_PORT);//监听到http的端口号后转向到的https的端口号(一般会用443端口)
return connector;
} @Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(){ TomcatServletWebServerFactory tomcat =new TomcatServletWebServerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint=new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection=new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector());
return tomcat;
}
}
package com.nsoft.gkzp.syscore.config; 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.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* 监听http端口,如访问网址为http协议的,自动转换为Https
*/
@Configuration
public class HttpsComponent { @Bean
public Connector connector(){ Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8082);//Connector监听的http的端口号
connector.setSecure(false);
connector.setRedirectPort(8443);//监听到http的端口号后转向到的https的端口号(一般会用443端口)
return connector;
} @Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(){ TomcatServletWebServerFactory tomcat =new TomcatServletWebServerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint=new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection=new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector());
return tomcat;
}
}
直接写端口号,理解更直观些
另:
这边自己生成的证书,是不被公网认证的。如下图。要想公网认证,需要去网上相关机构的去买(将域名提供给他们,他们生成公网可认证的证书),便宜点的大约一年一千多块钱吧。

2) 同时支持http和https访问
(参考 : https://blog.csdn.net/qq_38288606/article/details/89478353)
注意:Spring Boot不支持通过application.properties同时配置HTTP连接器和HTTPS连接器
故我在application.properties配置了https相关配置,然后添加了一个自定义的server.http.port参数,然后新建httpComponent.java配置java类,来启动http端口访问
application.properties

新建类D:\workspace-gzy-gkzp\src\main\java\com\nsoft\gkzp\syscore\config\httpComponent.java
package com.nsoft.gkzp.syscore.config; import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; /**
* 监听http端口,使http访问端口生效
*/
@Configuration
@PropertySource(value="classpath:application.properties")
public class httpComponent { //读取application.properties配置文件配置的http监控端口
@Value("${server.http.port}")
public int SYSTEM_HTTP_PORT; @Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
return tomcat;
} private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(SYSTEM_HTTP_PORT);
return connector;
} }
至此,就可以用 https://localhost 和 http://localhost:8082 访问了。这是spring2.x的配法。

注意: 上面在application.properties配置文件中配置访问端口号,是因为工程用了内置的tomcat容器(如下图pom.xml引入的tomcat依赖)。如果是用外部的tomcat,则直接在tomcat的\conf\server.xml配置文件里配置相关参数。

参考文章:
springboot+https+http的更多相关文章
- springboot https证书配置
如果公司有提供证书如: 拿到证书秘钥可直接在springboot 的配置文件中配置: server.ssl.key-store=classpath:cert.pfx server.ssl.key-st ...
- https证书制作及springboot配置https
1.生成秘钥 openssl genrsa -out private.key 2048 2.生成用于申请请求的证书文件csr,一般会将该文件发送给CA机构进行认证,本例使用自签名证书 openssl ...
- springBoot+springSecurity 数据库动态管理用户、角色、权限
使用spring Security3的四种方法概述 那么在Spring Security3的使用中,有4种方法: 一种是全部利用配置文件,将用户.权限.资源(url)硬编码在xml文件中,已经实现过, ...
- SpringBoot开发案例之打造私有云网盘
前言 最近在做工作流的事情,正好有个需求,要添加一个附件上传的功能,曾找过不少上传插件,都不是特别满意.无意中发现一个很好用的开源web文件管理器插件 elfinder,功能比较完善,社区也很活跃,还 ...
- springCloud、springBoot学习
http://projects.spring.io/spring-cloud/ 官网https://springcloud.cc/spring-cloud-netflix.htmlhttp://cl ...
- SpringBoot 使用Mybatis-Plus
简介 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性 无侵入:Mybatis-Plus 在 My ...
- springboot 有用网址收集
http://www.ityouknow.com/spring-boot.html springboot多数据源配置: https://blog.csdn.net/neosmith/article/d ...
- redis 安装使用 & SpringBoot Redis配置
1.安装 https://www.cnblogs.com/dingguofeng/p/8709476.html https://www.runoob.com/redis/redis-keys.html ...
- java客户端验证https连接(忽略证书验证和证书验证两种方式)
首先根据如下操作生成证书,配置springboot https,生成一个简单的https web服务 https://www.cnblogs.com/qq931399960/p/11889349.ht ...
随机推荐
- PostgreSQL 11 Partitioning Improvements
转自:https://pgdash.io/blog/partition-postgres-11.html PostgreSQL 11, due to be released later this ye ...
- plv8 centos install steps
install deps yum -y update yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL- ...
- 请用js语言实现sort排序函数,要求:sort([4,5,2,-1,0])返回[-1,0,2,4,5]
Array.prototype.sort1=function(fn){ var len=this.length; while(len>0){ for(var i=0;i<len;i++){ ...
- 使用jackson转换xml格式数据进行响应
最近在做微信扫码支付的功能,按照微信开发文档与支付平台进行数据交互只能使用XML格式的数据,调用别人定义的接口就需要按规则来嘛,没办法.自己之前使用jackson,主要是因为SpringMVC默认将j ...
- apply() 函数家族介绍
apply() 函数算是R语言中很基础的一个函数,同时还有 sapply() lapply() tapply() 函数精简了 apply() 函数的用法. apply() 函数是一个很R语言的函数 ...
- helm repository 相关
chart repo是一个可用来存储index.yaml与打包的chart文件的HTTP server.当要分享chart时,需要上传chart文件到chart仓库,任何一个能够提供yaml与tar文 ...
- concurrent(三)互斥锁ReentrantLock & 源码分析
参考文档:Java多线程系列--“JUC锁”02之 互斥锁ReentrantLock:http://www.cnblogs.com/skywang12345/p/3496101.html Reentr ...
- 学Redis这篇就够了!
学Redis这篇就够了! 作者:王爷科技 https://www.toutiao.com/i6713520017595433485 Redis 简介 & 优势 Redis 数据类型 发布订 ...
- Java通过poi创建Excel文件并分页追加数据
以下的main函数,先生成一个excel文件,并设置sheet的名称,设置excel头:而后,以分页的方式,向文件中追加数据 maven依赖 <dependency> <groupI ...
- IIS 10 PHP 运行环境
1.下载php for windows 解压到指定目录 https://windows.php.net/download/ 2.安装iis 勾选CGI 3 ,处理映射 4.添加测试站点测试是否已经 ...