Samples JDK
WSSecurityInterceptor.java
1 package com.freemindcafe.apache.cxf.wsdl.sample3;
2 
3 import java.util.List;
4 import java.util.Map;
5 
6 import javax.xml.namespace.QName;
7 
8 import org.apache.cxf.binding.soap.SoapMessage;
9 import org.apache.cxf.headers.Header;
10 import org.apache.cxf.interceptor.Fault;
11 import org.apache.cxf.phase.Phase;
12 import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
13 import org.apache.log4j.Logger;
14 
15 //\cond HIDDEN_SYMBOLS
16 /**
17  *
18  * @author li00000y
19  *
20  */
21 //Phase.PRE_PROTOCOL
22 public class WSSecurityInterceptor extends WSS4JInInterceptor{
23 
24  protected Logger log = Logger.getLogger(WSSecurityInterceptor.class);
25 
26  public WSSecurityInterceptor() {
27  super();
28  }
29 
30  public WSSecurityInterceptor(Map<String, Object> properties) {
31  super(properties);
32  //setBefore(MustUnderstandInterceptor.class.getName());
33 
34  }
35 
36  public void handleMessage(SoapMessage message) throws Fault {
37  try {
38  System.out.println("WSSecurityInterceptor-----------------");
39  boolean securityHeaderExists = false;
40  List<Header> headers = message.getHeaders();
41  for(Header head : headers){
42  if( head.getName().getLocalPart().equals("Security")){
43  securityHeaderExists = true;
44  break;
45  }
46 
47  }
48 
49  if( !securityHeaderExists){
50  return;
51  }
52 
53  super.handleMessage(message);
54 
55  } catch (Fault f) {
56  f.setMessage("Authentication failed");
57  f.setFaultCode(new QName("Client"));
58 
59  throw f;
60  } catch (Exception ex) {
61  Fault fault = new Fault(new Exception("Authentication failed"));
62  fault.setFaultCode(new QName("Client"));
63  throw fault;
64  }
65  }
66 
67  @Override
68  public void handleFault(SoapMessage message) {
69  //In case of error in intercepter chain, this will clear the authentication for this thread
70  //SecurityContextHolder.getContext().setAuthentication(null);
71  //.setSession(null);
72  super.handleFault(message);
73  }
74 
75 }
76 //\endcond