我正在尝试将客户端证书信息从Spring Cloud Gateway转发到它背后的微服务。我修改了Netty配置,它成功地从客户端请求了客户端证书,但我没有看到它将其转发到它后面的微服务。在Apache中,我们过去常常使用+ExportCertData,它用客户端证书DN、有效时间等填充了一些头文件。Spring Cloud Gateway有像这样的开箱即用功能吗?
我发现这两个问题看起来很相似,但都没有明确的答案。spring cloud gateway forward client certificate和Does anyone have a simple example of implementing x509 mutual authentication in Spring Cloud Gateway/Spring WebFlux?
发布于 2021-08-24 12:39:52
玩了一段时间后,在Netty HttpClient上更改一些东西似乎是不正确的,因为据我所知,它不知道请求来自哪里。然而,我发现过滤器链拥有我需要的所有信息,所以我放入了一个自定义的GlobalFilter,它像Apache一样将证书信息添加到头文件中。
public class ClientSSLToHeaderFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest req = exchange.getRequest();
SslInfo info = req.getSslInfo();
if(info != null) {
X509Certificate[] certs = info.getPeerCertificates();
if(certs != null && certs.length > 0) {
ServerHttpRequest request = exchange.getRequest().mutate()
.headers(httpHeaders -> {
try {
certs[0].checkValidity();
String encodedCert = new String(Base64.getEncoder().encode(certs[0].getEncoded()));
httpHeaders.add("SSL_CLIENT_CERT", encodedCert);
httpHeaders.add("SSL_CLIENT_VERIFY", "success");
} catch(CertificateEncodingException | CertificateExpiredException
| CertificateNotYetValidException e) {
// TODO Auto-generated catch block
log.log(Level.ERROR, e, e);
}
}).build();
return chain.filter(exchange.mutate().request(request).build());
}
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return -1;
}}
https://stackoverflow.com/questions/68865665
复制相似问题