Springboot支持 https和http请求

一 首先去下载证书

 我自己用阿里云的免费的一年

在这里插入图片描述
我下载的是 tomcat那个,下载完成有两个文件
Springboot支持 https和http请求_第1张图片
一个是证书 ,一个是密码,之后将证书放在 resource下面
Springboot支持 https和http请求_第2张图片
之后就是 配置 yml了
Springboot支持 https和http请求_第3张图片
最后 在 启动类中配置

 /**
    * @return
     */
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                collection.addMethod(DEFAULT_PROTOCOL);//没加这句话时,请求get会报错,在网上找了下 加上这句话就好了
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        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(8088);
        connector.setSecure(false);
        //监听到https的端口号
        //connector.setRedirectPort(443);
        return connector;
    }

启动:
在这里插入图片描述

测试:
https 443端口
Springboot支持 https和http请求_第4张图片

http 8088端口:
Springboot支持 https和http请求_第5张图片

你可能感兴趣的:(JAVA学习笔记)