Springboot搭建https与wss协议

一、注意事项:
tomcat搭建https的低版本支持高版本不支持。

   <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
       
        <version>1.5.7.RELEASEversion>
        <relativePath/> 
    parent>

二、cmd到命令行生成证书。

keytool -genkey -alias tomcat  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

三、增加配置与存放证书位置。

server.port=8443
server.ssl.key-store=classpath:keystore.p12
server.ssl.key-store-password=12345678
server.ssl.keyStoreType=PKCS12
server.ssl.keyAlias=tomcat

Springboot搭建https与wss协议_第1张图片
四、在SpringApplication.run(DemoApplication.class, args);的启动方法下增加bean配置。

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.apache.tomcat.websocket.server.WsSci;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

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

    /**
     * 配置一个TomcatEmbeddedServletContainerFactory bean
     * 
     * @return
     */
    @Bean
    public EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    /**
     * 让我们的应用支持HTTP是个好想法,但是需要重定向到HTTPS,
     * 但是不能同时在application.properties中同时配置两个connector, 所以要以编程的方式配置HTTP
     * connector,然后重定向到HTTPS connector
     * 
     * @return Connector
     */
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8088);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }

    /**
     * 创建wss协议接口
     * @return
     */
    @Bean
    public TomcatContextCustomizer tomcatContextCustomizer() {
        System.out.println("TOMCATCONTEXTCUSTOMIZER INITILIZED");
        return new TomcatContextCustomizer() {
            @Override
            public void customize(Context context) {
                context.addServletContainerInitializer(new WsSci(), null);
            }

        };
    }
}

五、使用其它容器配置https证书。

Tomcat   ---> TomcatEmbeddedServletContainerFactory
Jetty    ---> JettyEmbeddedServletContainerFactory
Undertow ---> UndertowEmbeddedServletContainerFactory

六、配置一个公共的https配置。或者采用单独的更改一下方法就可以。

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;

public class CustomerServlet implements EmbeddedServletContainerCustomizer {

    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
        configurableEmbeddedServletContainer.setPort(9090); //1
        configurableEmbeddedServletContainer.setContextPath("/test"); //2
    }
} 

七、配置bean类。

@Configuration
public class ServletConfig {
    //tomcat配置
    @Bean
    public EmbeddedServletContainerFactory servletContainer(){
        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
        factory.setPort(9999);
        factory.setContextPath("/test");
        return factory;
    }
    //jetty配置
    @Bean
    public EmbeddedServletContainerFactory servletContainer(){
        JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
        factory.setPort(9999);
        factory.setContextPath("/test");
        return factory;
    }
    //Undertow配置
    @Bean
    public EmbeddedServletContainerFactory servletContainer(){
        UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
        factory.setPort(9999);
        factory.setContextPath("/test");
        return factory;
    }
}

注意:一个项目只用配置一个容器即可,不要重复注册多个。
八、修改pom.xml

         
         <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starte-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jettyartifactId>
        dependency>
      
       <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starte-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-undertowartifactId>
        dependency>
    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.7.RELEASEversion>
        <relativePath/> 
    parent>

九、生成https的两种命令。
证书分为单向证书与双向证书
单向证书命令:

1.下载安装编译好的OpenSSL
2.建议直接解压到C盘,打开cmd窗口,cd到C:\OpenSSL-Win64\bin文件夹目录,开始以下的操作
3.命令:openssl genrsa -des3 -out server.key 2048
4.这时会在C:\OpenSSL-Win64\bin目录下生成server.key文件
5.命令:openssl req -new -x509 -key server.key -out server.crt -days 3650
6.这时会在C:\OpenSSL-Win64\bin目录下生成server.crt文件
7.命令:openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt
8.提示输入server.key文件的密码
9.这时会在C:\OpenSSL-Win64\bin目录下生成server.pfx文件

单向证书在application.properties配置

server.port: 8443
server.ssl.key-store: src/main/resources/server.pfx
server.ssl.key-store-password: 12345678

双向证书命令:

keytool -genkey -alias tomcat  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

注意:生成server.pfx证书与单向证书一样。
双向证书在application.properties配置

server.port: 8443
server.ssl.key-store: src/main/resources/server.pfx
server.ssl.key-store-password: 12345678
server.ssl.trust-store=src/main/resources/keystore.p12
server.ssl.trust-store-password=12345678
server.ssl.client-auth=need

《参考:https://blog.csdn.net/ONS_cukuyo/article/details/79173651》
《参考:https://blog.csdn.net/mickjoust/article/details/51731860》
《参考:https://blog.csdn.net/maguanghui_2012/article/details/60618028》
《参考:https://blog.csdn.net/hu_344/article/details/73368774》

你可能感兴趣的:(JAVA开发,服务器运维)