添加trustStore以进行客户端身份验证[重复](Add trustStore for client authentication [duplicate])

编程入门 行业动态 更新时间:2024-10-07 13:23:04
添加trustStore以进行客户端身份验证[重复](Add trustStore for client authentication [duplicate])

这个问题在这里已有答案:

SSLHandshakeException:没有共同的密码套件 2个答案

服务器和相应的客户端支持客户端身份验证,但如下所示: SSLHandshakeException:没有共同的密码套件 ,没有trustStore引用,即它们使用默认的trustStore。 如何指定trustStore?

ClassFileServer:

private static ServerSocketFactory getServerSocketFactory(String type) { if (type.equals("TLS")) { SSLServerSocketFactory ssf = null; Properties systemProps = System.getProperties(); systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks"); systemProps.put( "javax.net.ssl.trustStorePassword", "p@ssw0rd"); System.setProperties(systemProps); try { // set up key manager to do server authentication SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; char[] passphrase = "p@ssw0rd".toCharArray(); ctx = SSLContext.getInstance("TLS"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream("keystore.jks"), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); ssf = ctx.getServerSocketFactory(); return ssf; } catch (Exception e) { e.printStackTrace(); } }

SSLSocketClientWithClientAuth:

try { /* * Set up a key manager for client authentication * if asked by the server. Use the implementation's * default TrustStore and secureRandom routines. */ Properties systemProps = System.getProperties(); systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks"); systemProps.put( "javax.net.ssl.trustStorePassword", "changeit"); System.setProperties(systemProps); SSLSocketFactory factory = null; try { SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; char[] passphrase = "changeit".toCharArray(); ctx = SSLContext.getInstance("TLS"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream("keystore.jks"), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); factory = ctx.getSocketFactory(); } catch (Exception e) { throw new IOException(e.getMessage()); } SSLSocket socket = (SSLSocket)factory.createSocket(host, port);

This question already has an answer here:

SSLHandshakeException: no cipher suites in common 3 answers

A server and respective client support client authentication but as noted here: SSLHandshakeException: no cipher suites in common, do not have trustStore reference, i.e. they use the default trustStore. How can the trustStore be specified?

ClassFileServer:

private static ServerSocketFactory getServerSocketFactory(String type) { if (type.equals("TLS")) { SSLServerSocketFactory ssf = null; Properties systemProps = System.getProperties(); systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks"); systemProps.put( "javax.net.ssl.trustStorePassword", "p@ssw0rd"); System.setProperties(systemProps); try { // set up key manager to do server authentication SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; char[] passphrase = "p@ssw0rd".toCharArray(); ctx = SSLContext.getInstance("TLS"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream("keystore.jks"), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); ssf = ctx.getServerSocketFactory(); return ssf; } catch (Exception e) { e.printStackTrace(); } }

SSLSocketClientWithClientAuth:

try { /* * Set up a key manager for client authentication * if asked by the server. Use the implementation's * default TrustStore and secureRandom routines. */ Properties systemProps = System.getProperties(); systemProps.put( "javax.net.ssl.trustStore", "cacerts.jks"); systemProps.put( "javax.net.ssl.trustStorePassword", "changeit"); System.setProperties(systemProps); SSLSocketFactory factory = null; try { SSLContext ctx; KeyManagerFactory kmf; KeyStore ks; char[] passphrase = "changeit".toCharArray(); ctx = SSLContext.getInstance("TLS"); kmf = KeyManagerFactory.getInstance("SunX509"); ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream("keystore.jks"), passphrase); kmf.init(ks, passphrase); ctx.init(kmf.getKeyManagers(), null, null); factory = ctx.getSocketFactory(); } catch (Exception e) { throw new IOException(e.getMessage()); } SSLSocket socket = (SSLSocket)factory.createSocket(host, port);

最满意答案

通过设置系统属性来指定trustStore :

System.setProperty("javax.net.ssl.trustStore", "cacerts.jks");

或通过命令行调用:

-Djavax.net.ssl.keyStore=path/to/keystore.jks

Specify the trustStore by setting the system properties:

System.setProperty("javax.net.ssl.trustStore", "cacerts.jks");

or via command line invocation:

-Djavax.net.ssl.keyStore=path/to/keystore.jks

更多推荐

本文发布于:2023-08-07 18:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465266.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   客户端   trustStore   Add   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!