为spring boot项目配置HTTPS证书

package xxxxxx;

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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class XxxApplication {

    public static void main(String[] args) {
        SpringApplication.run(XxxApplication .class, args);
    }

    /**
     * http重定向到httpsuserapp
     *
     * @return
     */
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(org.apache.catalina.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(8080);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号,也就是项目配置的port
        connector.setRedirectPort(8081);
        return connector;
    }
}


applation.yml文件(关于HTTPS部分)

 	

server:
  port: 8081
  ssl:
    key-store: classpath:5718842_api1.wechat.copyctrl.cn.jks
    key-store-password: wN2KBl5l
    key-store-type: JKS

原创文章,作者:witersen,如若转载,请注明出处:https://www.witersen.com

(2)
witersen的头像witersen
上一篇 2021年5月31日 下午9:51
下一篇 2021年9月4日 下午4:09

相关推荐

发表回复

登录后才能评论