2016-01-22 31 views
0

我需要創建與Spring集成HTTP站網關的URL的HTTPS連接,我下載使用Mozilla Firefox的.cer文件從URL證書,並在春天豆配置它,如圖http://www.programering.com/a/MDN4czNwATY.html建立HTTPS連接在Spring框架

對於這兩種信任存儲和密鑰存儲我使用「的changeit」密碼

運行後我得到java.io.IOException的應用程序:無效的密鑰存儲格式

起初我試圖訪問https://www.google.com

如果任何一個已經面臨這個問題,那麼請讓知道解決的辦法。

我也曾嘗試設置:

System.setProperty("javax.net.ssl.trustStore","C:\\SWDTOOLS\\jdk1.7.0_21-x64\\jre\\lib\\security\\cacerts"); 
     System.setProperty("javax.net.ssl.keyStore", "C:\\repos\\C:\repos\thawtePrimaryRootCA.crt"); 
     System.setProperty("javax.net.ssl.keyStoreType", "JKS"); 
     System.setProperty("javax.net.ssl.keyStorePassword","changeit"); 

和安裝在JVM信任存儲密鑰存儲 belowe是我已經習慣了使用https連接spring配置:

<bean id="trustStore" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
     <property name="targetObject" value="#{@systemProperties}" /> 
     <property name="targetMethod" value="putAll" /> 
     <property name="arguments"> 
      <props> 
       <prop key="javax.net.ssl.trustStore"> 
        ${key.store.truststore} 
       </prop> 
       <prop key="javax.net.ssl.keyStorePassword">${key.store.password}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="keystore" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
     <property name="targetObject" value="#{@systemProperties}" /> 
     <property name="targetMethod" value="putAll" /> 
     <property name="arguments"> 
      <props> 
       <prop key="javax.net.ssl.keyStore"> 
        ${key.store.file.thwate} 
       </prop> 
       <prop key="javax.net.ssl.keyStorePassword">${key.store.password}</prop> 
      </props> 
     </property> 
    </bean> 

這是我的stacktrace: 線程「主」中的例外

org.springframework.integration.MessageHandlingException: HTTP request execution failed for URI [https://edw.morningstar.com/login.aspx] 
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:284) 
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:98) 
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78) 
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:110) 
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97) 
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:44) 
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157) 
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128) 
    at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288) 
    at org.springframework.integration.core.MessagingTemplate.send(MessagingTemplate.java:149) 
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendMessage(AbstractReplyProducingMessageHandler.java:176) 
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.sendReplyMessage(AbstractReplyProducingMessageHandler.java:160) 
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.produceReply(AbstractReplyProducingMessageHandler.java:125) 
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleResult(AbstractReplyProducingMessageHandler.java:119) 
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:101) 
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78) 
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:110) 
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97) 
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:44) 
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157) 
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128) 
    at org.springframework.integration.core.MessagingTemplate.doSend(MessagingTemplate.java:288) 
    at org.springframework.integration.core.MessagingTemplate.doSendAndReceive(MessagingTemplate.java:318) 
    at org.springframework.integration.core.MessagingTemplate.sendAndReceive(MessagingTemplate.java:239) 
    at org.springframework.integration.gateway.MessagingGatewaySupport.doSendAndReceive(MessagingGatewaySupport.java:234) 
    at org.springframework.integration.gateway.MessagingGatewaySupport.sendAndReceiveMessage(MessagingGatewaySupport.java:208) 
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invokeGatewayMethod(GatewayProxyFactoryBean.java:300) 
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.doInvoke(GatewayProxyFactoryBean.java:269) 
    at org.springframework.integration.gateway.GatewayProxyFactoryBean.invoke(GatewayProxyFactoryBean.java:260) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at com.sun.proxy.$Proxy9.send(Unknown Source) 
    at com.amg.fds.UniverseMain.main(UniverseMain.java:91) 
Caused by: org.springframework.web.client.ResourceAccessException: I/O error: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext); nested exception is java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:453) 
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409) 
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:384) 
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:258) 
    ... 32 more 
