@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = 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);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
//Connector监听的http的端口号8080
connector.setPort(8080);
connector.setSecure(true); //设置为false时 跳转https
//监听到http的端口号后 会 重定向用Get方法 到的https的端口号,也就是项目配置的port
// connector.setRedirectPort(443);
return connector;
}
如果需要监听http跳转https 需将connector.setSecure(true); 设置为false,然后解开connector.setRedirectPort(443);的注释