有没有人有一个简单的例子在Spring Cloud Gateway/Spring WebFlux中实现x509相互身份验



我正在尝试在Spring Cloud Gateway中实现相互身份验证(使用x509客户端证书进行身份验证(,但是在整个研究中,我一直无法弄清楚从哪里开始。

据我所知,身份验证不是通过Spring Cloud Gateway本身处理的,而是应该通过Spring WebFlux完成的 - 如果这是一个不正确的假设,请纠正我。我已经找到了通过Spring Security实现认证身份验证的示例,但是我没有找到WebFlux的任何示例。

任何人都可以提供一些技巧甚至一些代码示例来让我走上正确的轨道吗?

您可以在src/main/resources/application.yml中配置它,例如

server:
# for testing or development without SSL certs (HTTP) use an "appropriate"
# non-secure port, e.g. 8080
# for HTTPS use an "appropriate" secure port, e.g. 8443
port: 8443
ssl:
# for HTTP set enabled to false, for HTTPS (with required client certs) set to true
enabled: true
# this is the spring cloud gateway _server_ cert
key-store: /etc/pki/tls/private/server.p12
key-store-password: servercertpassword
key-store-type: PKCS12
# this is the "bundle" of CA intermediate/root upon which the client cert has to
# match
trust-store: /etc/pki/ca-trust/extracted/java/cacerts
# this `client-auth` option is where you *require* mutual-TLS, it can alternatively
# be made optional
client-auth: need
trust-store-password: truststorepassword
trust-store-type: JCEKS
management:
# management port without SSL to allow monitoring/etc. without client certs
# e.g. /actuator/health
server:
port: 8080
ssl:
enabled: false

如果您有一组客户端证书、服务器证书和信任库/CA 捆绑包,这是如何在 Spring 云网关中配置它的示例。

应用程序中将提供 X509PreAuthenticatedAuthenticationToken,用于通过相互 TLS 成功建立连接,其中包含客户端证书的主体/详细信息。

你对使用 webflux 的网关是正确的,因为没有标准的身份验证机制,如 Spring mvc。 建议的身份验证方法如下: https://docs.spring.io/spring-security/site/docs/5.2.5.RELEASE/reference/html/reactive-x509.html

我还发现这里的代码示例有助于设置 application.yml 文件。 https://github.com/spring-projects/spring-security-samples/tree/main/reactive/webflux/java/authentication/x509

这篇文章也可能有所帮助: 通过 WebFlux 的证书进行身份验证?

这篇文章不是关于webflux的,但有助于理解如何设置证书: https://www.baeldung.com/x-509-authentication-in-spring-security

最新更新