2019独角兽企业重金招聘Python工程师标准>>> hot3.png

实现访问服务器地址(默认http的80)跳转到https端口(443)

开启端口监听

因为https默认是443端口,需要开启对443端口的监听

在application.properties中

#web监听端口
server.port = 443

配置ssl证书

由于没有从相关机构申请证书,使用jdk自带的证书管理工具进行生成,方法如下

keytool -genkey -alias tomcat  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

这个命令需要交互输入一些密码以及组织的相关信息,这些信息再后面的配置中会使用到

会生成一个名为keystore.p12的文件,这个文件就是我们需要使用的文件,将其放置到resources目录下

在application.properties中配置

#https
server.ssl.enabled=true
server.ssl.key-store= classpath:keystore.p12
server.ssl.protocol=TLS
server.ssl.key-store-password= 自定义
server.ssl.keyStoreType= PKCS12
server.ssl.keyAlias= tomcat

配置跳转

创建下面的配置类

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.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 设置将访问80端口的请求跳转到指定的端口上去,这样用户就可以直接输入设备地址访问而不需要输入前缀和端口了** @author 阿信sxq**/
@Configuration
public class ContainerCustomizer {@Value("${server.port}")private Integer webPort;/*** 构建servlet容器的工厂类* 将80端口跳转到{@linkplain #webPort}端口** @return 内置servlet容器类的工厂实例*/@Beanpublic EmbeddedServletContainerFactory servletContainer() {TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() {@Overrideprotected void postProcessContext(Context context) {SecurityConstraint securityConstraint = new SecurityConstraint();securityConstraint.setUserConstraint("CONFIDENTIAL");SecurityCollection collection = new SecurityCollection();collection.addPattern("/*");securityConstraint.addCollection(collection);context.addConstraint(securityConstraint);}};factory.addAdditionalTomcatConnectors(createConnector());return factory;}/*** 创建tomcat连接器。* 该连接器将会接收http的80端口的访问,并且重定向到指定的端口上去,{@linkplain #webPort}** @return tomcat连接器*/private Connector createConnector() {final Connector connector = new Connector();connector.setPort(80);connector.setRedirectPort(webPort);return connector;}
}

这个是使用内置的tomcat做容器的配置方式,使用jetty需要使用另外的方式

jetty配置跳转

下面的代码来自http://stackoverflow.com/questions/26655875/spring-boot-redirect-http-to-https,
由于没有使用jetty,所以没有进行验证

import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.webapp.AbstractConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;class HttpToHttpsJettyConfiguration extends AbstractConfiguration {// http://wiki.eclipse.org/Jetty/Howto/Configure_SSL#Redirecting_http_requests_to_https@Overridepublic void configure(WebAppContext context) throws Exception {Constraint constraint = new Constraint();constraint.setDataConstraint(2);ConstraintMapping constraintMapping = new ConstraintMapping();constraintMapping.setPathSpec("/*");constraintMapping.setConstraint(constraint);ConstraintSecurityHandler constraintSecurityHandler = new ConstraintSecurityHandler();constraintSecurityHandler.addConstraintMapping(constraintMapping);context.setSecurityHandler(constraintSecurityHandler);}
}
@Configuration
public class HttpToHttpsJettyCustomizer implements EmbeddedServletContainerCustomizer{@Overridepublic void customize(ConfigurableEmbeddedServletContainer container) {JettyEmbeddedServletContainerFactory containerFactory = (JettyEmbeddedServletContainerFactory) container;//Add a plain HTTP connector and a WebAppContext config to force redirect from http->httpscontainerFactory.addConfigurations(new HttpToHttpsJettyConfiguration());containerFactory.addServerCustomizers(server -> {HttpConfiguration http = new HttpConfiguration();http.setSecurePort(443);http.setSecureScheme("https");ServerConnector connector = new ServerConnector(server);connector.addConnectionFactory(new HttpConnectionFactory(http));connector.setPort(80);server.addConnector(connector);});}
}

特别说明

使用上面的方法配置的https由于证书不是受信的,所以浏览器会报警告,需要手工确认,这个没办法

转载于:https://my.oschina.net/songxinqiang/blog/890971

spring-boot实现访问http跳转到https端口的方法相关推荐

  1. nginx 强制使用https访问(http跳转到https)

