这里服务端开放了简单的SOAP的API,但是想获取数据时需要双向SSL以及WS-Security签名。

其中对应的xsd文件如下:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring.io/guides/gs-producing-web-service"targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified"><xs:element name="getCountryRequest"><xs:complexType><xs:sequence><xs:element name="name" type="xs:string"/></xs:sequence></xs:complexType></xs:element><xs:element name="getCountryResponse"><xs:complexType><xs:sequence><xs:element name="country" type="tns:country"/></xs:sequence></xs:complexType></xs:element><xs:complexType name="country"><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="population" type="xs:int"/><xs:element name="capital" type="xs:string"/><xs:element name="currency" type="tns:currency"/></xs:sequence></xs:complexType><xs:simpleType name="currency"><xs:restriction base="xs:string"><xs:enumeration value="GBP"/><xs:enumeration value="EUR"/><xs:enumeration value="PLN"/></xs:restriction></xs:simpleType>
</xs:schema>

对应的Maven如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.16.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>wsdl4j</groupId><artifactId>wsdl4j</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.springframework.ws</groupId><artifactId>spring-ws-core</artifactId><version>3.0.8.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>jaxb2-maven-plugin</artifactId><version>2.5.0</version><executions><execution><id>xjc</id><goals><goal>xjc</goal></goals></execution></executions><configuration><sources><source>${project.basedir}/src/main/resources/countries.xsd</source></sources></configuration></plugin></plugins></build></project>

将源码跑起来(文章最后有源码的打包下载地址)

从中可以看到,HTTPS以及开启

这里使用SoapUI来测试,获取其wsdl

会提示如下错误

下面将证书挂上!

完成后测试下:

可以看到连接成功,获取到了wsdl

客户端的配置

客户端中配置主要在SoapConfiguration.java中,首先是配置SSL双向认证:

private static final Resource KEYSTORE_LOCATION = new ClassPathResource("client.jks");
private static final String KEYSTORE_PASSWORD = "keystore";
...@Bean
Jaxb2Marshaller marshaller() {Jaxb2Marshaller marshaller = new Jaxb2Marshaller();marshaller.setContextPath("io.spring.guides.gs_producing_web_service");return marshaller;
}@Bean
KeyStoreFactoryBean keyStore() {KeyStoreFactoryBean factoryBean = new KeyStoreFactoryBean();factoryBean.setLocation(KEYSTORE_LOCATION);factoryBean.setPassword(KEYSTORE_PASSWORD);return factoryBean;
}@Bean
TrustManagersFactoryBean trustManagers(KeyStoreFactoryBean keyStore) {TrustManagersFactoryBean factoryBean = new TrustManagersFactoryBean();factoryBean.setKeyStore(keyStore.getObject());return factoryBean;
}@Bean
HttpsUrlConnectionMessageSender messageSender(KeyStoreFactoryBean keyStore,TrustManagersFactoryBean trustManagers
) throws Exception {HttpsUrlConnectionMessageSender sender = new HttpsUrlConnectionMessageSender();KeyManagersFactoryBean keyManagersFactoryBean = new KeyManagersFactoryBean();keyManagersFactoryBean.setKeyStore(keyStore.getObject());keyManagersFactoryBean.setPassword(KEYSTORE_PASSWORD);keyManagersFactoryBean.afterPropertiesSet();sender.setKeyManagers(keyManagersFactoryBean.getObject());sender.setTrustManagers(trustManagers.getObject());return sender;
}

双向SSL认证配置同样也需要在服务端进行设置。在高度安全的环境中客户端都需要有自己的证书,在与服务器通信之前都需要验证这些证书合不合法。

客户端的keystore存放的路径为:src/mian/resources/client.jks。这个jks中包含了客户端的私钥和公钥。使用spring-ws-security配置代替HttpsUrlConnectionMessageSender,使用这种方式可以实现SOAP的双向SSL认证。

下面是配置WS-Security:

@Bean
CryptoFactoryBean cryptoFactoryBean() throws IOException {CryptoFactoryBean cryptoFactoryBean = new CryptoFactoryBean();cryptoFactoryBean.setKeyStoreLocation(KEYSTORE_LOCATION);cryptoFactoryBean.setKeyStorePassword(KEYSTORE_PASSWORD);return cryptoFactoryBean;
}@Bean
Wss4jSecurityInterceptor securityInterceptor(CryptoFactoryBean cryptoFactoryBean) throws Exception {Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();securityInterceptor.setSecurementActions("Signature");securityInterceptor.setSecurementUsername(KEY_ALIAS);securityInterceptor.setSecurementPassword(KEYSTORE_PASSWORD);securityInterceptor.setSecurementSignatureKeyIdentifier("DirectReference");securityInterceptor.setSecurementSignatureAlgorithm(WSS4JConstants.RSA_SHA1);securityInterceptor.setSecurementSignatureDigestAlgorithm(WSS4JConstants.SHA1);securityInterceptor.setSecurementSignatureCrypto(cryptoFactoryBean.getObject());return securityInterceptor;
}

这里需要为WSSJ4提供客户端的keystore及password。配置SecurityInterceptor,这个拦截器将每个SOAP消息都带上签名。

最后在GetCountryReponseClient中使用双向SSL及WS-Security:

@Bean
CountryClient countryClient(Jaxb2Marshaller marshaller,HttpsUrlConnectionMessageSender messageSender,Wss4jSecurityInterceptor securityInterceptor
) {CountryClient countryClient = new CountryClient();countryClient.setInterceptors(new ClientInterceptor[]{securityInterceptor});countryClient.setMessageSender(messageSender);countryClient.setMarshaller(marshaller);countryClient.setUnmarshaller(marshaller);return countryClient;
}

