spring-boot内嵌三大容器https设置

spring-boot默认的内嵌容器为tomcat,除了tomcat之前还可以设置jetty和undertow。

1.设置https

spring-boot默认http端口为8080,可以在配置文件中通过server.port来修改端口值。

server:
port: 8080

设置https访问只需通过增加配置信息:

server:
port: 8080
ssl:
key-store: classpath:https.jks
key-store-type: JKS
key-store-password: 123456

不过这样设置后http访问不了,只能使用https访问了。我们当然是希望能够兼容,最好是http请求能够自动跳转到https。所以我们增加一个自定义的配置项http.port(因为增加了https访问,所以server.port端口属性被https使用,故增加http端口)

http:
port: 80
server:
port: 443
ssl:
key-store: classpath:https.jks
key-store-type: JKS
key-store-password: 123456

这样配置后,我们希望无论是http://localhost还是https://localhost都能正常访问项目,而且http://localhost还能自动跳转到https://localhost

2.tomcat

spring-boot内嵌容器默认为tomcat,所以我们无需引用其他依赖即可使用

增加配置类

package com.github.yvanchen;

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.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.servlet.Servlet; /**
* @author evan.chen
* @date 2019/11/25 10:29
*/
@Configuration
public class TomcatHttpsConfig { @Value("${server.port}")
protected int httpsPort; @Value("${http.port}")
protected int httpPort; @Bean
public ServletWebServerFactory servletWebServerFactory() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
//开启HTTP自动跳转至HTTPS
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
Connector connector = new Connector();
connector.setPort(httpPort);
connector.setRedirectPort(httpsPort);
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
}

3.jetty

需要排除默认tomcat,增加jetty

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

增加配置类

package com.github.yvanchen;

import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.webapp.AbstractConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author evan.chen
* @date 2019/11/25 10:29
*/
@Configuration
public class JettyHttpsConfig { @Value("${server.port}")
protected int httpsPort; @Value("${http.port}")
protected int httpPort; @Bean
public ServletWebServerFactory servletWebServerFactory() {
JettyServletWebServerFactory jetty = new JettyServletWebServerFactory();
jetty.addConfigurations(new AbstractConfiguration() { @Override
public void configure(WebAppContext context) {
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);
}
}); jetty.addServerCustomizers((Server server) -> {
HttpConfiguration http = new HttpConfiguration();
http.setSecurePort(httpsPort);
ServerConnector connector = new ServerConnector(server);
connector.addConnectionFactory(new HttpConnectionFactory(http));
connector.setPort(httpPort); server.addConnector(connector);
});
return jetty;
}
}

3.undertow

需要排除默认tomcat,增加undertow

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

增加配置类

package com.github.yvanchen;

import io.undertow.Undertow;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.SecurityInfo;
import io.undertow.servlet.api.TransportGuaranteeType;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author evan.chen
* @date 2019/11/25 10:29
*/
@Configuration
public class UndertowHttpsConfig { @Value("${server.port}")
protected int httpsPort; @Value("${http.port}")
protected int httpPort; @Bean
public ServletWebServerFactory servletWebServerFactory() {
UndertowServletWebServerFactory undertow = new UndertowServletWebServerFactory();
undertow.addBuilderCustomizers((Undertow.Builder builder) -> {
builder.addHttpListener(httpPort, "0.0.0.0");
});
undertow.addDeploymentInfoCustomizers(deploymentInfo -> {
// 开启HTTP自动跳转至HTTPS
deploymentInfo.addSecurityConstraint(new SecurityConstraint()
.addWebResourceCollection(new WebResourceCollection().addUrlPattern("/*"))
.setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
.setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
.setConfidentialPortManager(exchange -> httpsPort);
});
return undertow;
}
}

总结

以上就是对三大内嵌容器设置https的过程

spring-boot内嵌三大容器https设置的更多相关文章

  1. Spring Boot 容器选择 Undertow 而不是 Tomcat Spring Boot 内嵌容器Unde

    Spring Boot 内嵌容器Undertow参数设置 配置项: # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程 # 不要设置过大,如果过大,启动 ...

