客户端调用的代码如下:
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
String serviceURL = "http://localhost:8080/WebStudy/services/TestHelloService";
Service srvcModel = new ObjectServiceFactory().create(ITestHelloService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
ITestHelloService service = (ITestHelloService)factory.create(srvcModel,serviceURL);
Client client = Client.getInstance(service);
client.addOutHandler(new DOMOutHandler());
Properties properties = new Properties();
//设置认证类型为数字证书方式
properties.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
//添加用户名:
properties.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
properties.setProperty(WSHandlerConstants.USER,"admin");
properties.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordHandler.class.getName());
properties.setProperty(WSHandlerConstants.SIG_PROP_FILE, "META-INF/xfire/outsecurity.properties");
properties.setProperty(WSHandlerConstants.SIG_KEY_ID, "IssuerSerial");
client.addOutHandler(new WSS4JOutHandler(properties));
System.out.println(service.sayHello("Web Service"));
}
catch(Exception ex)
{
System.out.println(ex);
}
}服务器配置service.xml文件如下:
<service>
<name>TestHelloService</name>
<namespace>http://com.webservice/TestHelloService</namespace>
<serviceClass>com.webservice.ITestHelloService</serviceClass>
<implementationClass>com.webservice.TestHelloServiceImpl
</implementationClass>
<inHandlers>
<handler handlerClass = "org.codehaus.xfire.util.dom.DOMInHandler" />
<bean id="org.codehaus.xfire.security.wss4j.WSS4JInHandler"
class="org.codehaus.xfire.security.wss4j.WSS4JInHandler" xmlns="">
<property name="properties">
<props>
<prop key="action">Signature</prop>
<prop key="signaturePropFile">META-INF/xfire/insecurity.properties</prop>
<prop key="passwordCallbackClass">com.webservice.wssecurity.PasswordHandler</prop>
</props>
</property>
</bean>
<handler handlerClass ="com.webservice.wssecurity.ValidateUserTokenHandler"/>
</inHandlers>
</service>数字证书都放到了服务器的\src\META-INF\xfire\下面了生成数字证书的命令如下:
客户端调用的时候报错如下:
org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: WSHandler: Signature: error during message procesingorg.apache.ws.security.WSSecurityException: Signature creation failed; nested exception is:
java.security.UnrecoverableKeyException: Cannot recover key
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
String serviceURL = "http://localhost:8080/WebStudy/services/TestHelloService";
Service srvcModel = new ObjectServiceFactory().create(ITestHelloService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());
ITestHelloService service = (ITestHelloService)factory.create(srvcModel,serviceURL);
Client client = Client.getInstance(service);
client.addOutHandler(new DOMOutHandler());
Properties properties = new Properties();
//设置认证类型为数字证书方式
properties.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
//添加用户名:
properties.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
properties.setProperty(WSHandlerConstants.USER,"admin");
properties.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS, PasswordHandler.class.getName());
properties.setProperty(WSHandlerConstants.SIG_PROP_FILE, "META-INF/xfire/outsecurity.properties");
properties.setProperty(WSHandlerConstants.SIG_KEY_ID, "IssuerSerial");
client.addOutHandler(new WSS4JOutHandler(properties));
System.out.println(service.sayHello("Web Service"));
}
catch(Exception ex)
{
System.out.println(ex);
}
}服务器配置service.xml文件如下:
<service>
<name>TestHelloService</name>
<namespace>http://com.webservice/TestHelloService</namespace>
<serviceClass>com.webservice.ITestHelloService</serviceClass>
<implementationClass>com.webservice.TestHelloServiceImpl
</implementationClass>
<inHandlers>
<handler handlerClass = "org.codehaus.xfire.util.dom.DOMInHandler" />
<bean id="org.codehaus.xfire.security.wss4j.WSS4JInHandler"
class="org.codehaus.xfire.security.wss4j.WSS4JInHandler" xmlns="">
<property name="properties">
<props>
<prop key="action">Signature</prop>
<prop key="signaturePropFile">META-INF/xfire/insecurity.properties</prop>
<prop key="passwordCallbackClass">com.webservice.wssecurity.PasswordHandler</prop>
</props>
</property>
</bean>
<handler handlerClass ="com.webservice.wssecurity.ValidateUserTokenHandler"/>
</inHandlers>
</service>数字证书都放到了服务器的\src\META-INF\xfire\下面了生成数字证书的命令如下:
客户端调用的时候报错如下:
org.codehaus.xfire.XFireRuntimeException: Could not invoke service.. Nested exception is org.codehaus.xfire.fault.XFireFault: WSHandler: Signature: error during message procesingorg.apache.ws.security.WSSecurityException: Signature creation failed; nested exception is:
java.security.UnrecoverableKeyException: Cannot recover key
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货