Using Eclipse to Connect to a Network

I want to connect the Eclipse plug-in to the HTTPS URL, but there is a problem because the user will need to accept the certificate. Of course, there are some tutorials on how to do this in simple Java , but it can be difficult to do inside the Eclipse plug-in, and I think I invented the wheel this way.

Because Eclipse has built-in tools for connecting to sites with different network protocols. An example is the action "Install new software ...". The toolkit even has a page that lists HTTPS separately.

According to the Eclipse Help , KeyStore is used as a repository for certificates used to make trust decisions [...] when creating an SSL connection. "But I could not figure out how to use it.

So my question is: how can I use the Eclipse assembly to connect to my HTTPS site?

+4
source share
1 answer

Based on this answer here I am creating my own plugin that uploads only one certificate that I need (I'm lucky) in EarlyStartup:

public class EarlyStartup implements IStartup {

    private static final String ALIAS = "ACME"; 

    @Override
    public void earlyStartup() {
        final char[] passphrase = "changeit".toCharArray();
        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();
        }
    }
}
0
source

Source: https://habr.com/ru/post/1653761/


All Articles