CXF学习笔记(4)-HelloWorld!-安全认证

前3节中介绍了如何发布一个webservice和客户端如何调用,一切貌似都正常,但存在着安全隐患-这样导致任意的客户端都可以调用我们的webservice服务,接下来将介绍如何采用基于用户名和密码认证的方式来实现客户端调用

1.服务器端配置

a.在发布service的时候为该service添加一个WSS4JInInterceptor 拦截器

表示我们的service需要用户提供用户名和密码认证后才能调用

[java] view plain copy print ?
  1. package com.crazycoder2010.webservice.cxf.server;    
  2.     
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.servlet.ServletConfig;  
  7.   
  8. import org.apache.cxf.endpoint.Endpoint;  
  9. import org.apache.cxf.endpoint.Server;  
  10. import org.apache.cxf.frontend.ServerFactoryBean;  
  11. import org.apache.cxf.service.invoker.BeanInvoker;  
  12. import org.apache.cxf.transport.servlet.CXFNonSpringServlet;  
  13. import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;  
  14. import org.apache.ws.security.WSConstants;  
  15. import org.apache.ws.security.handler.WSHandlerConstants;    
  16.     
  17. class=keyword>import com.crazycoder2010.webservice.cxf.server.HelloWorldServiceImpl;    
  18.     
  19. public class WebServiceServlet extends CXFNonSpringServlet {    
  20.     private static final long serialVersionUID = -5314312869027558456L;    
  21.     
  22.     @Override    
  23.     protected void loadBus(ServletConfig servletConfig) {    
  24.         super.loadBus(servletConfig);    
  25.         System.out.println("#####################");    
  26.         ServerFactoryBean svrFactory = new ServerFactoryBean();  
  27.         svrFactory.setServiceClass(HelloWorldService.class);  
  28.         svrFactory.setAddress("/passportService");  
  29.         svrFactory.setInvoker(new BeanInvoker(new HelloWorldServiceImp()));  
  30.         Server server = svrFactory.create();  
  31.         Endpoint cxfEndpoint = server.getEndpoint();  
  32.         Map inProps = new HashMap();  
  33.         inProps.put(WSHandlerConstants.ACTION,  
  34.                 WSHandlerConstants.USERNAME_TOKEN);  
  35.         inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);  
  36.         inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,  
  37.                 ServerPasswordHandler.class.getName());  
  38.         cxfEndpoint.getInInterceptors().add(new WSS4JInInterceptor(inProps));  
  39.     }    
  40. }  
package com.crazycoder2010.webservice.cxf.server;  
  
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;

import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.service.invoker.BeanInvoker;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;  
  
import com.crazycoder2010.webservice.cxf.server.HelloWorldServiceImpl;  
  
public class WebServiceServlet extends CXFNonSpringServlet {  
    private static final long serialVersionUID = -5314312869027558456L;  
  
    @Override  
    protected void loadBus(ServletConfig servletConfig) {  
        super.loadBus(servletConfig);  
        System.out.println("#####################");  
        ServerFactoryBean svrFactory = new ServerFactoryBean();
        svrFactory.setServiceClass(HelloWorldService.class);
        svrFactory.setAddress("/passportService");
        svrFactory.setInvoker(new BeanInvoker(new HelloWorldServiceImp()));
        Server server = svrFactory.create();
        Endpoint cxfEndpoint = server.getEndpoint();
        Map inProps = new HashMap();
        inProps.put(WSHandlerConstants.ACTION,
                WSHandlerConstants.USERNAME_TOKEN);
        inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
        inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
                ServerPasswordHandler.class.getName());
        cxfEndpoint.getInInterceptors().add(new WSS4JInInterceptor(inProps));
    }  
}
b.处理用户名和密码的逻辑

添加一个ServerPasswordHandler类,该类实现CallbackHandler接口,user='kevin',password='111111'为该service的访问用户名密码

[java] view plain copy print ?
  1. public class ServerPasswordHandler implements CallbackHandler{  
  2.     private String user = "kevin";  
  3.     private String password = "111111";  
  4.     @Override  
  5.     public void handle(Callback[] callbacks) throws IOException,  
  6.             UnsupportedCallbackException {  
  7.         WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];  
  8.         if(user.equals(pc.getIdentifier())){  
  9.             pc.setPassword(password);  
  10.         }  
  11.     }  
  12. }  
