2019 年 8 月更新:适用于 Jetty 9.4.20.v20190813 版本。
证书由 HttpConfiguration Customizer 添加到 Request 对象(如 HttpServletRequest)。
具体来说,SecureRequestCustomizer。
您使用它的代码将如下所示(向下滚动)...
Server server = new Server();
// === HTTP Configuration ===
HttpConfiguration http_config = new HttpConfiguration();
http_config.setSecureScheme("https");
http_config.setSecurePort(8443);
http_config.setOutputBufferSize(32768);
http_config.setRequestHeaderSize(8192);
http_config.setResponseHeaderSize(8192);
http_config.setSendServerVersion(true);
http_config.setSendDateHeader(false);
// === Add HTTP Connector ===
ServerConnector http = new ServerConnector(server,
new HttpConnectionFactory(http_config));
http.setPort(8080);
http.setIdleTimeout(30000);
server.addConnector(http);
// === Configure SSL KeyStore, TrustStore, and Ciphers ===
SslContextFactory sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath("/path/to/keystore");
sslContextFactory.setKeyStorePassword("changeme");
sslContextFactory.setKeyManagerPassword("changeme");
sslContextFactory.setTrustStorePath("/path/to/truststore");
sslContextFactory.setTrustStorePassword("changeme");
// OPTIONAL - for client certificate auth (both are not needed)
// sslContextFactory.getWantClientAuth(true)
// sslContextFactory.setNeedClientAuth(true)
// === SSL HTTP Configuration ===
HttpConfiguration https_config = new HttpConfiguration(http_config);
https_config.addCustomizer(new SecureRequestCustomizer()); // <-- HERE
// == Add SSL Connector ===
ServerConnector sslConnector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory,"http/1.1"),
new HttpConnectionFactory(https_config));
sslConnector.setPort(8443);
server.addConnector(sslConnector);
使用此 SecureRequestCustomizer,您可以使用以下属性名称从调用中访问有关 SSL 连接的各个部分。HttpServletRequest.getAttribute(String)
javax.servlet.request.X509Certificate
java.security.cert.X509Certificate
的数组[]
javax.servlet.request.cipher_suite
密码套件的字符串名称。(与从 javax.net.ssl.SSLSession.getCipherSuite()
返回的内容相同)
javax.servlet.request.key_size
正在使用的密钥长度的整数
javax.servlet.request.ssl_session_id
活动 SSL 会话 ID 的字符串表示形式(十六进制)