使用vs开发的winform访问java发布的webservice,同时为保证安全需要用户名密码验证
spring中配置如下:
<?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
<span style="white-space:pre"> </span>http://www.springframework.org/schema/beans/spring-beans.xsd
<span style="white-space:pre"> </span>http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension*.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="dailyreportServiceImpl" class="com.jereh.webservice.dailyreport.soap.DailyReportImpl"/>
<jaxws:endpoint id="dr" implementor="com.test.soap.DailyReportImpl" address="/drService">
<!--进入web service之前用户验证的拦截器-->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<!-- 设置加密类型 -->
<entry key="action" value="UsernameToken" />
<!-- 设置密码类型为明文 -->
<entry key="passwordType" value="PasswordText" />
<!--
<entry key="action" value="UsernameToken Timestamp" />
设置密码类型为加密<entry key="passwordType" value="PasswordDigest" />
-->
<entry key="passwordCallbackClass" value="com.test.interceptor.WsAuthHandler" />
</map>
</constructor-arg>
</bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
</xml>
回调函数WsAuthHandler代码如下:
public class WsAuthHandler implements CallbackHandler {
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ COME IN @@@@@@@@@@@@@@");
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
if (pc.getIdentifier().equals("myusername"))
{
pc.setPassword("mypassword");
}
}
}
vs中app.config配置如下(从网上找的没有研究):
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DrSoapBinding" maxReceivedMessageSize="106496"/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://10.202.10.109:9000/services/drService"
binding="basicHttpBinding" bindingConfiguration="DailyreportServiceSoapBinding"
contract="ServiceReference1.DrService" name="DRImplPort">
<headers>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>myusername</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">mypassword</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</headers>
</endpoint>
</client>
</system.serviceModel>
代码中调用,没有特别的:
/*调用WebService*/
ServiceReference1.DrServiceClient sr = new ServiceReference1.DrServiceClient();
ServiceReference1.WSResult wsResult = sr.syncBasicInfo();