  2. Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现

    Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现 Anoyi 精讲JAVA 精讲JAVA 微信号 toooooooozi 功能介绍 讲解java深层次 ...

  3. 如何优雅的关闭基于Spring Boot 内嵌 Tomcat 的 Web 应用

    背景 最近在搞云化项目的启动脚本,觉得以往kill方式关闭服务项目太粗暴了,这种kill关闭应用的方式会让当前应用将所有处理中的请求丢弃,响应失败.这种形式的响应失败在处理重要业务逻辑中是要极力避免的 ...

  4. Spring Boot内嵌Tomcat session超时问题

    最近让Spring Boot内嵌Tomcat的session超时问题给坑了一把. 在应用中需要设置session超时时间,然后就习惯的在application.properties配置文件中设置如下, ...

  5. Spring Boot 内嵌Tomcat的端口号的修改

    操作非常的简单,不过如果从来没有操作过,也是需要查找一下资料的,所以,在此我简单的记录一下自己的操作步骤以备后用! 1:我的Eclipse版本,不同的开发工具可能有所差异,不过大同小异 2:如何进入对 ...

  6. Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

    之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...

  7. 精尽Spring Boot源码分析 - 内嵌Tomcat容器的实现

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  8. spring boot项目发布tomcat容器(包含发布到tomcat6的方法)

    spring boot因为内嵌tomcat容器,所以可以通过打包为jar包的方法将项目发布,但是如何将spring boot项目打包成可发布到tomcat中的war包项目呢? 1. 既然需要打包成wa ...

  9. (7)Spring Boot web开发 --- servlet容器

    文章目录 配置嵌入式 Servlet 容器 注册 三大组件 使用其他 servlet 容器 使用外置的 `Servlet` 容器 配置嵌入式 Servlet 容器 Spirng Boot 默认使用自带 ...

随机推荐

  1. Pytorch Bi-LSTM + CRF 代码详解

    久闻LSTM + CRF的效果强大,最近在看Pytorch官网文档的时候,看到了这段代码,前前后后查了很多资料,终于把代码弄懂了.我希望在后来人看这段代码的时候,直接就看我的博客就能完全弄懂这段代码. ...

  2. Java语法格式

    任何一种语言都有自己的语法规则,Java也一样,既然是规则,那么知道其如何使用就可以了. 代码都定义在类中,类由class来定义,区分 public class  和  class; 代码严格区分大小 ...

  3. 如何将为Android开发的AIR应用转移到SD卡

    如果你想用户能够将为Android开发的AIR应用转移到SD卡上,所需要做的是在你的应用程序描述符中修改一下.如果你想你的应用程序默认安装到内置存储器中,但是允许用户把它转移到SD卡上,设置andro ...

  4. Springboot 自定义多个404页面

    在Springboot中,可以通过修改配置.或者在static文件夹下添加error文件夹引入个性化的404模版.但是如果需要针对不同url地址规则,返回不同样式的404页面,则难以实现了.针对这个问 ...

  5. Github开源人脸识别项目face_recognition

    Github开源人脸识别项目face_recognition 原文:https://www.jianshu.com/p/0b37452be63e 译者注: 本项目face_recognition是一个 ...

  6. H3C通过桥ID决定端口角色

  7. 2018-8-10-win10-uwp-Window.Current.Dispatcher中Current为null

    title author date CreateTime categories win10 uwp Window.Current.Dispatcher中Current为null lindexi 201 ...

  8. 洛谷——P1305 新二叉树(新建二叉树以及遍历)

    题目描述输入一串二叉树,用遍历前序打出. 输入输出格式输入格式: 第一行为二叉树的节点数n.(n \leq 26n≤26) 后面n行,每一个字母为节点,后两个字母分别为其左右儿子. 空节点用*表示 输 ...

  9. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  10. 基于jquery的带事件显示功能的日历板插件calendar.js

    项目中需要用到一个日历板控件,要求能显示事件,于是想到了一年前在app项目上写的一个粗略版日历板,然后又想着这个可能以后还会用 于是我就封装了一下,能满足基本要求,如果有需要更多功能的也可以自行修改源 ...