需要在开发期间使用Spring信任所有的证书 [英] Need to trust all the certificates during the development using Spring

查看:278
本文介绍了需要在开发期间使用Spring信任所有的证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了关于禁用所有https证书的非常实用的文章程序化。我只需要在开发中这样的方法。我使用Spring。任何人都有想法,我怎么能做同样的事情只是在春天上下文文件,而不是在Java代码?
我的意思是代码的这个特定部分:

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>

此外,我可以创建此trustAllCerts bean。
但是...我困惑..有可能在spring上下文初始化中调用一个bean的一些方法?我的意思是如何调用sc.init和sc.getSocketFactory在spring上下文文件?是否不可能?

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?

推荐答案

是的 - 您可以使用 init-method 属性。查看初始化回调。但这个接口是不够的。我建议只需放置此代码

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());

在您的自定义类中,将知道您处于开发模式或wchich将有这样的init方法。
BTW调用 getSocketFactory 很简单 - 这只是得到 socketFactory 属性。

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.

这篇关于需要在开发期间使用Spring信任所有的证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