首页 > 其他分享 >springboot项目同时支持http和https访问

springboot项目同时支持http和https访问

时间:2022-11-30 15:32:04浏览次数:38  
标签:http springboot boot springframework server https import org


首先使用https需要一个server.keystore,生成教程可以

然后开始改动项目:

配置文件中填入server.keystore的信息

springboot项目同时支持http和https访问_tomcat

server.ssl.key-store=server.keystore
server.ssl.key-alias=tomcat
server.ssl.enabled=true
server.ssl.key-store-password=****
server.ssl.key-store-type=****

配置文件中配置两个访问用的端口

springboot项目同时支持http和https访问_https和http_02

启动类代码设置http访问和https

package com.bootdo;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
@EnableTransactionManagement
@ServletComponentScan
@MapperScan("com.bootdo.*.dao")
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class BootdoApplication {
private static final Logger log= LoggerFactory.getLogger(BootdoApplication.class);

@Value("${http.port}")
private Integer port;

@Value("${server.port}")
private Integer httpsPort;

public static void main(String[] args) {

SpringApplication.run(BootdoApplication.class, args);
}
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
// 如果要强制使用https,请松开以下注释
// SecurityConstraint constraint = new SecurityConstraint();
// constraint.setUserConstraint("CONFIDENTIAL");
// SecurityCollection collection = new SecurityCollection();
// collection.addPattern("/*");
// constraint.addCollection(collection);
// context.addConstraint(constraint);
}
};
tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
return tomcat;
}

// 配置http
private Connector createStandardConnector() {
// 默认协议为org.apache.coyote.http11.Http11NioProtocol
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setSecure(false);
connector.setScheme("http");
connector.setPort(port);
connector.setRedirectPort(httpsPort); // 当http重定向到https时的https端口号
return connector;
}



}

这样就可以根据 http的8199端口和https的8299端口的方式访问项目了   还有一件事就是生产的server.keystore文件需要和项目部署的文件放在同一目录中!!
 


标签:http,springboot,boot,springframework,server,https,import,org
From: https://blog.51cto.com/u_15897407/5899844

相关文章

  • SpringBoot Maven RepackageMojo 打包失败原因
    maven打包提示:org/springframework/boot/maven/RepackageMojohasbeencompiledbyamorerecentversionoftheJavaRuntime(classfileversion61.0),thisver......
  • WCF调https时调用失败的问题
    最近做项目的时候,发现wcf调http可以正常,https就调用失败,但是那项目布在http,就可以正常调,布在https,就调失败了。红框位置为建立安全通道,不管是http还是https都可以正常调......
  • 前端入门 HTTP协议 HTML简介
    目录前端和后端的概念前端前戏数据交互的协议HTTP协议1.四大特性1.基于请求响应2.基于TCP、IP作用与应用层之上的协议3.无状态4.无\短连接2.数据格式3.响应状态码(statu......
  • HttpClient的三种超时
    HttpClient的3种超时说明/*从连接池中取连接的超时时间*/ConnManagerParams.setTimeout(params,1000);/*连接超时*/HttpConnectionParams.setConnectionTimeout(params......
  • SpringMVC 项目中 创建SpringBoot,使用Hibernate和JPA
    起因:老项目是SpringMVC,为了之后能使用更方便的SpringBoot。所以在其中添加了SpringBoot项目,但是老项目SpringMVC使用的Hibernate,SpringBoot希望使用JPA 解决方案:......
  • tomcat源码分析-http请求在Container中的执行路线
     在CoyoteAdapter的service方法中,主要干了2件事:  1.org.apache.coyote.Request->org.apache.catalina.connector.RequestextendsHttpServletRequest     ......
  • Tengine 主动式后端服务器健康检查功能 ngx_http_upstream_check_module
    本文使用的版本:Tengine-2.3.3在Tengine2.3.3中ngx_http_upstream_check_module默认是不包含的,所以编译配置的时候需要手动添加上去./configure--add-module=modu......
  • SpringBoot
    《静态资源导入》根据源码:静态资源放在下面的目录都可以:   ......
  • SpringBoot2 配置
    一.Properties与YamlSpringBoot支持properties与yaml两种配置文件application.properties/application.ymlyaml简单使用1.yaml简介yaml是以数据为中心的,比json,xml更适合做配......
  • 求超大文件上传方案( SpringBoot )
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上......