http - Jetty 9.2.1 将 http 重定向到 https

标签 http https jetty

谁能帮忙把代码从 jetty 8 迁移到 9.2.1。

我需要让 jetty 监听端口 80 (http) 并将每个请求重定向到 443 (https)。

这是 jetty 8 的代码,但在 9.2.1 上不起作用。版本 9 使用 ServerConnector,但我找不到有关如何使用 setConfidentialPort 属性的示例。

Server server = new Server();

//Create a connector on port 80 to listen for HTTP requests
SelectChannelConnector httpConnector = new SelectChannelConnector();
httpConnector.setPort(80);
server.addConnector(httpConnector);

//Create a connector on port 443 to listen for HTTPS requests
SslSocketConnector httpsConnector = new SslSocketConnector();
httpsConnector.setPort(443);
httpsConnector.setKeystore("name_of_the_keystore");
httpsConnector.setPassword("password_for_the_keystore");
httpsConnector.setKeyPassword("password_for_the_key");
server.addConnector(httpsConnector);

//Redirect the HTTP requests to HTTPS port
httpConnector.setConfidentialPort(443);

最佳答案

我自己也遇到过这个问题。我通过使用在 https://serverfault.com/questions/367660/how-to-have-jetty-redirect-http-to-https 找到的 web.xml 转换示例来解决这个问题进入以下内容:

基本上,您必须添加一个安全约束,强制所有路径的所有数据保密,否则会抛出 !403 错误。然后配置 http 连接器将所有 !403 错误重定向到 https:

Server server = new Server();

// HTTP Configuration
HttpConfiguration http_config = new HttpConfiguration();
http_config.addCustomizer(new SecureRequestCustomizer());

//these two settings allow !403 errors to be redirected to https
http_config.setSecureScheme("https");
http_config.setSecurePort(443);

//setup the secure config using the original http config + SecureRequestCustomizer
HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer());

// SSL Context Factory - tells how to access certificate info
SslContextFactory sslContextFactory = new SslContextFactory();
 sslContextFactory.setKeyStorePath(EmbeddedJetty.class.getResource("/keystore.jks").toExternalForm());
sslContextFactory.setKeyStorePassword("keystorepassword");
sslContextFactory.setKeyManagerPassword("keymanagerpassword");

//Create a connector on port 80 to listen for HTTP requests (that will get redirected)
ServerConnector httpConnector = new ServerConnector(server);
httpConnector.addConnectionFactory(new HttpConnectionFactory(http_config));
httpConnector.setPort(80);

//Connector on port 443 for HTTPS requests
ServerConnector sslConnector = new ServerConnector(server,
        new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.toString()),
        new HttpConnectionFactory(https_config));
sslConnector.setPort(443);

//setup the constraint that causes all http requests to return a !403 error
ConstraintSecurityHandler security = new ConstraintSecurityHandler();        

Constraint constraint = new Constraint();
constraint.setDataConstraint(Constraint.DC_CONFIDENTIAL);

//makes the constraint apply to all uri paths        
ConstraintMapping mapping = new ConstraintMapping();
mapping.setPathSpec( "/*" );
mapping.setConstraint( constraint );

security.addConstraintMapping(mapping);

//in my case I also define a ServletContextHandler for managing SpringMVC beans
//that I daisy-chain into the security handler like so:
//security.setHandler(servletContextHandler);

server.setHandler(security);
server.setConnectors(new Connector[] { httpConnector, sslConnector });

server.start();
server.join();

关于http - Jetty 9.2.1 将 http 重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24991508/

相关文章:

Node.js - Express - 类型错误 : res. json 不是函数

ssl - 是否可以从捕获的 SSL 流量中提取/生成私钥?

java - Web 应用程序中的随机 NoClassDefFound 错误

memory-leaks - Jetty 8 服务器上的内存泄漏

jakarta-ee - 您如何部署 Web 应用程序?

reactjs - 如何在发送 Content-Type multipart/form-data 的同时将图像/jpeg 文件上传到 s3 上的预签名 url?

http - 使用 C 中的简单 tcp 服务器在 linux 上通过 http 发送 MJPEG 流

java - 如何在 Google AppEngine 中设置文件下载的文件名?

CakePHP 应用程序 : some pages with SSL, 一些没有

ios - 使用经过验证的证书“发生 SSL 错误,无法与服务器建立安全连接”