boot https ssl 使用http协议访问报错

在springboot中配置ssl以后, 再次使用http访问对应的接口就会报错

可以考虑如下设置,将http访问的端口重定向到https对应的端口

java 复制代码
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatConfig {
    @Bean
    TomcatServletWebServerFactory tomcatServletWebServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(){
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        factory.addAdditionalTomcatConnectors(createTomcatConnector());
        return factory;
    }
    // 8080 -> 重定向到8843端口
    private Connector createTomcatConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
}
相关推荐
生擒小朵拉7 分钟前
STM32添加库函数
java·javascript·stm32
Z_z在努力13 分钟前
【杂类】Spring 自动装配原理
java·spring·mybatis
小小菜鸡ing40 分钟前
pymysql
java·服务器·数据库
getapi43 分钟前
shareId 的产生与传递链路
java
我没想到原来他们都是一堆坏人2 小时前
(未完待续...)如何编写一个用于构建python web项目镜像的dockerfile文件
java·前端·python
沙二原住民2 小时前
提升数据库性能的秘密武器:深入解析慢查询、连接池与Druid监控
java·数据库·oracle
Jerry&Grj2 小时前
SpringBoot埋点功能技术实现方案深度解析:架构设计、性能优化与扩展性实践
java·微服务·性能优化·springboot·架构设计·埋点技术
没有bug.的程序员2 小时前
Redis Stream:轻量级消息队列深度解析
java·数据库·chrome·redis·消息队列
用户8160791833333 小时前
告别“魔法”:包你解决 Gradle 的下载慢问题
java
当归10243 小时前
SQL Server死锁排查实战指南
java·服务器·网络