Caused by: java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext) 
    at javax.net.ssl.DefaultSSLSocketFactory.throwException(SSLSocketFactory.java:198) 
    at javax.net.ssl.DefaultSSLSocketFactory.createSocket(SSLSocketFactory.java:205) 
    at sun.net.www.protocol.https.HttpsClient.createSocket(HttpsClient.java:409) 
    at sun.net.NetworkClient.doConnect(NetworkClient.java:162) 
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) 
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) 
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275) 
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) 
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153) 
    at org.springframework.http.client.SimpleClientHttpRequest.executeInternal(SimpleClientHttpRequest.java:73) 
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:56) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:438) 
    ... 35 more 
Caused by: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext) 
    at java.security.Provider$Service.newInstance(Provider.java:1262) 
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:236) 
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:164) 
    at javax.net.ssl.SSLContext.getInstance(SSLContext.java:156) 
    at javax.net.ssl.SSLContext.getDefault(SSLContext.java:97) 
    at javax.net.ssl.SSLSocketFactory.getDefault(SSLSocketFactory.java:121) 
    at javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory(HttpsURLConnection.java:333) 
    at javax.net.ssl.HttpsURLConnection.<init>(HttpsURLConnection.java:291) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.<init>(HttpsURLConnectionImpl.java:85) 
    at sun.net.www.protocol.https.Handler.openConnection(Handler.java:62) 
    at sun.net.www.protocol.https.Handler.openConnection(Handler.java:57) 
    at java.net.URL.openConnection(URL.java:971) 
    at org.springframework.http.client.SimpleClientHttpRequestFactory.openConnection(SimpleClientHttpRequestFactory.java:91) 
    at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:76) 
    at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:76) 
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:434) 
    ... 35 more 
Caused by: java.io.IOException: Invalid keystore format 
    at sun.security.provider.JavaKeyStore.engineLoad(JavaKeyStore.java:650) 
    at sun.security.provider.JavaKeyStore$JKS.engineLoad(JavaKeyStore.java:55) 
    at java.security.KeyStore.load(KeyStore.java:1214) 
    at sun.security.ssl.SSLContextImpl$DefaultSSLContext.getDefaultKeyManager(SSLContextImpl.java:610) 
    at sun.security.ssl.SSLContextImpl$DefaultSSLContext.<init>(SSLContextImpl.java:495) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at java.lang.Class.newInstance(Class.java:374) 
    at java.security.Provider$Service.newInstance(Provider.java:1238) 
    ... 50 more 

網址我試圖訪問:

https://edw.morningstar.com/login.aspx 

我使用firfox瀏覽器

+0

你應該告訴我們你是如何導入CER到密鑰存儲。你有使用門檻嗎? – Marged

+0

我已經更新了我如何配置了信任和密鑰庫 – focode

+1

我想這個問題'C:\\ \\回購C:\回購\ thawtePrimaryRootCA.crt'將無法正常工作。使用哪個命令將證書添加到密鑰庫? – Marged

回答

-1

最簡單的方法,你的情況是忽略證書(如果安全不是你的問題)下載證書。查看以下剪斷代碼示例:

TrustStrategy acceptingTrustStrategy = new TrustStrategy() { 
    @Override 
    public boolean isTrusted(X509Certificate[] certificate, String authType) { 
     return true; 
    } 
}; 
SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, 
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
SchemeRegistry registry = new SchemeRegistry(); 
registry.register(new Scheme("https", 8443, sf)); 
ClientConnectionManager ccm = new PoolingClientConnectionManager(registry); 

DefaultHttpClient httpClient = new DefaultHttpClient(ccm); 

String urlOverHttps = "https://localhost:8443/spring-security-rest-basic-auth/api/bars/1"; 
HttpGet getMethod = new HttpGet(urlOverHttps); 
HttpResponse response = httpClient.execute(getMethod); 

欲瞭解更多,你可以問我或者使用參考:http://www.baeldung.com/httpclient-ssl

+0

我不能忽視的證書,即不推薦按我們的客戶 – focode

+0

我downvoting這個答案,因爲它與建議潛在的嚴重安全隱患的做法,並沒有充分披露這種方式。 – Madbreaks