gpt4 book ai didi

web-services - JAX-WS 独立服务器通过证书相互认证

转载 作者:太空宇宙 更新时间:2023-11-03 14:46:40 25 4
gpt4 key购买 nike

我有一个使用 TLS 的简单 JAX-WS 独立服务器:

SSLContext ssl = SSLContext.getInstance("TLS");

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
store.load(new FileInputStream(keystoreFile), keyPass.toCharArray());

kmf.init(store, keyPass.toCharArray());
KeyManager[] keyManagers = new KeyManager[1];
keyManagers = kmf.getKeyManagers();

TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(store);
TrustManager[] trustManagers = tmf.getTrustManagers();
ssl.init(keyManagers, trustManagers, new SecureRandom());

HttpsConfigurator configurator = new HttpsConfigurator(ssl);
HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress("localhost", 8443), 8443);
httpsServer.setHttpsConfigurator(configurator);

HttpContext context = httpsServer.createContext("/test");
httpsServer.start();

endpoint.publish(context);

我想在使用网络服务之前使用客户端证书创建相互验证。我也想看看客户端使用什么证书来读取 DN 和其他证书属性。

我该怎么做?

最佳答案

我终于让它工作了。

可以通过 SSLParameters 中的 setNeedClientAuth 配置相互身份验证,如下所示:

HttpsConfigurator configurator=new HttpsConfigurator(ssl) {
public void configure (HttpsParameters params)
{
SSLContext context;
SSLParameters sslparams;

context=getSSLContext();
sslparams=context.getDefaultSSLParameters();
sslparams.setNeedClientAuth(true);
params.setSSLParameters(sslparams);
}
};

并且可以根据需要从SSLSession中检查和解析客户端证书。此类可以作为 Web 服务类中的资源加载:

@Resource
private WebServiceContext context;

HttpsExchange exchange = (HttpsExchange) context.getMessageContext().get(JAXWSProperties.HTTP_EXCHANGE);
SSLSession sslsession = exchange.getSSLSession();

sslsession.getPeerCertificates();

关于web-services - JAX-WS 独立服务器通过证书相互认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38381689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com