在 Jetty 未按预期工作的情况下在 micronaut 上启用 HTTPS 支持
Posted
技术标签:
【中文标题】在 Jetty 未按预期工作的情况下在 micronaut 上启用 HTTPS 支持【英文标题】:Enabling HTTPS support on micronaut with Jetty not working as expected 【发布时间】:2021-05-28 17:34:42 【问题描述】:我正在尝试为我拥有的一个 使用 Jetty 的 micronaut 应用程序启用 HTTPS 支持。 您可以在以下位置找到该应用程序的精简版:https://github.com/galzetta/debug-micronaut-https
The documentation 表示指定以下属性就足够了:
micronaut.ssl.enabled=true
micronaut.ssl.buildSelfSigned=true
并且网络服务器应该使用新生成的自签名证书自动在端口 8443
上提供 https。
此外,the documentation 声称如果micronaut.ssl.enabled=true
则默认不会服务器 HTTP,但您可以启用双协议处理。
事实:当我指定上述属性时:
-
Micronaut 确实侦听端口
8443
,但 curl
和浏览器给出 SSL 协议错误。
即使我没有指定 micronaut.server.dualProtocol=true
并且即使我明确地将其设置为 false
,Micronaut 也会继续侦听端口 8080
作为参考,这是我在启动时得到的:
__ __ _ _
| \/ (_) ___ _ __ ___ _ __ __ _ _ _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| | | | | (__| | | (_) | | | | (_| | |_| | |_
|_| |_|_|\___|_| \___/|_| |_|\__,_|\__,_|\__|
Micronaut (v2.3.3)
11:02:02.804 [main] INFO org.eclipse.jetty.util.log - Logging initialized @510ms to org.eclipse.jetty.util.log.Slf4jLog
11:02:02.854 [main] INFO org.eclipse.jetty.server.Server - jetty-9.4.32.v20200930; built: 2020-09-30T16:16:37.804Z; git: de97d26f7bd222a0e16831e353d702a7a422f711; jvm 14.0.1+7
11:02:02.927 [main] INFO o.e.j.server.handler.ContextHandler - Started i.m.s.j.@5ebd56e9/,null,AVAILABLE
11:02:03.008 [main] INFO o.e.jetty.server.AbstractConnector - Started ServerConnector@2bb7bd00SSL, (ssl, http/1.1)0.0.0.0:8443
11:02:03.009 [main] INFO o.e.jetty.server.AbstractConnector - Started ServerConnector@662f5666HTTP/1.1, (http/1.1)0.0.0.0:8080
11:02:03.009 [main] INFO org.eclipse.jetty.server.Server - Started @716ms
11:02:03.011 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 443ms. Server Running: https://127.0.1.1:8443/
这是我尝试请求 HTTPS 应用程序时遇到的错误:
$ curl -vvv 'https://127.0.1.1:8443/verify'
* Trying 127.0.1.1:8443...
* TCP_NODELAY set
* Connected to 127.0.1.1 (127.0.1.1) port 8443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Request CERT (13):
* TLSv1.3 (IN), TLS alert, handshake failure (552):
* error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure
* Closing connection 0
curl: (35) error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure
我怀疑它可能在端口 8443 上提供 HTTP,但似乎不是:
$ curl -vvv 'http://127.0.1.1:8443/verify'
* Trying 127.0.1.1:8443...
* TCP_NODELAY set
* Connected to 127.0.1.1 (127.0.1.1) port 8443 (#0)
> GET /verify HTTP/1.1
> Host: 127.0.1.1:8443
> User-Agent: curl/7.68.0
> Accept: */*
>
* Received HTTP/0.9 when not allowed
* Closing connection 0
curl: (1) Received HTTP/0.9 when not allowed
用于测试的应用程序是用 Kotlin 编写的,并且是:
package com.example
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.runtime.Micronaut.build
fun main(args: Array<String>)
build()
.args(*args)
.packages("com.example")
.start()
@Controller
class Controller
@Get("/verify")
fun verify(): String = "Working!"
pom.xml
是(从真实的减少):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>2.3.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>server</artifactId>
<properties>
<jdk.version>11</jdk.version>
<release.version>11</release.version>
<micronaut.version>2.3.3</micronaut.version>
<exec.mainClass>com.example.ApplicationKt</exec.mainClass>
<kotlinVersion>1.4.10</kotlinVersion>
</properties>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
<repository>
<id>jcenter.bintray.com</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.micronaut.servlet</groupId>
<artifactId>micronaut-http-server-jetty</artifactId>
<exclusions>
<exclusion>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- ========================================================================== -->
<!-- Explicitly include jetty-servlet since we needed it at compile-time -->
<!-- Cause: https://github.com/micronaut-projects/micronaut-servlet/issues/114 -->
<!-- ========================================================================== -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.32.v20200930</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>$kotlinVersion</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>$kotlinVersion</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.kotlin</groupId>
<artifactId>micronaut-kotlin-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-runtime</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.kotlin</groupId>
<artifactId>micronaut-kotlin-extension-functions</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>$kotlinVersion</version>
<configuration>
<jvmTarget>$jdk.version</jvmTarget>
<compilerPlugins>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=io.micronaut.aop.Around</option>
</pluginOptions>
</configuration>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>$micronaut.version</version>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<version>$micronaut.version</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessorArgs>
<annotationProcessorArg>
micronaut.processing.group=com.example
</annotationProcessorArg>
<annotationProcessorArg>
micronaut.processing.module=server
</annotationProcessorArg>
</annotationProcessorArgs>
</configuration>
</execution>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>$kotlinVersion</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<proc>none</proc>
<source>$jdk.version</source>
<target>$jdk.version</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
谁能告诉我我做错了什么?
注意:micronaut servlet 的文档并没有说明 HTTPS,实际上它只是链接到 micronaut 的文档,这让我觉得一切都应该默认工作。见:https://micronaut-projects.github.io/micronaut-servlet/1.0.x/guide/#faq
注意:如果我使用 Netty,它确实仅在 8443 上按预期绑定工作:
__ __ _ _
| \/ (_) ___ _ __ ___ _ __ __ _ _ _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| | | | | (__| | | (_) | | | | (_| | |_| | |_
|_| |_|_|\___|_| \___/|_| |_|\__,_|\__,_|\__|
Micronaut (v2.3.3)
14:13:04.325 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 1482ms. Server Running: https://0.0.0.0:8443
控制器工作:
$ curl -k https://localhost:8443/verify
Working!
【问题讨论】:
【参考方案1】:在不验证证书的情况下尝试请求..
curl -k -vvv 'https://127.0.1.1:8443/verify'
如果可行,则意味着您的 SSL/TLS 层正在运行,但可能不是使用针对 curl 和您的浏览器识别的已知 CA 签名的证书。 (您启用micronaut.ssl.buildSelfSigned=true
的事实暗示了这种情况)
如果您已经走到了这一步,那么此时您需要关注对您来说重要的是密钥库中的证书,以及您打算如何访问服务器。
一般建议:
不要使用 IP 访问服务器(这会破坏大多数 SSL/TLS 证书验证) 不要通过 localhost 访问服务器(浏览器不支持,而且无论如何您都不能拥有“localhost”的证书) 如果您想避免浏览器上的警告和curl -k
要求,那么您需要让您的证书由浏览器和您的 curl 都识别的 CA 签名(使用letsencrypt 是一个很好的选择,也是免费)
您的证书与您的主机名相关联,如果主机名发生更改,您的证书也会如此(在大多数情况下)。
【讨论】:
curl -k
或 curl --insecure
不会更改错误。我仍然得到:curl: (35) error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure
。从错误中我相信它在发送证书之前就中断了,在 ssl/tls 协议的初始协商中以上是关于在 Jetty 未按预期工作的情况下在 micronaut 上启用 HTTPS 支持的主要内容,如果未能解决你的问题,请参考以下文章