How can I get a list of trusted root certificates in Java?

后端 未结 2 1327
孤独总比滥情好
孤独总比滥情好 2020-11-27 05:21

I would like to be able to get access to all trusted root certificates programmatically in a Java app.

I was looking at the keystore interface, but I\'m hoping to

2条回答
  •  死守一世寂寞
    2020-11-27 05:42

    This should be more flexible using the default trust store in the system to get all certificates:

    TrustManagerFactory trustManagerFactory =
       TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    List x509Certificates = new ArrayList<>();
    trustManagerFactory.init((KeyStore)null);                 
    Arrays.asList(trustManagerFactory.getTrustManagers()).stream().forEach(t -> {
                        x509Certificates.addAll(Arrays.asList(((X509TrustManager)t).getAcceptedIssuers()));
                    });
    

    ```

提交回复
热议问题