下面是测试下客户端,看看连接是否正常以及能否调用服务:

@SpringBootTest
public class CountryClientIntegrationTest {@AutowiredCountryClient countryClient;@Testvoid shouldDownloadCountry() {// givenString countryName = "Poland";// whenCountry country = countryClient.getCountry(countryName).getCountry();// thenCountry expectedCountry = new Country();expectedCountry.setName("Poland");expectedCountry.setCapital("Warsaw");expectedCountry.setCurrency(Currency.PLN);expectedCountry.setPopulation(38186860);assertThat(country.getName()).isEqualTo("Poland");assertThat(country.getCapital()).isEqualTo("Warsaw");assertThat(country.getCurrency()).isEqualTo(Currency.PLN);assertThat(country.getPopulation()).isEqualTo(38186860);}}

服务端下载地址:

https://github.com/fengfanchen/Java/tree/master/SSLWebService

客户端下载地址:

https://github.com/fengfanchen/Java/tree/master/SSLWebServiceClient

Java笔记-基于Spring Boot的SOAP双向SSL认证及WS-Security相关推荐

  1. java restful接口开发实例_实战:基于Spring Boot快速开发RESTful风格API接口

    写在前面的话 这篇文章计划是在过年期间完成的,示例代码都写好了,结果亲戚来我家做客,文章没来得及写.已经很久没有更新文章了,小伙伴们,有没有想我啊.言归正传,下面开始,今天的话题. 目标 写一套符合规 ...

  2. java quartz spring_JavaLib-quartz | 基于Spring Boot Quartz开发的定时任务

    基于Spring Boot Quartz开发的JavaLib-quartz,目的是帮你快速构建定时任务系统,你可以专心编写你的业务逻辑,而不必关注定时任务具体是如何实现的,他的性能如何,有没有异常以及 ...

  3. 第64节:Java中的Spring Boot 2.0简介笔记

    Java中的Spring Boot 2.0简介笔记 spring boot简介 依赖java8的运行环境 多模块项目 打包和运行 spring boot是由spring framework构建的,sp ...

  4. 【毕业设计】基于spring boot的图书管理系统 -java 计算机 软件工程

    文章目录 1 前言 2 系统简介 2.1 领域模型 2.2 技术栈 2.3 表结构设计 2.4 接口设计 2.4.1 接口定义 2.4.2 接口测试 2.5 权限设计 3 运行效果 3.1 系统登录 ...

  5. 【java毕业设计】基于Spring Boot+mysql的线上教学平台系统设计与实现(程序源码)-线上教学平台

    基于Spring Boot+mysql的线上教学平台系统设计与实现(程序源码+毕业论文) 大家好,今天给大家介绍基于Spring Boot+mysql的线上教学平台系统设计与实现,本论文只截取部分文章 ...

  6. 自荐Mall4j项目一个基于spring boot的Java开源商城系统

    前言 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样 ...

  7. 基于spring boot + MybatisPlus 商城管理系统的Java开源商城系统

    前言 Mall4j项目致力于为中小企业打造一个完整.易于维护的开源的电商系统,采用现阶段流行技术实现.后台管理系统包含商品管理.订单管理.运费模板.规格管理.会员管理.运营管理.内容管理.统计报表.权 ...

  8. springboot毕设项目基于Spring Boot的智慧天气管理系统84z99(java+VUE+Mybatis+Maven+Mysql)

    springboot毕设项目基于Spring Boot的智慧天气管理系统84z99(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8. ...

  9. 【java毕业设计】基于Spring Boot+mysql的酒店管理系统设计与实现(程序源码+毕业论文)-酒店管理系统

    基于Spring Boot+mysql的酒店管理系统设计与实现(程序源码+毕业论文) 大家好,今天给大家介绍基于Spring Boot+mysql的酒店管理系统设计与实现,本论文只截取部分文章重点,文 ...

最新文章

  1. 最近邻插值实现:图像任意尺寸变换
  2. java office 集成开发_Office文件格式突变,促使Java和Office更完美集成
  3. 科普 | Shell中傻傻分不清楚的TOP3
  4. Linux下简单的系统调用
  5. HDU 4383 To The Moon 解题报告
  6. 格式化文件系统命令所在目录——command not found
  7. RC952-FXE1-BL用户使用手册(存档)
  8. 深度学习2.0-31.CIFAR100与VGG13实战
  9. 2022.4.7网页一直在加载中,无提示
  10. 74HC/LS/HCT/F系列芯片的区别
  11. 神经网络——激活函数的作用
  12. 动力电池集成关键技术及电池测试与验证
  13. mc:Ignorable=“d“什么意思?
  14. matlab中电压跟踪型pwm,新型数字锁相环实现对电压信号的无差跟踪
  15. matlab程序二不能用于负数,matlab中负数的二进制码如何求取
  16. 人最大的教养,是原谅父母的不完美
  17. DTT的生活就是对吃的一种细细品味
  18. 端午送粽子祝福微信小程序源码下载支持打赏模式带背景音乐
  19. google::protobuf::Closure::Run
  20. 简单BroadcastRecevier

热门文章

  1. Oracle审计功能
  2. traceroute命令原理
  3. 如何在Linux kernel Makefile中添加宏定义
  4. Hyper-V常用问题解惑
  5. 40种网站设计常用技巧
  6. 翻译 《Why Indy?》计划进度表
  7. NOTEPAD大神是否鄙视一切IDE?
  8. 我花了十多分钟的i698源代码时间
  9. 后面的 飞鸽传书 l代表lock
  10. C++数据与我们转移过空间之后