<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的家園

    我的家園

    注意:以下客戶端調(diào)用代碼中獲取服務(wù)端ws實例,都是通過CXF 入門: 遠(yuǎn)程接口調(diào)用方式實現(xiàn)

    ?

    直入正題!

    以下是服務(wù)端配置

    ========================================================

    一,web.xml配置,具體不在詳述

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<!--ws-context.xml(必須)是cxf配置文件, wssec.xml可選,作用可以打印出加密信息類容 -->
    		<param-value>WEB-INF/ws-context.xml,WEB-INF/wssec.xml</param-value>
    	</context-param>
    
    	<listener>
    		<listener-class>
    			org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
    
    	<servlet>
    		<servlet-name>CXFServlet</servlet-name>
    		<display-name>CXF Servlet</display-name>
    		<servlet-class>
    			org.apache.cxf.transport.servlet.CXFServlet
    		</servlet-class>
    		<load-on-startup>0</load-on-startup>
    	</servlet>
    
    	<servlet-mapping>
    		<servlet-name>CXFServlet</servlet-name>
    		<url-pattern>/services/*</url-pattern>
    	</servlet-mapping>
    </web-app>

    ?二,ws具體代碼
    簡單的接口

    ?

    import javax.jws.WebService;
    
    @WebService()
    public interface WebServiceSample {
    	String say(String name);
    
    }
    

    ?

    ?

    接口具體實現(xiàn)類

    ?

    public class WebServiceSampleImpl implements WebServiceSample {
    
    	public String say(String name) {
    		return "你好," + name;
    	}
    }
    ?

    ?

    ?

    三,ws回調(diào)函數(shù),必須實現(xiàn)javax.security.auth.callback.CallbackHandler

    從cxf2.4.x后校驗又cxf內(nèi)部實現(xiàn)校驗,所以不必自己校驗password是否相同,但客戶端必須設(shè)置,詳情請參考:http://cxf.apache.org/docs/24-migration-guide.html的Runtime ?Changes片段

    ?

    回調(diào)函數(shù)WsAuthHandler代碼,校驗客戶端請求是否合法 ,合法就放行,否則拒絕執(zhí)行任何操作

    ?

    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.cxf.interceptor.Fault;
    import org.apache.ws.security.WSPasswordCallback;
    import org.apache.xmlbeans.impl.soap.SOAPException;
    
    public class WsAuthHandler implements CallbackHandler {
    
    	public void handle(Callback[] callbacks) throws IOException,
    			UnsupportedCallbackException {
    		for (int i = 0; i < callbacks.length; i++) {
    			WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
    			String identifier = pc.getIdentifier();
    			int usage = pc.getUsage();
    			if (usage == WSPasswordCallback.USERNAME_TOKEN) {// 密鑰方式USERNAME_TOKEN
    				// username token pwd...
    				// ▲這里的值必須和客戶端設(shè)的值相同,從cxf2.4.x后校驗方式改為cxf內(nèi)部實現(xiàn)校驗,不必自己比較password是否相同
    				// 請參考:http://cxf.apache.org/docs/24-migration-guide.html的Runtime
    				// Changes片段
    				pc.setPassword("testPassword");// ▲【這里非常重要】▲
    				// ▲PS 如果和客戶端不同將拋出org.apache.ws.security.WSSecurityException:
    				// The
    				// security token could not be authenticated or
    				// authorized異常,服務(wù)端會認(rèn)為客戶端為非法調(diào)用
    			} else if (usage == WSPasswordCallback.SIGNATURE) {// 密鑰方式SIGNATURE
    				// set the password for client's keystore.keyPassword
    				// ▲這里的值必須和客戶端設(shè)的值相同,從cxf2.4.x后校驗方式改為cxf內(nèi)部實現(xiàn)校驗,不必自己比較password是否相同;
    				// 請參考:http://cxf.apache.org/docs/24-migration-guide.html的Runtime
    				// Changes片段
    				pc.setPassword("testPassword");// //▲【這里非常重要】▲
    				// ▲PS:如果和客戶端不同將拋出org.apache.ws.security.WSSecurityException:The
    				// security token could not be authenticated or
    				// authorized異常,服務(wù)端會認(rèn)為客戶端為非法調(diào)用
    			}
    			//不用做其他操作
    		}
    	}
    }
    
    ?

    ?

    四,CXF配置ws-context.xml:

    ?

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    
    	<import resource="classpath:META-INF/cxf/cxf.xml" />
    	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    	<!-- 以上未基本配置,必須,位置在cxf jar中 -->
    	<jaxws:endpoint id="webServiceSample" address="/WebServiceSample"
    		implementor="com.service.impl.WebServiceSampleImpl">
    		<!--inInterceptors表示被外部調(diào)用時,調(diào)用此攔截器 -->
    		<jaxws:inInterceptors>
    			<bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor" />
    			<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
    				<constructor-arg>
    					<map>
    						<!-- 設(shè)置加密類型 -->
    						<entry key="action" value="UsernameToken" />
    						<!-- 設(shè)置密碼類型為明文 -->
    						<entry key="passwordType" value="PasswordText" />
    						<!--<entry key="action" value="UsernameToken Timestamp" /> 設(shè)置密碼類型為加密<entry 
    							key="passwordType" value="PasswordDigest" /> -->
    						<entry key="passwordCallbackClass" value="com.service.handler.WsAuthHandler" />
    					</map>
    				</constructor-arg>
    			</bean>
    		</jaxws:inInterceptors>
    	</jaxws:endpoint>
    </beans>
    

    ?

    ?

    CXF配置wssec.xml(可選),用于配置輸出校驗的具體信息

    ?

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
    	xmlns:wsa="http://cxf.apache.org/ws/addressing" xmlns:http="http://cxf.apache.org/transports/http/configuration"
    	xmlns:wsrm-policy="http://schemas.xmlsoap.org/ws/2005/02/rm/policy"
    	xmlns:wsrm-mgr="http://cxf.apache.org/ws/rm/manager"
    	xsi:schemaLocation="
           http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
           http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
           http://schemas.xmlsoap.org/ws/2005/02/rm/policy http://schemas.xmlsoap.org/ws/2005/02/rm/wsrm-policy.xsd
           http://cxf.apache.org/ws/rm/manager http://cxf.apache.org/schemas/configuration/wsrm-manager.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    	<cxf:bus>
    		<cxf:features>
    			<cxf:logging />
    			<wsa:addressing />
    		</cxf:features>
    	</cxf:bus>
    </beans>

    ?

    服務(wù)端代碼及配置到此結(jié)束!!!

    ?

    =========================================================

    =========================================================

    以下是客戶端配置,主要是回調(diào)函數(shù),在客戶端調(diào)用服務(wù)端前被調(diào)用,負(fù)責(zé)安全信息的設(shè)置

    ----------------------------------------------------------------------------------------

    ?

    ?

    一,先實現(xiàn)回調(diào)函數(shù)WsClinetAuthHandler,同樣必須實現(xiàn)javax.security.auth.callback.CallbackHandler

    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[i];
    			System.out.println("identifier: " + pc.getIdentifier());
    			// 這里必須設(shè)置密碼,否則會拋出:java.lang.IllegalArgumentException: pwd == null
    			// but a password is needed
    			pc.setPassword("testPassword");// ▲【這里必須設(shè)置密碼】▲
    		}
    	}
    }
    ?

    二,客戶端調(diào)用代碼:

    ?

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor;
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
    import org.apache.ws.security.WSConstants;
    import org.apache.ws.security.handler.WSHandlerConstants;
    
    import test.saa.client.WebServiceSample;
    import test.saa.handler.WsClinetAuthHandler;
    
    public class TestClient {
    
    	public static void main(String[] args) {
    		// 以下和服務(wù)端配置類似,不對,應(yīng)該說服務(wù)端和這里的安全驗證配置一致
    		Map<String, Object> outProps = new HashMap<String, Object>();
    		outProps.put(WSHandlerConstants.ACTION,
    				WSHandlerConstants.USERNAME_TOKEN);
    		outProps.put(WSHandlerConstants.USER, "admin");
    		outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
    		// 指定在調(diào)用遠(yuǎn)程ws之前觸發(fā)的回調(diào)函數(shù)WsClinetAuthHandler,其實類似于一個攔截器
    		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
    				WsClinetAuthHandler.class.getName());
    		ArrayList list = new ArrayList();
    		// 添加cxf安全驗證攔截器,必須
    		list.add(new SAAJOutInterceptor());
    		list.add(new WSS4JOutInterceptor(outProps));
    
    		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    		// WebServiceSample服務(wù)端接口實現(xiàn)類,這里并不是直接把服務(wù)端的類copy過來,具體請參考http://learning.iteye.com/blog/1333223
    		factory.setServiceClass(WebServiceSample.class);
    		// 設(shè)置ws訪問地址
    		factory.setAddress("http://localhost:8080/cxf-wssec/services/WebServiceSample");
            //注入攔截器,用于加密安全驗證信息
    		factory.getOutInterceptors().addAll(list);
    		WebServiceSample service = (WebServiceSample) factory.create();
    		String response = service.say("2012");
    		System.out.println(response);
    	}
    }

    ?客戶端到此結(jié)束!!!!

    ========================================================================

    #######################################################################

    ?

    PS:客戶端的另一種調(diào)用方式,主要通過配置文件,不過需要spring bean的配置文件(第一種就不用牽扯到spring的配置,比較通用吧!)

    ?

    一,回調(diào)函數(shù)WsClinetAuthHandler不變,和上面一樣
    二,client-beans.xml安全驗證配置文件,具體信息看注釋,如下:

    ?

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    	xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
    <!--這里無非是通過配置來替代JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean()創(chuàng)建代理并實例化一個ws-->
    	<bean id="client" class="test.saa.client.WebServiceSample"
    		factory-bean="clientFactory" factory-method="create" />
    	<!-- 通過代理創(chuàng)建ws實例 -->
    	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    		<property name="serviceClass" value="test.saa.client.WebServiceSample" />
    		<!-- ws地址,也可以是完整的wsdl地址 -->
    		<property name="address"
    			value="http://localhost:8080/cxf-wssec/services/WebServiceSample" />
    		<!--outInterceptors表示調(diào)用外部指定ws時,調(diào)用此攔截器 -->
    		<property name="outInterceptors">
    			<list>
    				<bean class="org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor" />
    				<ref bean="wss4jOutConfiguration" />
    			</list>
    		</property>
    	</bean>
    
    	<bean id="wss4jOutConfiguration" class="org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor">
    		<property name="properties">
    			<map>
    				<!-- 設(shè)置加密類型 ,服務(wù)端需要和這里的設(shè)置保持一致-->
    				<entry key="action" value="UsernameToken" />
    				<entry key="user" value="admin" />
    				<!-- 設(shè)置密碼為明文 ,服務(wù)端需要和這里的設(shè)置保持一致-->
    				<entry key="passwordType" value="PasswordText" />
    				<!-- <entry key="action" value="UsernameToken Timestamp" /> <entry key="user" 
    					value="adminTest" /> 設(shè)置密碼類型為加密方式,服務(wù)端需要和這里的設(shè)置保持一致<entry key="passwordType" value="PasswordDigest" 
    					/> -->
    				<entry key="passwordCallbackRef">
    					<ref bean="passwordCallback" />
    				</entry>
    			</map>
    		</property>
    	</bean>
    	<bean id="passwordCallback" class="test.saa.handler.WsClinetAuthHandler" />
    </beans>

    ?
    三,具體調(diào)用服務(wù)端代碼:

    ?

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import test.saa.client.WebServiceSample;
    
    public final class Client {
    
    	public static void main(String args[]) throws Exception {
    	//加載配置
    		ApplicationContext context = new ClassPathXmlApplicationContext(
    				new String[] { "test/saa/client-beans.xml" });
            //獲取ws實例
    		WebServiceSample client = (WebServiceSample) context.getBean("client");
    		String response = client.say("2012");
    		System.out.println("Response: " + response);
    	}
    }

    ?
    到此客戶端第二種實現(xiàn)方式結(jié)束
    GOOD LUCKY!!!
    如有不明,請指教!!!






    Feedback

    # re: CXF 入門:創(chuàng)建一個基于WS-Security標(biāo)準(zhǔn)的安全驗證(CXF回調(diào)函數(shù)使用,)[未登錄]  回復(fù)  更多評論   

    2015-06-11 16:39 by hello
    好文章!

    # re: CXF 入門:創(chuàng)建一個基于WS-Security標(biāo)準(zhǔn)的安全驗證(CXF回調(diào)函數(shù)使用,)  回復(fù)  更多評論   

    2015-06-24 14:49 by qping
    謝謝~

    # re: CXF 入門:創(chuàng)建一個基于WS-Security標(biāo)準(zhǔn)的安全驗證(CXF回調(diào)函數(shù)使用,)  回復(fù)  更多評論   

    2016-07-15 11:16 by 李虎鵬
    @qping
    寫的真好,項目中要用,我試了一下,可以!萬分感謝!!!

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲中文字幕不卡无码| 永久免费观看的毛片的网站| 久久夜色精品国产噜噜噜亚洲AV| 亚洲va中文字幕无码久久| xxxxx做受大片在线观看免费| 亚洲国产成人一区二区精品区| 久久久久久久久久免免费精品 | 国产精品亚洲综合一区在线观看| 国产亚洲精品无码拍拍拍色欲| 深夜a级毛片免费无码| 亚洲av无码成人黄网站在线观看 | 免费一级毛片无毒不卡| 亚洲国产精品乱码一区二区 | 在线观看午夜亚洲一区| 免费一本色道久久一区| 一级毛片视频免费| 亚洲制服丝袜第一页| 毛茸茸bbw亚洲人| 国内一级一级毛片a免费| 久久精品成人免费网站| 在线观看亚洲电影| 亚洲手机中文字幕| 亚洲欧洲精品成人久久曰影片 | 97在线观看永久免费视频| 亚洲男人的天堂久久精品| 久久亚洲2019中文字幕| 久久久久国色AV免费看图片| 最近免费mv在线观看动漫| 91亚洲自偷手机在线观看| heyzo亚洲精品日韩| 伊人免费在线观看高清版| 亚洲日产乱码一二三区别 | 亚洲福利一区二区三区| 亚洲日韩国产一区二区三区| 毛片免费在线视频| 一个人看的www免费高清| 亚洲欧洲国产综合AV无码久久| 久久狠狠高潮亚洲精品 | 国产亚洲综合精品一区二区三区| 亚洲黄色三级网站| 亚洲短视频男人的影院|