Spring-boot 为辅助 SSL 侦听器配置 client-auth=need
Posted
技术标签:
【中文标题】Spring-boot 为辅助 SSL 侦听器配置 client-auth=need【英文标题】:Spring-boot configure client-auth=need for secondary SSL listener 【发布时间】:2018-10-07 10:48:04 【问题描述】:我使用的是 spring-boot 版本 1.5.6.RELEASE。我在 application.yml 中以声明方式在端口 9443 上配置了 SSL。这是有效的。我也在为这个 Spring-boot 应用程序使用 Undertow。
server:
session:
cookie:
http-only: true
contextPath: /webapp
port: 9443
ssl:
key-store: /etc/pki/mycert.jks
key-store-password: $SSL_KEYSTORE_PWD
keyStoreType: JKS
keyAlias: alias
我以编程方式配置了一个额外的 SSL 端口。这是一个sn-p:
@Configuration
public class UndertowAdditionalSSLConfig
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory()
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
factory.addBuilderCustomizers(new UndertowBuilderCustomizer()
@Override
public void customize(Undertow.Builder builder)
try
builder.addHttpsListener(9444, "0.0.0.0", getSSLContext());
catch (Exception e)
log.error(e,"Could not add additional listener for https");
);
return factory;
辅助 ssl 端口用于服务器之间 REST 调用的 x509 客户端身份验证。我一直无法弄清楚如何以编程方式为辅助 ssl 端口执行以下操作:
client-auth=need
我遇到的问题是客户端证书似乎没有发送或服务器不接受它。我的想法是我错过了这件作品。 感谢您的帮助。
更新
在深入研究了 Spring boot 源代码之后。我发现了这个:
builder.setSocketOption(Options.SSL_CLIENT_AUTH_MODE, SslClientAuthMode.REQUIRED);
我将更改应用到我的代码:
@Override
public void customize(Undertow.Builder builder)
try
builder.addHttpsListener(8444, "0.0.0.0", getSSLContext());
builder.setSocketOption(Options.SSL_CLIENT_AUTH_MODE, SslClientAuthMode.REQUIRED);
catch (Exception e)
log.error(e,"Could not add additional listener for https");
我以为我找到了我正在寻找的解决方案,但是更改也渗透到端口 9443 上的 SSL,并且应用程序对浏览器访问没有响应。
真的,我要问的一个更好的问题是: 如何在 2 个单独的端口上设置 SSL 并让 1 个接受客户端证书,以便进行基于客户端的身份验证。
谢谢
【问题讨论】:
【参考方案1】:您需要在特定连接器上设置 ssl,而不是在 builder
的 addHttpsListener
方法中设置 getSslContext,该方法自定义所有连接器使用的整个 sslContext
public Ssl ssl()
Ssl ssl = new Ssl();
ssl.setProtocol("TLS");
ssl.setClientAuth(Ssl.ClientAuth.valueOf("need".toUpperCase()));
// Other SSL stuff
return ssl;
// Not sure where this function is for 1.5.6 spring boot, but for 1.5.2 it is a method of the container factory which you need to override
protected void customizeConnector(Connector aConnector)
final Ssl theSsl = ssl();
// .. Other stuff to enable disable based on condition
// turn on SSL for our connector
theSsl.setEnabled(true);
this.setSsl(theSsl);
this.setPort(myConnector.getPort()); //otherwise customizeConnector will override port
【讨论】:
【参考方案2】:您应该在application.properties
文件中设置client-auth:want
,如下所示:
server:
session:
cookie:
http-only: true
contextPath: /webapp
port: 9443
ssl:
key-store: /etc/pki/mycert.jks
key-store-password: $SSL_KEYSTORE_PWD
keyStoreType: JKS
keyAlias: alias
client-auth: want
然后以编程方式打开另一个端口,如下所示:
@Configuration
public class UndertowAdditionalSSLConfig
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory()
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
factory.addBuilderCustomizers(new UndertowBuilderCustomizer()
@Override
public void customize(Undertow.Builder builder)
try
builder.addListener(new Undertow.ListenerBuilder().setPort(8444)
.setType(Undertow.ListenerType.HTTPS)
.setSslContext(getSSLContext())
.setHost("0.0.0.0")
.setOverrideSocketOptions(OptionMap.create(Options.SSL_CLIENT_AUTH_MODE, SslClientAuthMode.REQUIRED)));
catch (Exception e)
log.error(e,"Could not add additional listener for https");
);
return factory;
如果你想使用 Java lambda 表达式:
@Configuration
public class UndertowAdditionalSSLConfig
@Bean
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory()
UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
factory.addBuilderCustomizers((UndertowBuilderCustomizer) builder ->
try
builder.addListener(new Undertow.ListenerBuilder().setPort(8444)
.setType(Undertow.ListenerType.HTTPS)
.setSslContext(getSSLContext())
.setHost("0.0.0.0")
.setOverrideSocketOptions(OptionMap.create(Options.SSL_CLIENT_AUTH_MODE, SslClientAuthMode.REQUIRED)));
catch (Exception e)
log.error(e, "Could not add additional listener for https");
);
return factory;
【讨论】:
以上是关于Spring-boot 为辅助 SSL 侦听器配置 client-auth=need的主要内容,如果未能解决你的问题,请参考以下文章