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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
public class SecurityHandler implements SOAPHandler<SOAPMessageContext>{
@Override
public boolean handleMessage(SOAPMessageContext context) {
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
SOAPMessage message = context.getMessage();
try {
SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
envelope.addNamespaceDeclaration("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
SOAPHeader header = envelope.addHeader();
SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
security.addNamespaceDeclaration("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
security.addNamespaceDeclaration("wsa", "http://www.w3.org/2005/08/addressing");
security.addNamespaceDeclaration("ds", "http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd");
/**
* 1er Niveau USERNAMETOKEN !!
*/
SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
usernameToken.addAttribute(new QName("wsu:Id"), "USER-ID");
SOAPElement username = usernameToken.addChildElement("Username", "wsse");
/**
* Le token !!
*/
username.addTextNode("TOKEN");
SOAPElement password = usernameToken.addChildElement("Password", "wsse");
password.setAttribute("Type", "wsse:PasswordDigest");
/**
* The Password
element is filled according to the OASIS rules. It contains a Base64 encoded value of
the SHA-1 hash of the string composed as Base64 decoded value of nonce + created (timestamp)
+ realPassword. See OASIS documentation for details.
*/
String nonceString = createNonce(30);
String timestamp = createSoapTimestamp();
String pass = "opif";
byte[] tmp = java.security.MessageDigest.getInstance("SHA1").digest((nonceString + timestamp + pass).getBytes());
password.addTextNode(new sun.misc.BASE64Encoder().encode(tmp));
SOAPElement nonce = usernameToken.addChildElement("Nonce", "wsse");
nonce.setAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
nonce.addTextNode(new sun.misc.BASE64Encoder().encode(nonceString.getBytes()));
SOAPElement created = usernameToken.addChildElement("Created", "wsu");
created.addTextNode(timestamp);
/**
* 2eme Niveau : SIGNATURE, Comment la générer ?
*/
//Print out the outbound SOAP message to System.out
message.writeTo(System.out);
System.out.println("");
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
//This handler does nothing with the response from the Web Service so
//we just print out the SOAP message.
SOAPMessage message = context.getMessage();
message.writeTo(System.out);
System.out.println("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
return outboundProperty;
}
public static String createSoapTimestamp()
{
String fmt = "yyyy-MM-dd'T'HH:mm:ss.000'Z'";
SimpleDateFormat df = new SimpleDateFormat(fmt, Locale.US);
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String date = df.format(new Date());
return date;
}
public static String createNonce(int length)
{
final String CHARS = "01234567890abcdefghijkmlmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder();
for(int i=0; i < length; i++)
sb.append(CHARS.charAt((int)(Math.random()*CHARS.length())));
return sb.toString();
}
@Override
public boolean handleFault(SOAPMessageContext context) {
System.out.println("Client : handleFault()......");
return true;
}
@Override
public void close(MessageContext context) {
System.out.println("Client : close()......");
}
@Override
public Set<QName> getHeaders() {
System.out.println("Client : getHeaders()......");
return null;
}
} |