【发布时间】:2018-08-23 07:12:51
【问题描述】:
我有以下代码来配置 Jetty 服务器:
@Configuration
public class RedirectHttpToHttpsOnJetty2Config {
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
JettyServletWebServerFactory factory = new JettyServletWebServerFactory();
factory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(Server server) {
ServerConnector connector = new ServerConnector(server);
connector.setPort(80);
server.addConnector(connector);
}
});
return factory;
}
}
和
application.properties 为
server.port=8443
server.ssl.key-store=classpath:keystore
server.ssl.key-store-password=xyzxyzxyz
server.ssl.key-password=xyzxyzxyz
当我访问 localhost:8443 但无法访问 localhost:80 时,我的应用程序可以正常工作。 gradlew bootRun 提到
... Jetty 在端口 8443 (ssl, http/1.1)、80 (http/1.1) 上启动,上下文路径为“/” ...
但在访问http://localhost:80 时,我收到了消息
无法访问此站点... localhost 拒绝连接。
我正在寻找 http://localhost:80 被重定向到 https://localhost:8443。
我让它在 Tomcat 中工作:
@Bean
public ServletWebServerFactory servletContainer(){
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(redirectConnector());
return tomcat;
}
private Connector redirectConnector(){
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
但无法找到 Jetty 的等价物。任何指针都非常感谢。
【问题讨论】:
-
您在您的
ServerConnector上缺少一个HttpConfiguration,该ServerConnector需要存在以至少识别什么是端口(安全与非安全)。
标签: spring-boot embedded-jetty http-redirect