posts - 19,  comments - 11,  trackbacks - 0

          如果用XFire 實現WS-Secuiry 在http://xfire.codehaus.org/WS-Security 

          下面說說如何寫Client 去 調用User Token Authentication 的 WS-Security.

          1.用Xfire 根據WSDL 去生成 Client stub.
          通常,用eclipse的XFire plug-in, 里面有一個Code generation from WSDL document的Wizard.生成的java文件里通常有xxxxServiceClient.java xxxxServcie.java xxxxServiceImpl.java 等文件.

          如果不考慮WS-Security Client code 很容易寫
          例如:

          xxxxServiceClient service = new xxxxxServiceClient();

          xxxxService client = service.getxxxServiceHttpPort("http://localhost:8080/services/xxxxService");


          2
          . 加WS-Security
          最好不要改XFire 生成的文件, 而是在自己的調用程序里增加.這樣將來WSDL改變, 可以在用XFire 生成Code 而不影響自己的程序

          xxxxServiceClient service = new xxxServiceClient();

          xxxxService client = service.getxxxxServiceHttpPort("http://localhost:8080/services/xxxxService");

          addWSSecurity(stallInfoClient);

          public void addWSSecurity(StallInfoService stallInfoService)
           {
               Client client = org.codehaus.xfire.client.Client.getInstance(stallInfoService);
               client.addOutHandler(new DOMOutHandler());
               Properties properties = new Properties();
                  configureOutProperties(properties);
                  WSS4JOutHandler wss4jOutHandler = new WSS4JOutHandler(properties);
                  Map props = wss4jOutHandler.getProperties();
                  PasswordHandler pwdHandler = new PasswordHandler();
                  pwdHandler.setPassword(password);
                  props.put(WSHandlerConstants.PW_CALLBACK_REF, pwdHandler);
                  wss4jOutHandler.setProperties(props);
                  client.addOutHandler(wss4jOutHandler);
           }
          protected void configureOutProperties(Properties config)
              {
                  // Action to perform : user token
                  config.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
                  // Password type : plain text
                  config.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
                  // for hashed password use:
                  //properties.setProperty(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
                  // User name to send
                  config.setProperty(WSHandlerConstants.USER, username);

              }
          public class PasswordHandler implements CallbackHandler {
                 private String password = " ";
                 public PasswordHandler() {       }
                
                 public void setPassword(String password) {
                   this.password = password;
                 }
                 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                     WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
                     String uid = pc.getIdentifer();
                     pc.setPassword(password);
                 }
          }
           
          當使用WSHandlerConstants.PW_CALLBACK_REF Xfire 有缺陷, 如果不手動把 passwordhandler 加入wss4jOutHandler的properties里, 將來是XFire無法調用自己的passwordhandler變量,  WSHandlerConstants.PW_CALLBACK_CLASS 沒有這個問題.

          通過這種方法,Client 就會在SOAP request 加入 security head.

          <soap:Envelope xmlns:soap=" <soap:Header>
          <wsse:Security soap:mustUnderstand="1" xmlns:wsse="
          http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
             <wsse:UsernameToken
          wsu:Id="UsernameToken-26235040" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
             <wsse:Username
          xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">username
             </wsse:Username>
             <wsse:Password Type="
          http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">password
             </wsse:Password>
          </wsse:UsernameToken>
          </wsse:Security>
          </soap:Header>
          <soap:Body>
          <GetRequest
          xmlns="....">11</GetRequest>
          </soap:Body>
          </soap:Envelope
          >

          posted on 2008-11-14 15:08 happyy2k 閱讀(2364) 評論(2)  編輯  收藏 所屬分類: Web Service

          FeedBack:
          # re: 編寫User Token Authentication 的 WS-Security client
          2007-05-08 18:12 | suwu
          good,正在找ws-security在xfire中的使用方面的資料,正好合用,希望能在寫多些這方面的資料!  回復  更多評論
            
          # re: 編寫User Token Authentication 的 WS-Security client
          2007-05-08 18:38 | hernry
          sever的呢?  回復  更多評論
            

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


          網站導航:
           

          <2007年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類(20)

          隨筆檔案(12)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 遂宁市| 新平| 金门县| 司法| 昌吉市| 岳阳县| 泉州市| 宁远县| 延边| 泗阳县| 龙南县| 贵溪市| 汝阳县| 临邑县| 南澳县| 卫辉市| 鹤壁市| 瓦房店市| 潢川县| 长宁区| 阿拉善盟| 木兰县| 疏勒县| 岳阳县| 鸡泽县| 甘谷县| 洛阳市| 塔河县| 晋宁县| 枝江市| 林芝县| 建昌县| 鲁山县| 崇州市| 遵化市| 彰武县| 淮北市| 石门县| 乐昌市| 商水县| 上蔡县|