栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot以Jetty为容器实现http重定向到https

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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;

@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相关的知识了,有兴趣的同学还是翻看之前的文章吧。


欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章!

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

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号