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;
    }
}
相关推荐
五阿哥永琪1 分钟前
java8新特性 时间间隔类 Duration和Period
java
.豆鲨包2 分钟前
【Android】HttpURLConnection解析
android·java
闻哥3 分钟前
Docker Swarm 负载均衡深度解析:VIP vs DNSRR 模式详解
java·运维·jvm·docker·容器·负载均衡
panzer_maus3 分钟前
工厂模式、代理模式与单例模式的介绍
java·设计模式·代理模式
小林学编程9 分钟前
模型上下文协议(MCP)的理解
java·后端·llm·prompt·resource·tool·mcp协议
软泡芙1 小时前
【Bug】ReactiveUI WPF绑定中依赖属性不更新的问题分析与解决方案
java·bug·wpf
小程故事多_801 小时前
Harness实战指南,在Java Spring Boot项目中规范落地OpenSpec+Claude Code
java·人工智能·spring boot·架构·aigc·ai编程
浪扼飞舟1 小时前
WPF输入验证(ValidationRule)
java·javascript·wpf
砍材农夫6 小时前
spring-ai 第四多模态API
java·人工智能·spring
她说..8 小时前
Java 对象相关高频面试题
java·开发语言·spring·java-ee