<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 28,  comments - 13,  trackbacks - 0

    WebService開發筆記 1中我們創建了一個WebService簡單實例,下面我們通過一個簡單的用戶口令驗證機制來加強一下WebService的安全性:

    1.修改WebService 服務端 spring 配置文件 ws-context.xml
    <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://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName" default-lazy-init="true"> <jaxws:endpoint id="webServiceSample" address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"> <jaxws:inInterceptors> <bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" /> <bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor"> <constructor-arg> <map> <entry key="action" value="UsernameToken" /> <entry key="passwordType" value="PasswordText" /> <entry key="passwordCallbackClass" value="cn.org.coral.biz.examples.webservice.handler.WsAuthHandler" /> </map> </constructor-arg> </bean> </jaxws:inInterceptors> </jaxws:endpoint> </beans>


    2.服務端添加passwordCallbackClass回調類,該類進行用戶口令驗證:
    package cn.org.coral.biz.examples.webservice.handler;
    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 WsAuthHandler  implements CallbackHandler{
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
    if (pc.getIdentifer().equals("ws-client")){
    if (!pc.getPassword().equals("admin")) {
    throw new SecurityException("wrong password");
    }
    }else{
    throw new SecurityException("wrong username");
    }
    }
    }
    


    3.客戶端修改spring 配置文件 wsclient-context.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://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName" default-lazy-init="true"> <!-- ws clinet --> <bean id="webServiceSampleClient" class="cn.org.coral.biz.examples.webservice.WebServiceSample" factory-bean="webServiceSampleClientFactory" factory-method="create" /> <bean id="webServiceSampleClientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="cn.org.coral.biz.examples.webservice.WebServiceSample" /> <property name="address" value="http://88.148.29.54:8080/aio/services/WebServiceSample" /> <property name="outInterceptors"> <list> <bean class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor" /> <ref bean="wss4jOutConfiguration" /> </list> </property> </bean> <bean id="wss4jOutConfiguration" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor"> <property name="properties"> <map> <entry key="action" value="UsernameToken" /> <entry key="user" value="ws-client" /> <entry key="passwordType" value="PasswordText" /> <entry> <key> <value>passwordCallbackRef</value> </key> <ref bean="passwordCallback" /> </entry> </map> </property> </bean> <bean id="passwordCallback" class="cn.org.coral.biz.examples.webservice.handler.WsClinetAuthHandler"> </bean> </beans>


    4.客戶端添加passwordCallback類,通過該類設置訪問口令
    package cn.org.coral.biz.examples.webservice.handler;
    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 WsClinetAuthHandler  implements CallbackHandler{
    public void handle(Callback[] callbacks) throws IOException,
    UnsupportedCallbackException {
    for (int i = 0; i < callbacks.length; i++) {
    WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
    int usage = pc.getUsage();
    System.out.println("identifier: " + pc.getIdentifer());
    System.out.println("usage: " + pc.getUsage());
    if (usage == WSPasswordCallback.USERNAME_TOKEN) {
    // username token pwd...
    pc.setPassword("admin");
    } else if (usage == WSPasswordCallback.SIGNATURE) {
    // set the password for client's keystore.keyPassword
    pc.setPassword("keyPassword");
    }
    }
    }
    }
    


    5.junit單元測試程序:
    package cn.org.coral.biz.examples.webservice;
    import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
    import org.springframework.util.Assert;
    public class TestWebService extends AbstractDependencyInjectionSpringContextTests {
    WebServiceSample webServiceSampleClient;
    @Override
    protected String[] getConfigLocations() {
    setAutowireMode(AUTOWIRE_BY_NAME);
    return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };
    }
    /**
    * @param webServiceSampleClient the webServiceSampleClient to set
    */
    public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {
    this.webServiceSampleClient = webServiceSampleClient;
    }
    public void testSay(){
    String result = webServiceSampleClient.say(" world");
    Assert.hasText(result);
    }
    }
    

    posted on 2008-03-19 10:10 Lib 閱讀(4106) 評論(2)  編輯  收藏


    FeedBack:
    # re: WebService開發筆記 3 -- 增加WebService訪問的安全性
    2008-06-18 17:42 | ych
    拋異常了,怎么才能解決

    2008-6-18 17:42:06 org.apache.cxf.bus.spring.BusApplicationContext getConfigResources
    信息: No cxf.xml configuration file detected, relying on defaults.
    2008-6-18 17:42:09 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
    信息: Creating Service {http://spring.demo/}HelloWorldService from class demo.spring.HelloWorld
    2008-6-18 17:42:13 org.apache.cxf.phase.PhaseInterceptorChain doIntercept
    信息: Interceptor has thrown exception, unwinding now
    org.w3c.dom.DOMException: No such Localname for SOAP URI
    at org.apache.axis.message.SOAPDocumentImpl.createElementNS(SOAPDocumentImpl.java:379)
    at org.apache.axis.SOAPPart.createElementNS(SOAPPart.java:1109)
    at org.apache.cxf.staxutils.W3CDOMStreamWriter.writeStartElement(W3CDOMStreamWriter.java:98)
    at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.writeSoapEnvelopeStart(SoapOutInterceptor.java:95)
    at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:76)
    at org.apache.cxf.binding.soap.interceptor.SoapOutInterceptor.handleMessage(SoapOutInterceptor.java:57)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:221)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:276)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:222)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:73)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:177)
    at $Proxy15.sayHi(Unknown Source)
    at demo.spring.client.Client.main(Client.java:38)
    Exception in thread "main" java.lang.NoSuchMethodError: javax.xml.soap.SOAPFactory.createFault()Ljavax/xml/soap/SOAPFault;
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:193)
    at $Proxy15.sayHi(Unknown Source)
    at demo.spring.client.Client.main(Client.java:38)  回復  更多評論
      
    # re: WebService開發筆記 3 -- 增加WebService訪問的安全性
    2011-05-04 18:38 | aqq
    qqq  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2008年3月>
    2425262728291
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345



    我的JavaEye博客
    http://lib.javaeye.com


    常用鏈接

    留言簿(2)

    隨筆分類

    文章分類

    FLASH

    Java

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲成人午夜电影| 国产亚洲一区二区手机在线观看| 亚洲码一区二区三区| 免费在线看污视频| 亚洲AV无码一区二区二三区软件| 精品无码一级毛片免费视频观看| 精品亚洲一区二区三区在线观看 | 成人性做爰aaa片免费看| 狠狠亚洲婷婷综合色香五月排名| 一级成人毛片免费观看| 亚洲熟女少妇一区二区| 七色永久性tv网站免费看| 亚洲一区二区在线免费观看| 最近最好最新2019中文字幕免费| 亚洲最大的视频网站| 成人免费视频小说| 一级毛片不卡免费看老司机| 亚洲av午夜福利精品一区人妖| 91短视频在线免费观看| 国产精品亚洲午夜一区二区三区| 免费永久在线观看黄网站| 中文字幕成人免费高清在线视频 | 国产在线19禁免费观看国产| www.av在线免费观看| 亚洲图片在线观看| 免费看片A级毛片免费看| 黄页网址在线免费观看| 亚洲AV日韩AV永久无码久久| 久九九精品免费视频| 男人和女人高潮免费网站| 亚洲激情中文字幕| 四虎在线免费播放| 久久九九久精品国产免费直播| 亚洲精品亚洲人成在线麻豆| 国产成人精品免费视频软件| 免费一级毛片在线播放视频| 国产成人亚洲综合无| 亚洲伦另类中文字幕| 日本无吗免费一二区| 久久综合九色综合97免费下载| 亚洲av无码专区首页|