spring-boot內嵌三大容器https設置


spring-boot內嵌三大容器https設置

spring-boot默認的內嵌容器為tomcat,除了tomcat之前還可以設置jetty和undertow。

1.設置https

spring-boot默認http端口為8080,可以在配置文件中通過server.port來修改端口值。

server:
	port: 8080

設置https訪問只需通過增加配置信息:

server:
    port: 8080
    ssl:
        key-store: classpath:https.jks
        key-store-type: JKS
        key-store-password: 123456

不過這樣設置后http訪問不了,只能使用https訪問了。我們當然是希望能夠兼容,最好是http請求能夠自動跳轉到https。所以我們增加一個自定義的配置項http.port(因為增加了https訪問,所以server.port端口屬性被https使用,故增加http端口)

http:
	port: 80
server:
    port: 443
    ssl:
        key-store: classpath:https.jks
        key-store-type: JKS
        key-store-password: 123456

這樣配置后,我們希望無論是http://localhost還是https://localhost都能正常訪問項目,而且http://localhost還能自動跳轉到https://localhost

2.tomcat

spring-boot內嵌容器默認為tomcat,所以我們無需引用其他依賴即可使用

增加配置類

package com.github.yvanchen;

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.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Servlet;

/**
 * @author evan.chen
 * @date 2019/11/25 10:29
 */
@Configuration
public class TomcatHttpsConfig {

    @Value("${server.port}")
    protected int httpsPort;

    @Value("${http.port}")
    protected int httpPort;
    
    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                //開啟HTTP自動跳轉至HTTPS
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        Connector connector = new Connector();
        connector.setPort(httpPort);
        connector.setRedirectPort(httpsPort);
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }
}

3.jetty

需要排除默認tomcat,增加jetty

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

增加配置類

package com.github.yvanchen;

import org.eclipse.jetty.security.ConstraintMapping;
import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.webapp.AbstractConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author evan.chen
 * @date 2019/11/25 10:29
 */
@Configuration
public class JettyHttpsConfig {
    
    @Value("${server.port}")
    protected int httpsPort;

    @Value("${http.port}")
    protected int httpPort;
    
    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        JettyServletWebServerFactory jetty = new JettyServletWebServerFactory();
        jetty.addConfigurations(new AbstractConfiguration() {

            @Override
            public void configure(WebAppContext context) {
                Constraint constraint = new Constraint();
                constraint.setDataConstraint(2);

                ConstraintMapping constraintMapping = new ConstraintMapping();
                constraintMapping.setPathSpec("/*");
                constraintMapping.setConstraint(constraint);

                ConstraintSecurityHandler constraintSecurityHandler = new ConstraintSecurityHandler();
                constraintSecurityHandler.addConstraintMapping(constraintMapping);
                context.setSecurityHandler(constraintSecurityHandler);
            }
        });

        jetty.addServerCustomizers((Server server) -> {
            HttpConfiguration http = new HttpConfiguration();
            http.setSecurePort(httpsPort);
            ServerConnector connector = new ServerConnector(server);
            connector.addConnectionFactory(new HttpConnectionFactory(http));
            connector.setPort(httpPort);

            server.addConnector(connector);
        });
        return jetty;
    }
}

3.undertow

需要排除默認tomcat,增加undertow

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

增加配置類

package com.github.yvanchen;

import io.undertow.Undertow;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.SecurityInfo;
import io.undertow.servlet.api.TransportGuaranteeType;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author evan.chen
 * @date 2019/11/25 10:29
 */
@Configuration
public class UndertowHttpsConfig {
    
    @Value("${server.port}")
    protected int httpsPort;

    @Value("${http.port}")
    protected int httpPort;
    
    @Bean
    public ServletWebServerFactory servletWebServerFactory() {
        UndertowServletWebServerFactory undertow = new UndertowServletWebServerFactory();
        undertow.addBuilderCustomizers((Undertow.Builder builder) -> {
            builder.addHttpListener(httpPort, "0.0.0.0");
        });
        undertow.addDeploymentInfoCustomizers(deploymentInfo -> {
            // 開啟HTTP自動跳轉至HTTPS
            deploymentInfo.addSecurityConstraint(new SecurityConstraint()
                    .addWebResourceCollection(new WebResourceCollection().addUrlPattern("/*"))
                    .setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
                    .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
                    .setConfidentialPortManager(exchange -> httpsPort);
        });
        return undertow;
    }
}

總結

以上就是對三大內嵌容器設置https的過程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM