Quiero añadir wsse: la seguridad a mi mensaje de jabón. Este es mi código:
public Document signSoapMessage(SOAPMessage message) {
try {
Document doc = message.getSOAPBody().getOwnerDocument();
Crypto crypto = CryptoFactory.getInstance(properties); //File
WSSecHeader secHeader = new WSSecHeader(doc);
secHeader.insertSecurityHeader();
InputStream inStream = new FileInputStream(properties.getProperty("org.apache.ws.security.crypto.merlin.keystore.file"));
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(inStream, properties.getProperty("privatekeypassword").toCharArray());
String alias = ks.aliases().nextElement();
X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
WSSecSignature sign = new WSSecSignature(secHeader);
sign.setX509Certificate(cert);
sign.setUserInfo(properties.getProperty("org.apache.ws.security.crypto.merlin.keystore.alias"), properties.getProperty("privatekeypassword"));
sign.setKeyIdentifierType(WSConstants.BST_DIRECT_REFERENCE); // Binary Security Token - SecurityTokenReference
sign.setUseSingleCertificate(true);
sign.setDigestAlgo(DigestMethod.SHA1);
//sign.build(crypto);
Document signedDoc = sign.build(crypto);
return signedDoc;
} catch (SOAPException e) {
e.printStackTrace();
return null;
} catch (WSSecurityException e) {
e.printStackTrace();
throw new RuntimeException("Error: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (CertificateException e) {
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
} catch (KeyStoreException e) {
e.printStackTrace();
return null;
}
}
Funciona para soapenv: Cuerpo (se le añade wsu: Id y xmlns: wsu parámetros)
Pero hay un elemento adicional en soapenv: Cabecera y no firma este elemento. No hay ninguna WSU: ID y xmlns: wsu parámetros y la falta de uno ds: Reference.
Ejemplo de msg jabón no firmado:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<!-- this element should be signed but is not - NOT WORKING -->
<something>
</something>
</soapenv:Header>
<!-- this element should be signed and It does. -->
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>
Comparo msg jabón de mi programa para trabajar msg jabón de proyecto SoapUI.
Cuando publico un mensaje al servicio web me sale un error: wsse:InvalidSecurity - Soap Header must be signed
. Mientras que en SoupUI que hace las obras.
Así que mi pregunta es ¿Cómo puedo forzar WSS4J a firmar adicional elemento dentro de soapenv: Cabecera ?
Ok, he resuelto el problema.
Normalmente este código debe trabajar en mi situación.
//strange static method from apache o.O
org.apache.xml.security.Init.init();
List<WSEncryptionPart> wsEncryptionParts = new ArrayList<>();
WSEncryptionPart somethingPart = new WSEncryptionPart("something","somethingNamespace","");
wsEncryptionParts.add(somethingPart);
sign.addReferencesToSign(wsEncryptionParts);
Sin embargo, no funciona. Siempre se produce una excepción:
org.apache.wss4j.common.ext.WSSecurityException: Ningún mensaje con ID "noXMLSig" encuentra en el paquete de recursos "org / apache / xml / seguridad / recurso / xmlsecurity". Excepción original era un mensaje y el mensaje org.apache.wss4j.common.ext.WSSecurityException No con ID "noEncElement" que se encuentra en el paquete de recursos "org / apache / xml / seguridad / recurso / xmlsecurity"
No pude encontrar una respuesta a lo que está mal con mi mensaje de jabón o código.
Sin embargo, después de un tiempo de depuración de org.apache.wss4j.dom.message.WSSecSignature . Sentí que algo está mal con la clase. Decidí modificar un método de construcción (Crypto cr) .
public Document build(Crypto cr) throws WSSecurityException {
LOG.debug("Beginning signing...");
this.prepare(cr);
if (this.getParts().isEmpty()) {
this.getParts().add(WSSecurityUtil.getDefaultEncryptionPart(this.getDocument()));
// --- Here is my edit - And it works!
WSEncryptionPart aaa = new WSEncryptionPart("something","somethingNamespace","");
this.getParts().add(aaa);
// ----------------------------------
} else {
Iterator var2 = this.getParts().iterator();
label33:
while(true) {
while(true) {
if (!var2.hasNext()) {
break label33;
}
WSEncryptionPart part = (WSEncryptionPart)var2.next();
if (part.getId() == null && "STRTransform".equals(part.getName())) {
part.setId(this.strUri);
} else if ("KeyInfo".equals(part.getName()) && "http://www.w3.org/2000/09/xmldsig#".equals(part.getNamespace()) && part.getElement() == null) {
Element keyInfoElement = this.getKeyInfoElement();
part.setElement(keyInfoElement);
}
}
}
}
List<javax.xml.crypto.dsig.Reference> referenceList = this.addReferencesToSign(this.getParts());
this.computeSignature(referenceList);
if (this.bstToken != null) {
this.prependBSTElementToHeader();
}
return this.getDocument();
}
Por supuesto, la solución es bastante débil. Sin embargo, al menos funciona ahora.
El problema existe en la versión más reciente:
WSS4J-ws-security-dom 2.2.2
WSS4J-ws-security-común 2.2.2