java 重定向_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的两种方式

2ed40996e15498a8a5c4aad4605a236d.png

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,具体代码如下:

package com.pkslow.ssl.config;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;@Configurationpublic 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);                }        );    }}

实现的重定向的结果如下:

2af451d8da96186c5691fdd1cdfa9f29.gif

2.3 更好玩的多http端口

有意思的是,我们可以实现多个http端口同时启用,并都重定向到https,增加代码如下即可:

ServerConnector connector2 = new ServerConnector(server);connector2.addConnectionFactory(new HttpConnectionFactory(httpConfiguration));connector2.setPort(httpPort + 1);server.addConnector(connector2);

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

890def3a386dbd9ab7a30d0bdbe1f4d8.gif

3 总结

本文没有太多的原理可讲,之前的文章已经讲了不少https相关的知识了,有兴趣的同学还是翻看之前的文章吧。

多读书,多分享;多写作,多整理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值