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;
    }
}
相关推荐
Black蜡笔小新6 分钟前
国标GB28181视频监控平台EasyCVR赋能平安乡村建设,构筑乡村治理“数字防线”
java·网络·音视频
蚰蜒螟9 分钟前
从 pthread_create 到 thread_native_entry:glibc 如何唤醒 Java 线程
java·开发语言
callJJ25 分钟前
JVM 类加载机制详解——从 .class 文件到对象诞生的完整旅程
java·jvm·类加载·双亲委派模型
Kiling_070438 分钟前
Java Math类核心用法全解析
java·开发语言
踏着七彩祥云的小丑38 分钟前
开发中用到的注解
java
小梦爱安全41 分钟前
Ansible剧本1
java·网络·ansible
pupudawang1 小时前
Spring Boot 热部署
java·spring boot·后端
我登哥MVP1 小时前
【SpringMVC笔记】 - 9 - 异常处理器
java·spring boot·spring·servlet·tomcat·maven
下地种菜小叶1 小时前
Spring Boot 2.x 升级 3.x / 4.x 怎么做?一次讲清 JDK、Jakarta、依赖兼容与上线策略
java·spring boot·后端
iiiiyu1 小时前
常用API(StringJoiner类 & Math类 & System类)
java·大数据·开发语言·数据结构·编程语言