设为首页 收藏本站
查看: 328|回复: 0

[经验分享] Setup Tomcat SSL and Spring RESTful webservice Client

[复制链接]

尚未签到

发表于 2017-2-5 07:22:43 | 显示全部楼层 |阅读模式
1. Generate a keystore using Java command : keytool 

 
    keytool -v -genkey -alias tomcat -keyalg RSA -keystore C:/test/tomcat.keystore -validity 36500
    For keytool usage , can refer to othere google search

Attention : 
   a. when prompted for password(e.g. changeit), please enter the same as you input in tomcat server.xml
   b. When prompted for " What is your first name and last name ?"  you should input the root web address :
    For example , your explored web address is   https://xxxx.yyy.zzz:8443/ , then the first name and last name should be xxx.yyy.zzz . otherwise , the client will fail to authorized  with exception like below :
Caused by: javax.net.ssl.SSLException: hostname in certificate didn't match: <xxx.yyy.zzz> != <your input for first and last name>
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:228)
at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:149)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:130)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:151)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:641)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(HttpComponentsClientHttpRequest.java:88)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:46)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:49)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)

 
2. Modify tomcat server.xml like below :
 TOMCAT/conf/server.xml 
 
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" keystoreFile="
C:/test/tomcat.keystore"
               keystorePass="changeit" sslProtocol="TLS" />

 
3. Start the Tomcat
you must restart tomcat everytime when you changed the keystore . so that the new keystore can take effect .
 
4.1 Spring restTemplate setting in server side :

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
default-merge="false"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http-2.2.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<int-http:inbound-gateway id="soa"
request-channel="receiveRequestChannel" reply-channel="responseCallBackChannel"
supported-methods="POST" path="/fcrsp/{indicator}" error-channel="logger"
request-payload-type="java.lang.String" >
<int-http:header name="indicator" expression="#pathVariables.indicator"/>
</int-http:inbound-gateway>
<int:channel id="receiveRequestChannel"></int:channel>
<int:service-activator input-channel="receiveRequestChannel" output-channel="responseCallBackChannel"
expression="@myWebService.callStoreProcedure(headers.get('indicator'),payload)">
</int:service-activator>
<int:channel id="responseCallBackChannel"></int:channel>
</beans>



 
 
4.2 Spring restTemplate setting in client side :
 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">

<!-- Username and Password Credentials to access restful service -->
<bean id="credentials" class="org.apache.http.auth.UsernamePasswordCredentials">
<constructor-arg>
<value>soauser</value>
</constructor-arg>
<constructor-arg>
<value>soapassword</value>
</constructor-arg>
</bean>
<bean id="httpClientFactory" class="com.hsbc.gbm.finit.lta.fcr.entity.FCRHttpComponentsClientHttpRequestFactory">
<constructor-arg ref="credentials" />
</bean>
<!-- Rest template -->
<bean id="restTemplate" name="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="httpClientFactory" />
</bean>
<bean id="fcrWebserviceClient" class="mypackage.myWebserviceClient">
<property name="restTemplate" ref="restTemplate" />
<property name="requestUrl" value="https://xxx.yyy.zzz:8443/FCR-Webservice-orchestration/rest/fcrsp/getHierarchy" />
<property name="compressed" value="false" />
</bean>
</beans>



 
  FCRHttpComponentsClientHttpRequestFactory.java
set the keystore value 
 

public class FCRHttpComponentsClientHttpRequestFactory extends
HttpComponentsClientHttpRequestFactory {
public FCRHttpComponentsClientHttpRequestFactory(){
super();
}
public FCRHttpComponentsClientHttpRequestFactory(Credentials credentials) throws Exception{
super();
DefaultHttpClient httpClient = (DefaultHttpClient) this.getHttpClient();
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,credentials);
httpClient.addRequestInterceptor(new FCRHttpRequestInterceptor(), 0);

KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File("c:/test/tomcat.keystore"));
try {
trustStore.load(instream, "changeit".toCharArray());
} finally {
try { instream.close(); } catch (Exception ignore) {}
}
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme scheme = new Scheme("https", 8443, socketFactory);
httpClient.getConnectionManager().getSchemeRegistry().register(scheme);
this.setHttpClient(httpClient);
this.setConnectTimeout(999999999);
}
}

 

public class FCRHttpRequestInterceptor implements HttpRequestInterceptor {
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
if (authState.getAuthScheme() == null) {
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()));
if (creds == null) {
throw new HttpException("No credentials for preemptive authentication");
}
authState.update(new BasicScheme(), creds);
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-337551-1-1.html 上篇帖子: Tomcat配置任意目录下的Web应用程序 下篇帖子: Howto: 在Tomcat上如何集成Pentaho和Liferay
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表