<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • 如何創建安全的Web Service

    時間:2024-07-27 01:01:07 J2EE培訓 我要投稿
    • 相關推薦

    如何創建安全的Web Service

      我們在使用Web Service的過程中,很多情況是需要對web service請求做認證的,對于運行在web容器里的應用程序來說,可能會比較簡單一些,通常可以通過filter來做一些處理,但是其實CXF本身也提供了對web service認證的方式。

      1. 首先是一個簡單pojo

      package com.googlecode.garbagecan.cxfstudy.security;

      public class User {

      private String id;

      private String name;

      private String password;

      public String getId() {

      return id;

      }

      public void setId(String id) {

      this.id = id;

      }

      public String getName() {

      return name;

      }

      public void setName(String name) {

      this.name = name;

      }

      public String getPassword() {

      return password;

      }

      public void setPassword(String password) {

      this.password = password;

      }

      }

      2. Web Service接口

      package com.googlecode.garbagecan.cxfstudy.security;

      import java.util.List;

      import javax.jws.WebMethod;

      import javax.jws.WebResult;

      import javax.jws.WebService;

      @WebService

      public interface UserService {

      @WebMethod

      @WebResult List list();

      }

      3. Web Service實現類

      package com.googlecode.garbagecan.cxfstudy.security;

      import java.util.ArrayList;

      import java.util.List;

      public class UserServiceImpl implements UserService {

      public List list() {

      List users = new ArrayList();

      for (int i = 0; i < 10; i++) {

      User user = new User();

      user.setId("" + i);

      user.setName("user_" + i);

      user.setPassword("password_" + i);

      users.add(user);

      }

      return users;

      }

      }

      4. Server端Handler,其中使用了一個Map來存放用戶信息,真是應用中可以使用數據庫或者其它方式獲取用戶和密碼

      package com.googlecode.garbagecan.cxfstudy.security;

      import java.io.IOException;

      import java.util.HashMap;

      import java.util.Map;

      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 ServerUsernamePasswordHandler implements CallbackHandler {

      // key is username, value is password

      private Map users;

      public ServerUsernamePasswordHandler() {

      users = new HashMap();

      users.put("admin", "admin");

      }

      public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

      WSPasswordCallback callback = (WSPasswordCallback) callbacks[0];

      String id = callback.getIdentifier();

      if (users.containsKey(id)) {

      if (!callback.getPassword().equals(users.get(id))) {

      throw new SecurityException("Incorrect password.");

      }

      } else {

      throw new SecurityException("Invalid user.");

      }

      }

      }

      5. Client端Handler,用來設置用戶密碼,在真實應用中可以根據此類和下面的測試類來修改邏輯設置用戶名和密碼。

      package com.googlecode.garbagecan.cxfstudy.security;

      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 ClientUsernamePasswordHandler implements CallbackHandler {

      public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

      WSPasswordCallback callback = (WSPasswordCallback) callbacks[0];

      int usage = callback.getUsage();

      System.out.println("identifier: " + callback.getIdentifier());

      System.out.println("usage: " + callback.getUsage());

      if (usage == WSPasswordCallback.USERNAME_TOKEN) {

      callback.setPassword("admin");

      }

      }

      }

      6. 單元測試類,注意在Server端添加了WSS4JInInterceptor到Interceptor列表中,在Client添加了WSS4JOutInterceptor到Interceptor列表中。

      package com.googlecode.garbagecan.cxfstudy.security;

      import java.net.SocketTimeoutException;

      import java.util.HashMap;

      import java.util.List;

      import java.util.Map;

      import javax.xml.ws.WebServiceException;

      import junit.framework.Assert;

      import org.apache.cxf.endpoint.Client;

      import org.apache.cxf.endpoint.Endpoint;

      import org.apache.cxf.frontend.ClientProxy;

      import org.apache.cxf.interceptor.LoggingInInterceptor;

      import org.apache.cxf.interceptor.LoggingOutInterceptor;

      import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

      import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

      import org.apache.cxf.transport.http.HTTPConduit;

      import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

      import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;

      import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;

      import org.apache.ws.security.WSConstants;

      import org.apache.ws.security.handler.WSHandlerConstants;

      import org.junit.BeforeClass;

      import org.junit.Test;

      public class UserServiceTest {

      private static final String address = "http://localhost:9000/ws/security/userService";

      @BeforeClass

      public static void setUpBeforeClass() throws Exception {

      JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();

      factoryBean.getInInterceptors().add(new LoggingInInterceptor());

      factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

      Map props = new HashMap();

      props.put("action", "UsernameToken");

      props.put("passwordType", "PasswordText");

      props.put("passwordCallbackClass", ServerUsernamePasswordHandler.class.getName());

      WSS4JInInterceptor wss4JInInterceptor = new WSS4JInInterceptor(props);

      factoryBean.getInInterceptors().add(wss4JInInterceptor);

      factoryBean.setServiceClass(UserServiceImpl.class);

      factoryBean.setAddress(address);

      factoryBean.create();

      }

      @Test

      public void testList() {

      JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

      factoryBean.setAddress(address);

      factoryBean.setServiceClass(UserService.class);

      Object obj = factoryBean.create();

      Client client = ClientProxy.getClient(obj);

      Endpoint endpoint = client.getEndpoint();

      Map props = new HashMap();

      props.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);

      props.put(WSHandlerConstants.USER, "admin");

      props.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);

      props.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientUsernamePasswordHandler.class.getName());

      WSS4JOutInterceptor wss4JOutInterceptor = new WSS4JOutInterceptor(props);

      endpoint.getOutInterceptors().add(wss4JOutInterceptor);

      HTTPConduit conduit = (HTTPConduit) client.getConduit();

      HTTPClientPolicy policy = new HTTPClientPolicy();

      policy.setConnectionTimeout(5 * 1000);

      policy.setReceiveTimeout(5 * 1000);

      conduit.setClient(policy);

      UserService service = (UserService) obj;

      try {

      List users = service.list();

      Assert.assertNotNull(users);

      Assert.assertEquals(10, users.size());

      } catch(Exception e) {

      if (e instanceof WebServiceException

      && e.getCause() instanceof SocketTimeoutException) {

      System.err.println("This is timeout exception.");

      } else {

      e.printStackTrace();

      }

      }

      }

      }

      最后運行上面的測試類來測試結果,也可以修改測試方法中的密碼,看看錯誤結果。

    【如何創建安全的Web Service】相關文章:

    Web Service的開發與應用基礎07-12

    如何保證Web服務器安全06-30

    如何面試Web前端開發10-10

    如何由淺入深實踐學習 Web 標準10-10

    如何識別和防御Web網頁木馬09-11

    如何創建班組文化09-18

    CAD邊界如何創建10-02

    CAD如何創建新布局06-09

    Web2.0該如何走向商業化08-29

    如何用Dreamweaver批量做web網頁詳細步驟09-17

    主站蜘蛛池模板: 中文字幕久久精品无码| 日韩精品无码Av一区二区| 久久无码人妻精品一区二区三区| 国产成人精品2021| 亚洲精品无码日韩国产不卡?V| 久久精品国产99国产电影网 | 亚洲国产精品无码久久久蜜芽| 国产成人精品综合在线观看| 91麻豆精品国产| 少妇人妻偷人精品无码视频| 色婷婷噜噜久久国产精品12p| 91av国产精品| 久久99热国产这有精品| 国产精品白丝AV网站| 日韩精品真人荷官无码| 中文字幕无码精品三级在线电影 | 2020久久精品国产免费| 日韩精品人妻系列无码专区| 香港aa三级久久三级老师2021国产三级精品三级在| 自怕偷自怕亚洲精品| 久久精品国产半推半就| 91在线视频精品| 国产精品美脚玉足脚交欧美| 蜜国产精品jk白丝AV网站| 亚洲精品乱码久久久久久| 亚洲国产成人久久精品99| 人妻少妇精品无码专区动漫| 久久成人精品| 久久精品国产亚洲5555| 九九线精品视频在线观看| 国产精品龙口护士门在线观看| 99爱在线视频这里只有精品| 欧美精品免费观看二区| 欧美精品国产精品| 91精品日韩人妻无码久久不卡| 亚洲综合精品一二三区在线| 3级黄性日本午夜精品| 99久久亚洲综合精品成人| 99RE8这里有精品热视频| 国产精品一区二区三区99| 精品久久久久久久久久中文字幕|