在我的春季启动项目中,我添加https支持,当我curl -I http://127.0.0.1:80
,我得到HTTP/1.1 302
时,如何使用HTTP 301重定向。
这是我的配置,在我的代码中有一些评论可能有用。
有人可以帮帮我吗?非常感谢!
@Configuration
public class WebConfig {
@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
TomcatEmbeddedServletContainerFactory factory =
new TomcatEmbeddedServletContainerFactory() {
@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);
}
};
factory.addAdditionalTomcatConnectors(createHttpConnector());
return factory;
}
private Connector createHttpConnector() {
//org.apache.catalina.connector.Connector,in this source code i saw a method named createResponse(),
//this method return an org.apache.catalina.connector.Response,
//in this response has a method sendRedirect(),there is a constant set httpstatus 302
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setSecure(false);
connector.setPort(80);
connector.setRedirectPort(443);
return connector;
}
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return container -> {
Ssl ssl = new Ssl();
ssl.setKeyStore("classpath:***.jks");
ssl.setKeyStorePassword("***");
container.setSsl(ssl);
container.setPort(443);
};
}
}