Need to trust all the certificates during the development using Spring
Asked Answered
L

1

4

I read this very useful article about disabling all the https certificates programmatically. I need such approach only in the development. And I'm using Spring. So does anybody have ideas about how can I do the same thing just in spring context files, not in the Java code? I mean this particular part of code:

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

Actually, I can simulate the first line: something like that:

<bean id="sslContext"
        class="javax.net.ssl.SSLContext"
      factory-method="getInstance">
      <constructor-arg type="java.lang.String" value="SSL" />
    </bean>

Also, I can create this trustAllCerts bean. But... I confused.. it is possible to call a some method of a bean in spring context initialization? I mean how can I call sc.init and sc.getSocketFactory in spring context file? Is it impossible or not?

Luong answered 22/7, 2011 at 11:53 Comment(0)
R
2

Yes - you can use init-method attribute. Look at Initialization callbacks. But this interface is not enough. I suggest simply placing this code

SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

In your custom class which will know that you are in dev mode or wchich will have such init-method. BTW calling getSocketFactory is simple - this is simply getting socketFactory property.

Ruel answered 22/7, 2011 at 12:47 Comment(1)
If we are constrained to use a config file, it would be nice to see how to do this in a config file with dependency injection.Knowledge

© 2022 - 2024 — McMap. All rights reserved.