`

cxf认证之https单向认证

    博客分类:
  • cxf
cxf 
阅读更多

cxf的https认证可以分为https单向认证和https双向认证。

 

 https单向认证 

 

服务端

 

1.制作相关的服务端证书相关命令

 

//创建私钥
openssl genrsa -out root/server/temip-key.pem 1024

 

创建证书请求
openssl req -new -out root/server/temip-req.csr -key root/server/temip-key.pem -subj /C=CN/ST=GuangDong/L=ShenZhen/O="ShanCao Technologies Co. Ltd."/OU="ShanCao EOMS System Team"/OU="Copyright (c) 1998-2018 ShanCao Technologies Co. Ltd."/CN=localhost/emailAddress=liuwuhen@huawei.com
注意cn如果是本机应该填写localhost,如果是网站则填写域名.

 

/签署服务器端证书
openssl x509 -req -in root/server/temip-req.csr -out root/server/temip-cert.pem -CA root/root-cert.pem -CAkey root/root-key.pem -CAcreateserial -days 3650

 

//将服务器端证书PKCS12格式
openssl pkcs12 -export -clcerts -in root/server/temip-cert.pem -inkey root/server/temip-key.pem -out root/server/temip-id.p12

 

通过上述几步之后服务端的证书已经制作完成。

 

 

 

 

2.在服务端添加https配置,我们只需要在tomcat中的server.xml文件中添加相关的https配置即可,详细的内容如下:

 

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" keystoreFile="conf/temip-id.p12"
      keystoreType="PKCS12" keystorePass="123456"/>

 

相关参数说明:

其中clientAuth为true代表双向认证,false表示单向认证,want表示服务端可以验证客户端,也可以不需要验证客户端。

 

 

3.运行mvn clean install -Ptomcat 通过访问https://localhost:8443/cxf-test/services/sayHello?wsdl 可以得到

 

正确的wsdl文件内容,即说明服务端的配置https已经成功。

 

客户端

 

客户端可以采用两种方式来完成https单向认证。

 

A.我们可以将生成的证书文件信息,导入到我们的jre中的证书库中。

 

    如果才用这种方式,那么需要将证书的格式转换成cer,采用命令如下:

 

keytool -import -v -trustcacerts -storepass changeit -alias temip -file root/server/temip-cert.pem -keystore root/server/temip-id.jks

 

keytool -export -alias temip -keystore root/server/temip-id.jks -file root/server/temip-id.cer -storepass changeit

 

 

将temip.cer证书导入到jre证书库的命令如下:

 

keytool -import -v -trustcacerts -storepass changeit -alias cz -file root\server\temip-id.cer -keystore

 

 D:\\jdk\\jre\\lib\\security\\cacerts

 

运行其客户端代码,即可输出say hello.

 

如果需要删除导入证书,可以采用keytool delete具体如下:

 

keytool -delete -alias sz  -keystore D:\\jdk\\jre\\lib\\security\\cacerts -storepass changeit

 

查询可以采用keytool -list

 

 keytool -list -v -keystore root\server\temip-id.p12 -storetype pkcs12 -storepass changeit

  

B.通过代码实现cxfhttps的单项认证

 

 1.将服务端生成的temip-id.cer证书导出,生成客户端keystore其命令如下:

 

   keytool -import -alias wuhen -trustcacerts -file  root/server/temip-id.cer -storepass changeit  -keystore client.keystore

 

2.编写工具代码

 

 public static TrustManager[] getTrustManagers() {
  // 读取证书仓库输入流
  InputStream is = null;
  try {
   // 信任仓库的默认算法X509
   String alg = TrustManagerFactory.getDefaultAlgorithm();
   // 获取信任仓库工厂
   TrustManagerFactory factory = TrustManagerFactory.getInstance(alg);
   // 读取信任仓库
   is = new FileInputStream(new File("F:\\client.keystore"));
   // 密钥类型
   KeyStore ks = KeyStore.getInstance("JKS");
   // 加载密钥
   ks.load(is, "123456".toCharArray());
   factory.init(ks);
   TrustManager[] tms = factory.getTrustManagers();
   return tms;
  } catch (Exception e) {
   e.printStackTrace();

  } finally {
   if (is != null) {
    try {
     is.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  return null;
 }

 

3.编写客户端测试代码

 public static void main(String[] args)

{

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  // 注册WebService接口
 factory.setServiceClass(IHelloWorld.class);

 // webservice请求地址
 String wsdlAdder = "https://localhost:8443/cxf-test/services/sayHello";
  // 发布接口
 factory.setAddress(wsdlAdder);
 IHelloWorld helloWorld = (IHelloWorld) factory.create();

Client proxy = ClientProxy.getClient(helloWorld);
  HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
  TLSClientParameters tlsParams = conduit.getTlsClientParameters();
  if (tlsParams == null) {
   tlsParams = new TLSClientParameters();
  }
  tlsParams.setSecureSocketProtocol("SSL");
  // 设置客户端证书
  tlsParams.setTrustManagers(WebServiceUtils.getTrustManagers());
  conduit.setTlsClientParameters(tlsParams);

}

 

运行客户端测试代码即可输出say hello.

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics