WebService开发笔记 3 -- 增强访问 WebService 的安全性

阅读更多
在 WebService开发笔记 1中我们创建了一个WebService简单实例,下面我们通过一个简单的用户口令验证机制来加强一下WebService的安全性:

1.修改WebService 服务端 spring 配置文件 ws-context.xml

	
	

		
			
			
				
					
						
						
						
					
				
			
			

	
	



2.服务端添加passwordCallbackClass回调类,该类进行用户口令验证:
package cn.org.coral.biz.examples.webservice.handler;

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

	public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
		WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
        if (pc.getIdentifer().equals("ws-client")){
            if (!pc.getPassword().equals("admin")) {
                throw new SecurityException("wrong password");
           }
        }else{
        	throw new SecurityException("wrong username");
        }
	}

}


3.客户端修改spring 配置文件 wsclient-context.xml 如下:




	
	


	
		
		
		
			
				
				
			
		
	

	
		
			
				
				
				
				
					
						passwordCallbackRef
					
					
				
			
		
	
	
	




4.客户端添加passwordCallback类,通过该类设置访问口令
package cn.org.coral.biz.examples.webservice.handler;

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


    public void handle(Callback[] callbacks) throws IOException, 
                    UnsupportedCallbackException { 
            for (int i = 0; i < callbacks.length; i++) { 
                    WSPasswordCallback pc = (WSPasswordCallback) callbacks[0]; 
                    int usage = pc.getUsage(); 


                    System.out.println("identifier: " + pc.getIdentifer()); 
                    System.out.println("usage: " + pc.getUsage()); 
                    if (usage == WSPasswordCallback.USERNAME_TOKEN) { 
                            // username token pwd... 
                            pc.setPassword("admin"); 

                    } else if (usage == WSPasswordCallback.SIGNATURE) { 
                            // set the password for client's keystore.keyPassword 
                            pc.setPassword("keyPassword"); 
                    } 
            } 
    } 

}


5.junit单元测试程序:
package cn.org.coral.biz.examples.webservice;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.util.Assert;

public class TestWebService extends AbstractDependencyInjectionSpringContextTests {
	WebServiceSample webServiceSampleClient;
	
	@Override
	protected String[] getConfigLocations() {
		setAutowireMode(AUTOWIRE_BY_NAME);
		return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };
	}

	/**
	 * @param webServiceSampleClient the webServiceSampleClient to set
	 */
	public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {
		this.webServiceSampleClient = webServiceSampleClient;
	}

	public void testSay(){
		String result = webServiceSampleClient.say(" world");
		Assert.hasText(result);		
	}
}

你可能感兴趣的:(WebService,Java,Apache,SOAP,Security)