1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package
com.what21.cxf.interceptor;
import
javax.xml.soap.SOAPException;
import
javax.xml.soap.SOAPHeader;
import
javax.xml.soap.SOAPMessage;
import
org.apache.cxf.binding.soap.SoapMessage;
import
org.apache.cxf.binding.soap.saaj.SAAJInInterceptor;
import
org.apache.cxf.interceptor.Fault;
import
org.apache.cxf.phase.AbstractPhaseInterceptor;
import
org.apache.cxf.phase.Phase;
import
org.w3c.dom.Node;
import
org.w3c.dom.NodeList;
public
class
HeaderAuthInterceptor
extends
AbstractPhaseInterceptor
private
SAAJInInterceptor saa =
new
SAAJInInterceptor();
public
HeaderAuthInterceptor() {
super
(Phase.PRE_PROTOCOL);
getAfter().add(SAAJInInterceptor.
class
.getName());
}
public
void
handleMessage(SoapMessage message)
throws
Fault {
SOAPMessage soapMsg = message.getContent(SOAPMessage.
class
);
if
(soapMsg ==
null
){
saa.handleMessage(message);
soapMsg = message.getContent(SOAPMessage.
class
);
}
SOAPHeader header =
null
;
try
{
header = soapMsg.getSOAPHeader();
NodeList userNodes = header.getElementsByTagName(
"username"
);
NodeList passNodes = header.getElementsByTagName(
"password"
);
if
(userNodes!=
null
&& passNodes!=
null
){
Node userNode = userNodes.item(
0
);
Node passNode = passNodes.item(
0
);
String username = userNode.getTextContent();
String password = passNode.getTextContent();
System.out.println(
"username : "
+ username +
",password : "
+ password);
return
;
}
}
catch
(SOAPException e) {
e.printStackTrace();
}
throw
new
Fault(
new
Exception(
"auth error."
));
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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"
/>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package
com.what21.cxf.interceptor;
import
java.util.List;
import
javax.xml.namespace.QName;
import
org.apache.cxf.binding.soap.SoapHeader;
import
org.apache.cxf.binding.soap.SoapMessage;
import
org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import
org.apache.cxf.headers.Header;
import
org.apache.cxf.helpers.DOMUtils;
import
org.apache.cxf.interceptor.Fault;
import
org.apache.cxf.phase.Phase;
import
org.w3c.dom.Document;
import
org.w3c.dom.Element;
public
class
ClientAuthInterceptor
extends
AbstractSoapInterceptor{
public
ClientAuthInterceptor(){
super
(Phase.WRITE);
}
public
void
handleMessage(SoapMessage soapMessage)
throws
Fault {
Document doc=DOMUtils.createDocument();
// 根节点
Element rootEle=doc.createElementNS(
""
,
"AuthToken"
);
// 用户ID
Element userEle = doc.createElement(
"username"
);
userEle.setTextContent(
"username"
);
rootEle.appendChild(userEle);
// 密码
Element passEle = doc.createElement(
"password"
);
passEle.setTextContent(
"password"
);
rootEle.appendChild(passEle);
// 添加到头
List
QName qname=
new
QName(
"AuthToken"
);
SoapHeader head=
new
SoapHeader(qname, rootEle);
headers.add(head);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package
com.what21.cxf.interceptor;
import
org.apache.cxf.interceptor.LoggingInInterceptor;
import
org.apache.cxf.interceptor.LoggingOutInterceptor;
import
org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import
com.what21.cxf.spring.IHello;
public
class
CxfClient {
/**
* @param args
*/
public
static
void
main(String[] args) {
JaxWsProxyFactoryBean soapFactoryBean =
new
JaxWsProxyFactoryBean();
soapFactoryBean.setAddress(
"http://127.0.0.1:8080/cxf/helloService"
);
soapFactoryBean.getInInterceptors().add(
new
LoggingInInterceptor());
soapFactoryBean.getOutInterceptors().add(
new
LoggingOutInterceptor());
// 添加拦截器
soapFactoryBean.getOutInterceptors().add(
new
ClientAuthInterceptor());
soapFactoryBean.setServiceClass(IHello.
class
);
Object o = soapFactoryBean.create();
IHello helloService = (IHello)o;
String result =
null
;
try
{
result = helloService.message(
"i am client!"
);
}
catch
(Exception e) {
e.printStackTrace();
}
System.out.println(result);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
|