CXF 发布WebService - 客户端调用

本文介绍了一个使用Java实现的WS客户端认证配置案例,包括密码回调类的实现、配置文件定义及客户端测试类。通过示例展示了如何设置客户端的身份验证信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 一个客户端用户名密码回调类

package org.demo.ws.client;

import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.ws.security.WSPasswordCallback;

public class ClientPasswordCallback implements CallbackHandler
{
    @Override
    public void handle(Callback[] callbacks) throws IOException,
            UnsupportedCallbackException
    {
        WSPasswordCallback pwCallback = (WSPasswordCallback)callbacks[0];
        pwCallback.setIdentifier("user1");
        pwCallback.setPassword("password1");
    }
}

2. src/ws-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd" >
    
    <bean id="clientPasswordCallback" class="org.demo.ws.client.ClientPasswordCallback" />
    <bean id="wSS4JOutInterceptor" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
        <constructor-arg>
            <map>
                <entry key="action" value="UsernameToken" />
                <entry key="passwordType" value="PasswordText" />
                <entry key="user" value="cxfClient" />
                <entry key="passwordCallbackRef">
                    <ref bean="clientPasswordCallback" />
                </entry>
            </map>
        </constructor-arg>
    </bean>
    
    <jaxws:client id="helloWorld" serviceClass="org.demo.ws.HelloWorld" 
        address="http://localhost:8080/spring/services/hello">
        <jaxws:outInterceptors>
            <ref bean="wSS4JOutInterceptor" />
        </jaxws:outInterceptors>
    </jaxws:client>
    
</beans>

3. 一个客户端测试类

package org.demo.ws.client;

import org.demo.ws.HelloWorld;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class WsClient
{
    
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("ws-client.xml");
        HelloWorld bean = (HelloWorld)ctx.getBean("helloWorld");
        String result = bean.sayHi("xiao ming");
        System.out.println("result is " + result);
    }
    
}

//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值