vs中service reference访问java cxf 发布的需要usernametoken的服务

本文介绍如何使用Visual Studio开发的WinForm应用访问Java发布的WebService,并实现基于用户名密码的安全验证。文中详细展示了Spring配置文件中关于用户验证拦截器的设置,以及VS配置文件(app.config)中的安全认证配置。

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

使用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();



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值