AXIS通过HTTPS(SSL加密)方式访问Web Service
一、简述
最近遇到这种情况,项目通过tomcat部署后,为了访问安全,加了SSL,将服务通信有http协议变更为https。但我们的客户端请求服务器的Webservice请求走的是http,这就导致请求验证过不去,下面将详细描述我们的解决处理过程。
二、解决过程
2.1 使用JDK自带的工具创建密匙库和信任库
使用keytool生成密钥库和信任库,得到如下4个文件:
(1)client.keystore
(2)client.truststore
(3)server.keystore
(4)server.truststore
命令如下所示:
set SERVER_DN="CN=Server, OU=ths, O=ths, L=BEIJING, S=BEIJING, C=CN"
set CLIENT_DN="CN=Client, OU=ths, O=ths, L=BEIJING, S=BEIJING, C=CN"
set KS_PASS=-storepass math1234
set KEYINFO=-keyalg RSA
keytool -genkey -alias Server -dname %SERVER_DN% %KS_PASS% -keystore server.keystore %KEYINFO% -keypass math1234
keytool -export -alias Server -file ths_https.cer %KS_PASS% -keystore server.keystore
keytool -import -file ths_https.cer %KS_PASS% -keystore client.truststore -alias serverkey -noprompt
keytool -genkey -alias Client -dname %CLIENT_DN% %KS_PASS% -keystore client.keystore %KEYINFO% -keypass math1234
keytool -export -alias Client -file ths_https.cer %KS_PASS% -keystore client.keystore
keytool -import -file ths_https.cer %KS_PASS% -keystore server.truststore -alias clientkey -noprompt
为了使用方便,您可以将其写成脚本,存成.bat文件。
注:密码我设置的是’math1234’,这个可以根据需求自己设定,把脚本中所有的密码的地方替换掉就可以了,其他参数(文件名称啥的)无所谓,也可以根据自己需要修改。
注:在什么位置执行该脚本,这4个文件就会生成到什么位置。
如下图所示,执行脚本文件:
得到以下四个文件:
2.2 配置tomcat
更改tomcat配置支持https,修改配置文件(server.xml)。
1、找到server.xml文件:一般在tomcat安装目录的conf文件夹中
2、修改server.xml文件:
增加或修改https connector配置:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false"
keystoreFile="conf/server.keystore" keystorePass="math1234"
truststoreFile="conf/server.truststore" truststorePass="math1234"
sslProtocol="TLS" />
说明:
1、keystoreFile、truststoreFile路径指向上一步中生成的server.keystore和server.truststore,根据自己的实际情况填写。(我这边是直接将这两个文件复制到conf目录下了)
2、端口号可根据实际情况修改
3、删除:
<Listener className="org.apache.catalina.core.AprLifecycleListener"SSLEngine=“on” />
4、重启tomcat:
进入bin目录 -> ./shutdowm.sh -> ./startup.sh
2.3 服务端 - 配置axis
修改java web工程中的/services.xml文件,增加红色标出部分
文件内容:
<service name="power/test" provider="java:RPC">
<description>处理事件</description>
<requestFlow>
<handler type="soapmonitor"/>
</requestFlow>
<responseFlow>
<handler type="soapmonitor"/>
</responseFlow>
<!-- 设置WebService对应的类名 -根据实际情况输入-->
<parameter name="ServiceClass">
qizhi.scada.axis.QizhiSpotProWebSerImpl
</parameter>
<!-- 设置WebService对应的函数名 - 根据实际情况输入 -->
<operation name="spontInfoPro">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
注:一般services.xml文件在web-inf目录下,有些工程目录是services/conf/META-INF/services.xml,不同工程可能目录不同
2.4 客户端 - 配置ssl文件路径
客户端测试程序,核心代码。不再赘述Axis如何发布和调用Webservice,需要的请看链接:
public class Test
{
public static void main(String [] args) throws Exception {
//这里是https
String endpoint = "https://localhost:" +"8443"+ "/power/test";
//client.keystore和client.truststore路径根据自己情况填写
String pathStart = System.getProperty("user.dir")+File.separator+"ssl"+File.separator;
System.out.println(pathStart);
System.setProperty("javax.net.ssl.keyStore",pathStart+"client.keystore");
System.setProperty("javax.net.ssl.keyStorePassword", "math1234");
System.setProperty("javax.net.ssl.trustStore",pathStart+"client.truststore");
System.setProperty("javax.net.ssl.trustStorePassword", "math1234");
//关键是上面几行代码,下面是调用Webservice常规步骤
.......
//后面的涉及到业务,这里删除了
}
}