下面是“Spring Boot整合CXF并实现用户名密码校验的方法”的完整攻略:
1. 添加依赖
在 pom.xml
文件中添加 CXF 相关的依赖:
<dependencies>
<!-- CXF 依赖 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-security-saml</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wss4j</groupId>
<artifactId>wss4j-ws-security-common</artifactId>
<version>${wss4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.wss4j</groupId>
<artifactId>wss4j-ws-security-stax</artifactId>
<version>${wss4j.version}</version>
</dependency>
</dependencies>
其中,${cxf.version}
和 ${wss4j.version}
分别是 CXF 和 WSS4J 的版本号。
2. 编写服务接口和实现类
这里我们以一个简单的计算器接口为例,添加一个 CalculatorService
接口和其实现类 CalculatorServiceImpl
:
@WebService
public interface CalculatorService {
int add(int a, int b);
}
@Service
@WebService(serviceName = "CalculatorService",
portName = "CalculatorServicePort",
endpointInterface = "com.example.CalculatorService",
targetNamespace = "http://example.com/calculator")
public class CalculatorServiceImpl implements CalculatorService {
@Override
public int add(int a, int b) {
return a + b;
}
}
3. 配置 CXF
在 application.yml
文件中添加以下配置:
cxf:
path: /services
# 安全配置
security:
username-password:
users:
user: password
其中,
path
是 CXF 服务的路径,默认是/services
;security
是安全配置,这里我们启用了 CXF 默认提供的基于用户名密码的安全验证。配置具体的用户名和密码可以在users
下添加一个或多个用户。
4. 配置安全策略
在项目的 src/main/resources
目录下创建 cxf.xml
文件,配置安全策略:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://cxf.apache.org/configuration/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:http="http://cxf.apache.org/transports/http/configuration"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 安全策略配置 -->
<cxf:bus>
<cxf:inInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken"/>
<entry key="passwordType" value="PasswordDigest"/>
<entry key="passwordCallbackClass"
value="com.example.CustomCallbackHandler"/>
</map>
</constructor-arg>
</bean>
</cxf:inInterceptors>
<cxf:outInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken"/>
<entry key="user" value="user"/>
<entry key="passwordType" value="PasswordDigest"/>
<entry key="passwordCallbackClass"
value="com.example.CustomCallbackHandler"/>
</map>
</constructor-arg>
</bean>
</cxf:outInterceptors>
<sec:authorize>
<sec:policy>
<sec:usernameToken>
<sec:username/>
<sec:password type="PasswordDigest"/>
</sec:usernameToken>
</sec:policy>
</sec:authorize>
</cxf:bus>
<!-- 安全回调类 -->
<bean id="customCallbackHandler" class="com.example.CustomCallbackHandler"/>
</beans>
其中,安全策略的配置包括:
inInterceptors
:接收请求时的安全拦截器,这里使用 WSS4JInInterceptor;outInterceptors
:返回响应时的安全拦截器,这里使用 WSS4JOutInterceptor;sec:authorize
:用于验证客户端请求中的安全凭据;sec:policy
:定义安全策略。
此处使用了自定义的 CustomCallbackHandler
类,这个类需要实现 CXF 的 CallbackHandler
接口,用于在进行用户名密码验证时提供密码的明文或摘要,具体实现可以根据项目的实际情况进行。
5. 启动应用程序并测试
启动应用程序,访问 http://localhost:8080/services/CalculatorService
即可看到 CXF 服务的 WSDL 文件。使用 SOAPUI 等工具进行测试时,需要在请求报文中添加以下 SOAP 头:
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>user</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">
<xop:Include href="cid:..."/>
</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
其中,wsse:Username
和 wsse:Password
分别是客户端请求的用户名和密码,在 wsse:Password
中的 xop:Include href="cid:..."
是 SOAP 附件的内容 ID,用于取得密码的摘要值。
以上就是“Spring Boot整合CXF并实现用户名密码校验的方法”的完整攻略。下面给出两条示例:
示例1:SOAPUI测试
使用 SOAPUI 工具进行测试时,应按以下步骤进行:
- 创建 SOAPUI 项目;
- 添加一个测试用例,并在请求报文中添加以上的 SOAP 头;
- 配置请求地址为:
http://localhost:8080/services/CalculatorService
; - 配置请求方法为:
add
; - 添加
a
和b
两个参数,并设置其值; - 发送请求,查看响应结果。
示例2:Spring Boot集成测试
在 Spring Boot 应用程序中添加一个测试类,测试接口的正确性:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CalculatorServiceTests {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testAdd() throws Exception {
mockMvc.perform(post("/services/CalculatorService")
.contentType(MediaType.TEXT_XML)
.content(createAddRequestXml(1, 2)))
.andExpect(status().isOk())
.andExpect(xpath("//addResponse/result").string("3"));
}
private String createAddRequestXml(int a, int b) {
return String.format("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
"xmlns:typ=\"http://example.com/calculator/types\">\n" +
" <soapenv:Header>\n" +
" <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">\n" +
" <wsse:UsernameToken>\n" +
" <wsse:Username>user</wsse:Username>\n" +
" <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">\n" +
" <xop:Include href=\"cid:...\"/>\n" +
" </wsse:Password>\n" +
" </wsse:UsernameToken>\n" +
" </wsse:Security>\n" +
" </soapenv:Header>\n" +
" <soapenv:Body>\n" +
" <typ:add>\n" +
" <a>%d</a>\n" +
" <b>%d</b>\n" +
" </typ:add>\n" +
" </soapenv:Body>\n" +
"</soapenv:Envelope>", a, b);
}
}
以上两个示例分别是使用 SOAPUI 和 Spring Boot 进行的集成测试。其中,SOAPUI 的测试用例直接发送请求并查看响应结果,而 Spring Boot 的测试类使用了 MockMvc 进行模拟请求并验证返回结果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:springBoot整合CXF并实现用户名密码校验的方法 - Python技术站