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 閱讀(2371) 評論(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的呢?  回復  更多評論
            

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


          網站導航:
           

          <2008年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(2)

          隨筆分類(20)

          隨筆檔案(12)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高阳县| 遂溪县| 黔江区| 沿河| 正定县| 遵化市| 安多县| 顺昌县| 股票| 梅州市| 富锦市| 东乡| 桐庐县| 彭山县| 定州市| 同德县| 锡林郭勒盟| 万载县| 江北区| 西乡县| 南雄市| 英德市| 玛多县| 南宁市| 虎林市| 富阳市| 开江县| 滦南县| 邮箱| 车致| 犍为县| 慈利县| 太湖县| 江口县| 孙吴县| 安义县| 福海县| 改则县| 琼海市| 蕲春县| 北海市|