添加配置类解决问题
package com.fuwu.config;
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description: https 配置
* @author: bo
* @create: 2023/5/8 16:21
* @since 1.0.0
*/
@Configuration
public class HttpsConfiguration {
@Value("${server.http.port}")
private int httpPort;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(createStandardConnector()); // 添加http
return tomcat;
}
private Connector createStandardConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http"); // 使用http协议
connector.setSecure(false); // 非安全传输
connector.setPort(httpPort); // HTTP监听端口
connector.setRedirectPort(443); // 重定向端口
return connector;
}
// public Connector getHttpConnector() {
// Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
// connector.setScheme("http"); // 使用http协议
// connector.setSecure(false); // 非安全传输
// connector.setPort(8080); // HTTP监听端口
// connector.setRedirectPort(443); // 重定向端口
// return connector;
// }
//
// @Bean
// public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
// TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
// @Override
// protected void postProcessContext(Context context) {
// SecurityConstraint securityConstraint = new SecurityConstraint();
// securityConstraint.setUserConstraint("CONFIDENTIAL"); // 设置约束
// SecurityCollection collection = new SecurityCollection();
// collection.addPattern("/*"); // 所有的路径全部进行重定向处理
// securityConstraint.addCollection(collection);
// context.addConstraint(securityConstraint);
// }
// };
// tomcat.addAdditionalTomcatConnectors(getHttpConnector()); // 添加连接器
// return tomcat;
// }
}