SpringBoot Web Https 配置

不管是游戏服务器开发,还是其它服务开发,越来越多的平台都要求服务端必须支持https的访问。以增加安全性。比如目前火热的小程序,要求服务端必须支持https,苹果商店也有说http请求要修改为https。所以https将会是游戏服务器的普遍需求。

一,证书生成

证书可以自己使用jdk生成进行测试。但是在正常使用的时候,需要去第三方机构购买,网上也有免费的。不过有效期有限制。具体获取证书的方法这里不再详细说明了。一般拿到证书之后会得到这几个文件:

cert.pem chain.pem   fullchain.pem  privkey.pem

二,将pem文件转化为keystore文件

如果使用nginx跳转的话,上面的证书文件可以直接使用,但是在tomcat中,证书的配置文件格式必须是.keystore的文件。所以需要做一下转化。

1、生成pkcs12格式的密钥文件:

$ openssl pkcs12 -export -in cert.pem -inkey privkey.pem -out my.pk12 -name mykey

(注:此过程中需要输入密码:123456)

2、生成keystore:

$ keytool -importkeystore -deststorepass 123456 -destkeypass 123456 -destkeystore my.keystore -srckeystore my.pk12 -srcstoretype PKCS12 -srcstorepass 123456 -alias shkey

成功之后会获得my.keystore文件。

三,在Spring boot web中配置https

首先是在application.properties中添加配置

server.port= 8446

server.ssl.key-store=/user/cert/my.keystore

server.ssl.key-store-password=123456

  

这样配置之后,启动服务,就可以https访问了。

四,同时支持http和https访问

1,http请求不跳转成https访问

这种方式是http请求单独走一个端口,https请求单独走一个端口。但是spring boot 的appplication.properties只能配置一个端口,这就需要我们手动再添加一个Connector了。

@Configuration

public class TomcatConfig {

@Bean

public EmbeddedServletContainerFactory servletContainerFactory(){

TomcatEmbeddedServletContainerFactory tomcatConfig = new TomcatEmbeddedServletContainerFactory();

tomcatConfig.addAdditionalTomcatConnectors(this.newHttpConnector());

return tomcatConfig;

}

private Connector newHttpConnector() {

Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

connector.setScheme("http");

connector.setPort(8080);

connector.setSecure(false);

return connector;

}

}

  

这样普通 的http请求,可以访问8080端口了。

2,将http请求强制跳转到https

有时候我们的一些旧业务是使用的http,但是新业务以及将来的框架都必须强制使用https,那就需要做一下跳转,把收到的http请求强制跳转到https上面。

@Configuration

public class TomcatConfig {

@Bean

public EmbeddedServletContainerFactory servletContainerFactory(){

TomcatEmbeddedServletContainerFactory tomcatConfig = new TomcatEmbeddedServletContainerFactory(){

@Override

protected void postProcessContext(Context context) {

SecurityConstraint securityConstraint = new SecurityConstraint();

securityConstraint.setUserConstraint("CONFIDENTIAL");

SecurityCollection collection = new SecurityCollection();

// 这里不知道为什么,只能配置以/*结尾的path。这样配置表示全部请求使用安全模式,必须走https

collection.addPattern("/*");

//另外还可以配置哪些请求必须走https,这表示以/home/开头的请求必须走https

collection.addPattern("/home/*");

securityConstraint.addCollection(collection);

context.addConstraint(securityConstraint);

}

};

tomcatConfig.addAdditionalTomcatConnectors(this.newHttpConnector());

return tomcatConfig;

}

private Connector newHttpConnector() {

Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");

connector.setScheme("http");

connector.setPort(8080);

connector.setSecure(false);

// 如果只需要支持https访问,这里把收到的http请求跳转到https的端口

connector.setRedirectPort(8446);

return connector;

}

}

 

以上跳转也可以使用nginx实现。如果有什么问题可以评论留言或加QQ群:66728073交流

原文地址:https://www.cnblogs.com/wgslucky/p/9092128.html

时间: 2024-08-30 14:20:34

SpringBoot Web Https 配置的相关文章

腾讯云SpringBoot部署 + HTTPS配置

springboot可以打包为jar和war,jar不多说了,最近的一个工程需要打包为war发布,大致说一下吧: 先看一下项目的大致结构: 第一步,需要排除springboot自带的tomcat插件 然后,不要忘记所要打包的那个工程吧默认的jar改为war 第三步,由于我们使用了外部tomcat,所以需要加入servlet依赖: 第四步,在application的同级包下创建新的启动类: 那么最后一步,就是打包了: 打包成功之后,访问我们的域名:http://imoocdsp.com/imooc

SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例 这是一个简单的SpringBoot整合实例 这里是项目的结构目录 首先是pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="h

springboot+web文件上传和下载

一.首先安装mysql数据库,开启web服务器. 二.pom.xml文件依赖包配置如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocatio

SpringBoot使用Nacos配置中心

本文介绍SpringBoot如何使用阿里巴巴Nacos做配置中心. 1.Nacos简介 Nacos是阿里巴巴集团开源的一个易于使用的平台,专为动态服务发现,配置和服务管理而设计.它可以帮助您轻松构建云本机应用程序和微服务平台. Nacos基本上支持现在所有类型的服务,例如,Dubbo / gRPC服务,Spring Cloud RESTFul服务或Kubernetes服务. 尤其是使用Eureka注册中心的,并且担心Eureka闭源的开发者们,可以将注册中心修改为Nacos,本文主要介绍Naco

尚硅谷springboot学习23-SpringMVC配置

1. Spring MVC auto-configuration 以下是SpringBoot对SpringMVC的默认配置:(WebMvcAutoConfiguration) Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans. 自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发?重定向?)) ContentNegotiatingVi

SpringBoot的自动配置原理

一.入口 上篇注解@SpringBootApplication简单分析,说到了@SpringBootApplication注解的内部结构, 其中@EnableAutoConfiguration利用EnableAutoConfigurationImportSelector.selectImports()给容器list中导入spring-boot-autoconfigure包下的多个配置类,根据包下的META-INF/spring.factories. # Auto Configure org.sp

面试题: SpringBoot 的自动配置原理及定制starter

3.Spring Boot 的自动配置原理 package com.mmall; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] ar

SpringBoot嵌入式Servlet配置原理

SpringBoot嵌入式Servlet配置原理 SpringBoot修改服务器配置 配置文件方式方式修改,实际修改的是ServerProperties文件中的值 server.servlet.context-path=/crud server.port=8081 Java代码方式修改.通过实现WebServerFactoryCusomizer接口来获取到达ConfigurableServletWebServerFactory的通道,ConfigurableServletWebServerFac

haproxy代理https配置方法

记得在之前的一篇文章中介绍了nginx反向代理https的方法,今天这里介绍下haproxy代理https的方法: haproxy代理https有两种方式:1)haproxy服务器本身提供ssl证书,后面的web服务器走正常的http 2)haproxy服务器本身只提供代理,后面的web服务器走https(配置ssl证书) 第一种方式:haproxy服务器本身提供ssl证书 注意:需要编译haproxy的时候支持ssl编译参数: #make TARGET=linux26 USE_OPENSSL=