    nginx强制使用https访问(http跳转到https) 基于nginx搭建了一个https访问的虚拟主机,监听的域名是test.com,但是很多用户不清楚https和http的区别,会很容易敲成 ...

  2. 如何修改嵌入式服务器的端口号,Ai聘网之如何修改Spring Boot应用启动的嵌入式Tomcat的默认端口8080...

    原标题:Ai聘网之如何修改Spring Boot应用启动的嵌入式Tomcat的默认端口8080 Spring Boot是深受广大Java开发人员喜爱的框架,尤其是需要用Java开发微服务的那些开发人员 ...

  3. Spring Boot设置访问url接口后缀

    传统的xml配置方式 <!--Spring MVC 配置--> <servlet><servlet-name>dispatcherServlet</servl ...

  4. Spring Boot之 Configuration Annotation Proessor not found in classpath解决方法

    出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationPropertie ...

  5. Spring Boot——一种包含分页和排序参数的接收方法DEMO

    Maven 主要Maven依赖 <dependency><groupId>org.springframework.boot</groupId><artifac ...

  6. Spring Boot——[Disconnected from the target VM, address: IP:端口, transport: #39socket#39]解决方案

    问题描述 问题分析 1.tomcat出问题了,更换本地的tomcat 2.maven出问题了,更换maven 3.tomcat和maven可能同时出问题,这个当然需要都更换一下了 4.项目中jdk的编 ...

  7. spring boot修改内置容器tomcat的服务端口

    方式一 在spring boot的web 工程中,可以使用内置的web container.有时需要修改服务端口,可以通过配置类和@Configuration注解来完成. // MyConfigura ...

  8. 三、spring boot 1.5.4 web容器定制(端口号等修改)

    spring boot 默认采用tomcat作为嵌入的web容器 定制方式有三种 1. 2.如下 @Componentpublic class CustomizationBean implements ...

  9. IDEA整合Spring Boot项目访问jsp文件

    官方不推荐使用jsp文件作为视图!!! 配置如下: 1,Pom文件中添加依赖: <!--添加jsp依赖 --><dependency><groupId>org.sp ...

  10. Spring boot添加员工页面跳转

    页面跳转 单击添加按钮,跳转到添加页面 可以选择员工的公寓,需要把公寓信息传递过去 添加按钮 <h2><a class="btn btn-sm btn-success&qu ...

最新文章

  1. 放弃使用jQuery实现动画
  2. Max Sum Array 贪心(2500)
  3. 【Oracle】查询当前SCN
  4. K-fold vs. Monte Carlo cross-validation(K折交叉验证与蒙特卡洛交叉验证(MCCV))
  5. ios github客户端_GitHub推出本地iOS和Android客户端
  6. 从“卡脖子”到人有我优,数字孪生盾构机施工流程可视化
  7. Android开发资料超级给力小游戏(精典美女搓搓 妄撮版)源码
  8. VelocityTracker使用总结
  9. 产品读书《重新定义团队:谷歌如何工作》
  10. 水滴筹、轻松筹干架,从线上到线下:员工医院互殴引围观
  11. 宝付国际一文读懂:跨境电商的外汇风险敞口(四)
  12. 阿里巴巴校招offer面经
  13. 产品思维训练 | 常见的用户增长手段有哪些?
  14. 婚姻家庭法重点复习提纲
  15. bilibili 视频网址
  16. Spring-Boot框架学习视频-百度云盘
  17. mysql 汉字字母拼音_mysql 汉字按拼音字母排序、获取拼音首字母、拼音全拼
  18. 护眼郎与您分享各国如何千方百计帮孩子远离近视
  19. Nginx “邪恶” rewrite
  20. office 2016 安装

热门文章

  1. java json字符串转成 Map或List
  2. NOI2003 文本编辑器
  3. 将Excel(.xlsx)中的数据导入到数据库中
  4. 一套完整自定义工作流的实现
  5. 转:《潜伏》映射办公室潜规则
  6. C++ 引用计数技术及智能指针的简单实现
  7. 隐马尔可夫的知识要点
  8. GCD介绍(二): 多核心的性能
  9. .net 2.0 只读TextBox取值问题
  10. 全军覆没!麻省理工零录取中国学生,斯坦福取消中国大陆面试! 这是怎么了?...