Java – connect to the network using eclipse

I want to connect my Eclipse Plug-in to the HTTPS URL, but there is a problem because the user needs to accept the certificate Of course, there are some tutorials for how to do this in plain Java, but it may be difficult to do so in the Eclipse Plug-in. I think I will reinvent the wheel in this way

Because eclipse has some built - in tools to connect to sites with different network protocols An example is the "install new software..." operation The tool even has a preference page that lists HTTPS separately

According to eclipse help, keystore "serves as a repository for certificates used for trust decisions when making SSL connections." But I can't figure out how to use it

So my question is: how do I connect to my HTTPS site using eclipse's build tool?

Solution

Based on this answer here, I built my own plug-in and loaded only one certificate I need in its earlystartup (fortunately):

public class EarlyStartup implements IStartup {

    private static final String ALIAS = "ACME"; 

    @Override
    public void earlyStartup() {
        final char[] passphrase = "changeit".@R_502_1882@arArray();
        final char separator = File.separatorChar;
        final File dir = new File(System.getProperty("java.home") + separator + "lib" + separator + "security");
        final File file = new File(dir,"cacerts");

        try (InputStream certIn = getClass().getResourceAsStream("acme.org.crt");
                final InputStream localCertIn = new FileInputStream(file);) {

            final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(localCertIn,passphrase);
            if (keystore.containsAlias(ALIAS)) {
                return;
            }

            final CertificateFactory cf = CertificateFactory.getInstance("X.509");
            final Certificate cert = cf.generateCertificate(certIn);
            keystore.setCertificateEntry(ALIAS,cert);

            try (OutputStream out = new FileOutputStream(file)) {
                keystore.store(out,passphrase);
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>