posts - 19,  comments - 11,  trackbacks - 0

          如果用XFire 實(shí)現(xiàn)WS-Secuiry 在http://xfire.codehaus.org/WS-Security 

          下面說說如何寫Client 去 調(diào)用User Token Authentication 的 WS-Security.

          1.用Xfire 根據(jù)WSDL 去生成 Client stub.
          通常,用eclipse的XFire plug-in, 里面有一個(gè)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 生成的文件, 而是在自己的調(diào)用程序里增加.這樣將來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);
                 }
          }
           
          當(dāng)使用WSHandlerConstants.PW_CALLBACK_REF Xfire 有缺陷, 如果不手動(dòng)把 passwordhandler 加入wss4jOutHandler的properties里, 將來是XFire無(wú)法調(diào)用自己的passwordhandler變量,  WSHandlerConstants.PW_CALLBACK_CLASS 沒有這個(gè)問題.

          通過這種方法,Client 就會(huì)在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) 評(píng)論(2)  編輯  收藏 所屬分類: Web Service

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

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

          常用鏈接

          留言簿(2)

          隨筆分類(20)

          隨筆檔案(12)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 屏山县| 靖宇县| 尉氏县| 瑞丽市| 建始县| 武川县| 永定县| 威海市| 珠海市| 从江县| 岳阳县| 米脂县| 长宁县| 诸城市| 永康市| 武强县| 墨玉县| 柳江县| 南乐县| 肇东市| 曲水县| 永顺县| 土默特右旗| 荣昌县| 蒙城县| 石泉县| 历史| 平阳县| 青龙| 通州市| 阳山县| 聂拉木县| 扬中市| 尉氏县| 永兴县| 雷山县| 贵港市| 兴宁市| 中方县| 连江县| 胶南市|