public class ServerPasswordHandler implements CallbackHandler{
	private String user = "kevin";
	private String password = "111111";
	@Override
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
		if(user.equals(pc.getIdentifier())){
			pc.setPassword(password);
		}
	}
}
2.客户端配置

a.配置WSS4JOutInterceptor拦截器,表示发出请求的时候将以提供的用户名和密码信息提交到服务器端认证

[java] view plain copy print ?
  1. public class WebServiceFactory {  
  2.     public static PassportService getHelloWorldService(){  
  3.     Map outProps = new HashMap();  
  4.     outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);  
  5.     outProps.put(WSHandlerConstants.USER, "kevin");//这个用户即服务器端配置的用户名   
  6.     outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);  
  7.     outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordHandler.class.getName());//这个类用来获取客户端认证用户名密码信息   
  8.     JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();    
  9.         bean.getInInterceptors().add(new LoggingInInterceptor());    
  10.         bean.getInFaultInterceptors().add(new LoggingOutInterceptor());   
  11.         bean.setServiceClass(PassportService.class);  
  12.         bean.setAddress("http://localhost:8080/CXF-Server/webservice/helloWorldService");   
  13.         HelloWorldService helloWorldService = (HelloWorldService)bean.create();  
  14.         ClientProxy clientProxy = (ClientProxy)Proxy.getInvocationHandler(passportService);  
  15.         Client client = clientProxy.getClient();  
  16.         client.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));//配置拦截器   
  17.         return helloWorldService;  
  18.     }  
  19. }  
public class WebServiceFactory {
	public static PassportService getHelloWorldService(){
	Map outProps = new HashMap();
	outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
	outProps.put(WSHandlerConstants.USER, "kevin");//这个用户即服务器端配置的用户名
	outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST);
	outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordHandler.class.getName());//这个类用来获取客户端认证用户名密码信息
	JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();  
        bean.getInInterceptors().add(new LoggingInInterceptor());  
        bean.getInFaultInterceptors().add(new LoggingOutInterceptor()); 
        bean.setServiceClass(PassportService.class);
        bean.setAddress("http://localhost:8080/CXF-Server/webservice/helloWorldService"); 
        HelloWorldService helloWorldService = (HelloWorldService)bean.create();
        ClientProxy clientProxy = (ClientProxy)Proxy.getInvocationHandler(passportService);
        Client client = clientProxy.getClient();
        client.getOutInterceptors().add(new WSS4JOutInterceptor(outProps));//配置拦截器
        return helloWorldService;
	}
}
b.配置客户端认证信息类

[java] view plain copy print ?
  1. public class ClientPasswordHandler implements CallbackHandler{  
  2.     @Override  
  3.     public void handle(Callback[] callbacks) throws IOException,  
  4.             UnsupportedCallbackException {  
  5.     WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];  
  6.         pc.setPassword("kevin");//这个地方的用户名和密码与服务器端保持一致     
  7.         pc.setIdentifier("111111");  
  8.     }  
  9. }  
public class ClientPasswordHandler implements CallbackHandler{
	@Override
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
	WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
        pc.setPassword("kevin");//这个地方的用户名和密码与服务器端保持一致	
        pc.setIdentifier("111111");
	}
}
c.客户端调用

[java] view plain copy print ?
  1. public class DemoClient {  
  2.     public static void main(String[] args) {  
  3. class=java name="code">                HelloWorldService helloWorldService = WebServiceFactory.getHelloWorldService();  
  4.         helloWorldService.sayHello("John");  
  5.     }  
  6. }  
  7.   
  8.   
  9.   
  10. 小结:  
  11.   
  12. 这种方式利用服务器端与客户端共享同一用户名密码的方式实现了webservice的安全访问,流程图如下

      
  13. "WIDTH: 433px" alt="" src="http://hi.csdn.net/attachment/201108/17/0_13135751629tGv.gif" jQuery17013429473743959463="1">

      
  14. 参考资料:"http://cxf.apache.org/docs/ws-security.html" target=_blank>http://cxf.apache.org/docs/ws-security.html
      

  15.   

  16.   

  17.   

  18.   

你可能感兴趣的:(Java,WebServcie)