cxf webservice配置(带超时)

1.spring配置文件方式测试
<!-- 客户端密码配置Bean定义 -->
<bean id="passwordCallback" class="com.creditease.core.ws.client.ClientValidateCallback">
<property name="userMap">
<map>
<entry key="abc" value="abc" />
</map>
</property>
</bean>

<jaxws:client id="testService" serviceClass="com.creditease.core.ws.client.UserService"
address="http://localhost:8080/project/services/testService">
      <jaxws:outInterceptors>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
<property name="properties">
<map>
<entry key="action" value="UsernameToken" />
<entry key="user" value="admin" />
<entry key="passwordType" value="PasswordText" />
<entry key="passwordCallbackRef" value-ref="passwordCallback" />
</map>
</property>
</bean>
</jaxws:outInterceptors>
</jaxws:client>
用户名密码验证类
public class ClientValidateCallback implements CallbackHandler {

	private Map<String, String> userMap = null;

	public void setUserMap(Map<String, String> userMap) {
		this.userMap = userMap;
	}

	public void handle(Callback[] callbacks) throws IOException,
			UnsupportedCallbackException {
		for (int i = 0; i < callbacks.length; i++) {
			WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
			int usage = pc.getUsage();
			if (!userMap.containsKey(pc.getIdentifier()))
				throw new WSSecurityException("user not exists ");
			String pass = userMap.get(pc.getIdentifier());
			if (usage == WSPasswordCallback.USERNAME_TOKEN && pass != null) {
				pc.setPassword(pass);
				return;
			}
		}
	}
}

@Autowired
private TestService testService;
2.客户端java代码测试
JaxWsProxyFactoryBean userClient = new JaxWsProxyFactoryBean();
		userClient.setServiceClass(UserService.class);
		userClient.setAddress("http://localhost:8080/project/services/testService");
		// 设置用户名密码
		Map<String, Object> outProps = new HashMap<String, Object>(); 
		outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN); 
		outProps.put(WSHandlerConstants.USER, "abc"); 
		outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT); 
		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ClientPasswordCallback.class.getName()); 
		
		WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps); 
		userClient.getOutInterceptors().add(wssOut); 
		userClient.getOutInterceptors().add(new SAAJOutInterceptor()); 

		TestService testService = (TestService) userClient.create();
// 超时设置
Client proxy = ClientProxy.getClient(testService);
		HTTPConduit conduit = (HTTPConduit) proxy.getConduit(); 
		HTTPClientPolicy httpClientPolicy =  new  HTTPClientPolicy();   
		httpClientPolicy.setConnectionTimeout(360000);   
		httpClientPolicy.setAllowChunking( false );   
		httpClientPolicy.setReceiveTimeout(320000);   
		conduit.setClient(httpClientPolicy);

用户名密码验证类
public class ClientPasswordCallback implements CallbackHandler {

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

		WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
		pc.setIdentifier("admin");
		pc.setPassword("admin");
	}

}



你可能感兴趣的:(webservice)