springboot配置https

首先:

1、购买域名,备案,阿里云/腾讯云,备案完成,申请免费域名

2、下载SSL证书,下载TomcatSSL的证书,jks格式、其他格式都可以,将jks的文件放到resources目录下,默认文件名为:域名.jks。
在这里插入图片描述
在这里插入图片描述

3、在SpringBoot中的application.yml配置文件中加入

server:
  port: 8686                 #https端口
  http:
    port: 8086              #http端口
  ssl:
    key-store: classpath:chengdashi.cn.jks     
    key-store-password: 80s1231jzr              #压缩包解压里面会有
    key-store-type: JKS
    enabled: true

写法一:在启动类中加入

@Bean
public TomcatServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            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");
    connector.setScheme("http");
    //Connector监听的http的端口号
    connector.setPort(8086);
    connector.setSecure(false);
    //监听到http的端口号后转向到的https的端口号
    connector.setRedirectPort(8686);
    return connector;
}

写法二:另建一个配置类,加上@Configuration注解

@Configuration
public class TomcatConfig {
    @Bean
    TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(){
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        factory.addAdditionalTomcatConnectors(createTomcatConnector());
        return factory;
    }

    private Connector createTomcatConnector() {
       Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
	    connector.setScheme("http");
	    //Connector监听的http的端口号
	    connector.setPort(8086);
	    connector.setSecure(false);
	    //监听到http的端口号后转向到的https的端口号
	    connector.setRedirectPort(8686);
	    return connector;
    }
}

通过域名访问失败原因及解决办法

域名未配置解析,去域名管理配置解析,10分钟后通过ping 域名看是否显示ip,显示则解析成功!
域名未认证,去域名管理上传个人信息进行域名实名!
域名已实名但未网站未备案,解决办法
去进行网站实名,使用腾讯云小程序认证进行icp备案即可!
可以通过域名访问但必须加上自定义端口才行,不能使用8080或443等默认端口,否则提示连接已重置!

如果配置SSL证书和配置之后报错,Tomcat启动报错:什么内嵌的Tomcat服务器启动错误,则的pom.xml中加入

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-tomcatartifactId>
    <scope>compilescope>
dependency>

你可能感兴趣的:(java,spring,boot,https,http)