gpt4 book ai didi

spring - 如何使用 SSL 配置 Spring RestTemplate(在 Spring @MVC 中)

转载 作者:IT老高 更新时间:2023-10-28 13:50:10 24 4
gpt4 key购买 nike

我想用 SSL 配置我的 Spring @MVC stub 应用程序的 Spring RestTemplate 以与 REST 基础进行通信https 应用程序,部署在 Tomcat 服务器(Spring 3、Tomcat 7)上。到目前为止,我已经完成了 refer this link 的作品。 .现在我不知道如何将这些生成的 证书Spring RestTemplate 一起使用,任何人都可以有一些想法请帮助我。谢谢。到现在为止我做过的事情,

//Spring Security xml 配置

   <http>
<intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" requires-channel="https"/>
<http-basic/></http>

//使用 Tomcat 启用 SSL 的配置

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="C:\Users\Channa\.keystore" keystorePass="changeit"
clientAuth="false" sslProtocol="TLS"/>

用于生成 key 、证书等,

//生成客户端和服务器 key :

F:\jdk1.6.0_23\bin>keytool -genkey -keystore keystore_client -alias clientKey -dname "CN=localhost, OU=Dev, O=MyBusiness, L=Colombo, S=Westen, C=SL"< br/>F:\jdk1.6.0_23\bin>keytool -genkey -keystore keystore_server -alias serverKey -dname "CN=localhost, OU=Dev, O=MyBusiness, L=Colombo, S=Westen, C=SL"

//生成客户端和服务器证书:

F:\jdk1.6.0_23\bin>keytool -export -alias clientKey -rfc -keystore keystore_client > client.certF:\jdk1.6.0_23\bin>keytool -export -alias serverKey -rfc -keystore keystore_server > server.cert

//将证书导入对应的信任库:

F:\jdk1.6.0_23\bin>keytool -import -alias clientCert -file client.cert -keystore truststore_serverF:\jdk1.6.0_23\bin>keytool -import -alias serverCert -file server.cert -keystore truststore_client

//Spring RestTemplate 配置

<!--Http client-->
<bean id="httpClient" class="org.apache.commons.httpclient.HttpClient">
<constructor-arg ref="httpClientParams"/>
<property name="state" ref="httpState"/>
</bean>

<!--Http state-->
<bean id="httpState" class="com.org.imc.test.stub.http.CustomHttpState">
<property name="credentials" ref="usernamePasswordCredentials"/>
</bean>

<!--User name password credentials-->
<bean id="usernamePasswordCredentials" class="org.apache.commons.httpclient.UsernamePasswordCredentials"/>

<!--Http client-->
<bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory">
<constructor-arg ref="httpClient"/>
</bean>

<!--RestTemplate-->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpClientFactory"/>
</bean>

//要访问的Https URL

ResponseEntity<User> rECreateUser = restTemplate.postForEntity("https://127.0.0.1:8443/skeleton-1.0/login", user, User.class);

//目前我得到的异常:

org.springframework.web.client.ResourceAccessException:I/O 错误:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径;嵌套异常是 javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径

最佳答案

这是因为您调用的服务的 SSL 证书未由受信任的证书颁发机构签署。解决方法是将证书导入 JRE 的证书信任库 (cacerts)。

  1. 通过在浏览器中打开 URL 下载证书,点击锁浏览器地址栏中的图标。
  2. 一旦你有一个 .cer 文件,执行以下命令

    keytool -import -keystore jdk1.8.0_77/jre/lib/security/cacerts -file ~/test.cer -alias test

关于spring - 如何使用 SSL 配置 Spring RestTemplate(在 Spring @MVC 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7913942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com