天天看點

Springboot以Jetty為容器實作http重定向到https

Springboot以Jetty為容器實作http重定向到https

1 簡介

之前講解的Springboot整合https用的是tomcat作為容器,tomcat也是一個流行多年的老牌Java容器了。但針對不同的場景,還是會有不同的選擇,如Jetty。Jetty是架構相對簡單、基于Handler的靈活可擴充的Servlet容器。更多詳情請參考官方文檔。

另外建議閱讀其它相關文章:

(1)Springboot整合https原來這麼簡單

(2)HTTPS之密鑰知識與密鑰工具Keytool和Keystore-Explorer

(3)Springboot以Tomcat為容器實作http重定向到https的兩種方式

2 重定向實作

為了代碼結構清晰一點,把配置拆成兩個類。

2.1 重定向

HttpToHttpsJettyConfig是與Jetty強相關的配置類,繼承于AbstractConfiguration,以便後續用于WebServerFactory的設定,如果沒有這個類的配置,那就會同時具有http和https服務,無法重定向。這個類的配置要求連接配接必須是安全的。具體代碼如下:

package com.pkslow.ssl.config;

import org.eclipse.jetty.security.ConstraintMapping;

import org.eclipse.jetty.security.ConstraintSecurityHandler;

import org.eclipse.jetty.util.security.Constraint;

import org.eclipse.jetty.webapp.AbstractConfiguration;

import org.eclipse.jetty.webapp.WebAppContext;

public class HttpToHttpsJettyConfig extends AbstractConfiguration {

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

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

    ConstraintSecurityHandler handler = new ConstraintSecurityHandler();
    handler.addConstraintMapping(mapping);

    context.setSecurityHandler(handler);
}           

}

2.2 同時打開http和https

WebServerFactoryCustomizerConfig的功能主要是在有https的前提下,還要提供http,具體代碼如下:

import org.eclipse.jetty.server.*;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;

import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;

import org.springframework.boot.web.server.WebServerFactoryCustomizer;

import org.springframework.context.annotation.Configuration;

import java.util.Collections;

@Configuration

public class WebServerFactoryCustomizerConfig implements WebServerFactoryCustomizer {

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

@Value("${http.port}")
private int httpPort;
           
@Override
public void customize(ConfigurableJettyWebServerFactory factory) {
    ((JettyServletWebServerFactory)factory).setConfigurations(
            Collections.singleton(new HttpToHttpsJettyConfig())
    );

    factory.addServerCustomizers(
            server -> {
                HttpConfiguration httpConfiguration = new HttpConfiguration();
                httpConfiguration.setSecurePort(httpsPort);
                httpConfiguration.setSecureScheme("https");

                ServerConnector connector = new ServerConnector(server);
                connector.addConnectionFactory(new HttpConnectionFactory(httpConfiguration));
                connector.setPort(httpPort);

                server.addConnector(connector);
            }
    );
}           

實作的重定向的結果如下:

2.3 更好玩的多http端口

有意思的是,我們可以實作多個http端口同時啟用,并都重定向到https,增加代碼如下即可:

ServerConnector connector2 = new ServerConnector(server);

connector2.addConnectionFactory(new HttpConnectionFactory(httpConfiguration));

connector2.setPort(httpPort + 1);

server.addConnector(connector2);

效果如下,使用80和81端口都可以實作重定向:

3 總結

本文沒有太多的原理可講,之前的文章已經講了不少https相關的知識了,有興趣的同學還是翻看之前的文章吧。

本文詳細代碼可在南瓜慢說公衆号回複擷取。

原文位址

https://www.cnblogs.com/larrydpk/p/12813908.html