CXF 2.5 服务端和客户端添加安全认证

阅读更多

CXF Download : http://www.apache.org/dyn/closer.cgi?path=/cxf/2.5.2/apache-cxf-2.5.2.zip

 

服务端代码

IHi.java

package com.cumce.spring.service;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
//import javax.jws.soap.SOAPBinding;
//import javax.jws.soap.SOAPBinding.Style;

/**
 * IHi.java
 * 
 * @author  
 * @version 1.0, 2012-2-26
 * @since   JDK1.5
 */
@WebService
//@SOAPBinding(style = Style.RPC)
public interface IHi {
	
	@WebResult(name = "result") String call(@WebParam(name = "name") String name);

}

Hi.java

package com.cumce.spring.service;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * Hi.java
 * 
 * @author 
 * @version 1.0, 2012-2-26
 * @since   JDK1.5
 */
@WebService
public class Hi implements IHi {

	/* (non-Javadoc)
	 * @see com.cumce.spring.service.IHi#call(java.lang.String)
	 */
	@Override
	public @WebResult(name = "result") String call(@WebParam(name = "name") String name) {
		String s = "Hi, " + name;
		System.out.println(s);
		return s;
	}

}

ServerPasswordCallback.java

package com.cumce.spring.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;

/**
 * ServerPasswordCallback.java
 * 
 * @author 
 * @version 1.0, 2012-2-26
 * @since   JDK1.5
 */
public class ServerPasswordCallback implements CallbackHandler {
	
	/* (non-Javadoc)
	 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
	 */
	@Override
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		
		WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
		if (pc.getIdentifier().equals("user_name")) { // username
			pc.setPassword("password"); // password
			System.out.println("通过安全认证");
		}

	}

}

application-context.xml



	
	
	
	
	
	
	
	
		
			
			
			
				
					
						
						
						
							
                        
					
				
			  
		
	
	
	

web.xml



	SPN
	
	
	
		contextConfigLocation
		classpath*:application-context.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		CXFServlet
		org.apache.cxf.transport.servlet.CXFServlet
		1
	
	
		CXFServlet
		/*
	
	
	
		index.jsp
	

 

启动Tomcat服务,发布WebService,略...

 

客户端代码

ClientPasswordCallback.java

package com.cumce.spring.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;

/**
 * ClientPasswordCallback.java
 * 
 * @author
 * @version 1.0, 2012-2-26
 * @since   JDK1.5
 */
public class ClientPasswordCallback implements CallbackHandler {

	/* (non-Javadoc)
	 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
	 */
	@Override
	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {

		WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
		// Identifier和Password不能为空,否则无法通过验证
		pc.setIdentifier("user_name"); // username
		pc.setPassword("password"); // password

	}

}

application-client.xml



	
	
	
	
	
	
		
			
				classpath:webservice_security.properties
			
		
	

	
	
		
			
            
            
            	
            		
            			
						
						
            			
                            
                        
            		
            	
            
		
	
	
	

SpringClient.java

package com.cumce.spring.client;

import javax.xml.ws.soap.SOAPFaultException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cumce.spring.service.IHi;
import com.cumce.spring.service.pojo.IPersonService;

/**
 * SpringClient.java
 * 
 * @author 
 * @version 1.0, 2012-2-26
 * @since   JDK1.5
 */
public class SpringClient {

	/**
	 * Application entry point.
	 */
	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("application-client.xml");
		try {
			IHi hi = (IHi) context.getBean("client");
			System.out.println(hi.call("CXF Client"));
		} catch (SOAPFaultException ex) {
			ex.printStackTrace();
		}
	}

}
 

 

你可能感兴趣的:(WebService)