springboot忽略证书_springboot https正式证书三步打造完成

博客介绍了在Spring Boot中配置正式证书及HTTPS的方法。先从阿里云购买并下载正式证书,放置在resource同级目录,接着在配置文件application.properties中进行配置,最后在主程序里完成https的配置,还给出了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、到阿里云购买正式证书,并进行下载,放到springboot的resource同级目录,如

image

2、配置文件进行配置application.properties

image

3、主程序进行https的配置,如下:

image

其中StartAppHttps类为:

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.CommandLineRunner;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;

import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;

import org.springframework.context.annotation.Bean;

@SpringBootApplication

@EnableAutoConfiguration

public class StartAppHttps implements CommandLineRunner {

@Bean

public EmbeddedServletContainerFactory servletContainer() {

TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {

@Override

protected void postProcessContext(Context context) {

//Due to CONFIDENTIAL and /*, this will cause Tomcat to redirect every request to HTTPS.

//You can configure multiple patterns and multiple constraints if you need more control over what is and is not redirected.

SecurityConstraint constraint = new SecurityConstraint();

constraint.setUserConstraint("CONFIDENTIAL");

SecurityCollection collection = new SecurityCollection();

collection.addPattern("/*");

constraint.addCollection(collection);

context.addConstraint(constraint);

}

};

tomcat.addAdditionalTomcatConnectors(httpConnector());

return tomcat;

}

@Bean

public Connector httpConnector() {

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

//Set the scheme that will be assigned to requests received through this connector

//@param scheme The new scheme

connector.setScheme("http");

//Set the port number on which we listen for requests.

// @param port The new port number

connector.setPort(80);

//Set the secure connection flag that will be assigned to requests received through this connector.

//@param secure The new secure connection flag

//if connector.setSecure(true),the http use the http and https use the https;else if connector.setSecure(false),the http redirect to https;

connector.setSecure(false);

//redirectPort The redirect port number (non-SSL to SSL)

connector.setRedirectPort(443);

return connector;

}

@Override

public void run(String... args) throws Exception {

// TODO Auto-generated method stub

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值