在 Jetty 未按预期工作的情况下在 micronaut 上启用 HTTPS 支持

如何解决在 Jetty 未按预期工作的情况下在 micronaut 上启用 HTTPS 支持

我正在尝试为我使用 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,但您可以启用双协议处理。

事实:当我指定上述属性时:

  1. Micronaut 确实侦听端口 8443,但 curl 和浏览器会出现 SSL 协议错误。
  2. Micronaut 也会继续监听端口 8080,即使我没有指定 micronaut.server.dualProtocol=true 并且即使我明确将其设置为 false

作为参考,这是我在启动时得到的:

 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  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@2bb7bd00{SSL,(ssl,http/1.1)}{0.0.0.0:8443}
11:02:03.009 [main] INFO  o.e.jetty.server.AbstractConnector - Started ServerConnector@662f5666{HTTP/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),Server hello (2):
* TLSv1.3 (IN),Encrypted Extensions (8):
* TLSv1.3 (IN),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!

解决方法

在不验证证书的情况下尝试请求..

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 签名(使用 letencrypt 是一个很好的选择,并且也是免费的)
  • 您的证书与主机名相关联,如果主机名发生变化,您的证书也会发生变化(在大多数情况下)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res