SQL - Generar wsse:Security en SOAPHeaderElement con JAVA

 
Vista:
sin imagen de perfil
Val: 10
Ha aumentado su posición en 8 puestos en SQL (en relación al último mes)
Gráfica de SQL

Generar wsse:Security en SOAPHeaderElement con JAVA

Publicado por Jesus (6 intervenciones) el 27/11/2019 14:34:28
Buenas

Tengo que generar la siguiente cabecera de SOAP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:mustUnderstand="1">
 
    <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-a5c74fab-1942-472b-9fbc-14b34b4b574a">
 
        <wsse:Username>APP-DCTPNSE-CAN-CR1</wsse:Username>
 
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">NgEsD3WVOn</wsse:Password>
 
        <wsse:Nonce>xmPk+9d5IB6z451tPdJ845UZSolA2L</wsse:Nonce>
 
        <wsu:Created>2019-09-20T09:38:15Z</wsu:Created>
 
    </wsse:UsernameToken>
 
</wsse:Security>



Utilizo el siguiente codigo (una Clase que llama tiene que recibir el tipo de dato SOAPHeaderElement):

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
public static SOAPHeaderElement generateHeaderBlock(String user, String password){
    System.out.println("Se entra en el metodo generateheaderBlock");
    SOAPHeaderElement security = null;
 
 
    // Defines some namespace and URL constants
    String WSSE = "wsse";
    String WSU = "wsu";
    String XMLNS = "xmlns";
    String WS_SEC_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
    String WS_UTI_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";
    String WS_SOAP_URL = "http://schemas.xmlsoap.org/soap/envelope/";
 
    try {
 
        System.out.println("Entramos en el try de generateheaderBlock");
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
        SOAPPart sOAPPart = soapMessage.getSOAPPart();
        SOAPEnvelope envelope = sOAPPart.getEnvelope();
 
        QName headerSecurity = new QName(WS_SEC_NS, "Security");
         AtomicReference<SOAPHeaderElement> header
            = new AtomicReference<SOAPHeaderElement>
                (new SOAPHeaderElement(headerSecurity) {
                   /**
                     * 
                     */
                    private static final long serialVersionUID = 6246594829237893452L;
                {
                    String uuid = UUID.randomUUID().toString();
                    System.out.println("uuid: " + uuid);
 
                    Name userTokenElementName = envelope.createName("UsernameToken", WSSE, WS_UTI_NS);
                    SOAPElement userNameToken = addChildElement(userTokenElementName);
                    userNameToken.removeNamespaceDeclaration(XMLNS);
                    userNameToken.addNamespaceDeclaration(WSU + ":Id", "SecurityToken-" + uuid);
                    System.out.println("userNameToken creado: " + userNameToken.toString());
 
                    Name userNameName = envelope.createName("Username", WSSE, WS_UTI_NS);
                    SOAPElement userName = userNameToken.addChildElement(userNameName);
                    userName.removeNamespaceDeclaration(WSSE);
                    userName.addTextNode(user);
                    System.out.println("userName creado: " + userName.toString());
 
                    Name contrasenaName = envelope.createName("Password", WSSE, WS_UTI_NS);
                    SOAPElement contrasena = userNameToken.addChildElement(contrasenaName);
                    contrasena.addTextNode(password);
                    contrasena.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
                    System.out.println("password creado: " + contrasena.toString());
 
                    Name codigoGeneradoName = envelope.createName("Nonce", WSSE, WS_UTI_NS);
                    String codigoGenerado = new String(Base64.encode((uuid + "_" + (System.currentTimeMillis() / 1000)).getBytes()));
                    SOAPElement nonce = userNameToken.addChildElement(codigoGeneradoName);
                    nonce.removeNamespaceDeclaration(WSSE);
                    nonce.addTextNode(codigoGenerado);
                    System.out.println("nonce creado: " + nonce.toString());
 
                    String fechaCreacion = String.valueOf(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(new Date()));
                    Name createdName = envelope.createName("Created", WSU, WS_UTI_NS);
                    SOAPElement created = userNameToken.addChildElement("Created", "wsse");
                    //created.removeNamespaceDeclaration(WSSE);
                    created.removeContents();
                    created.setPrefix(WSU);
                    created.addTextNode(fechaCreacion);
                    System.out.println("fecha creada: " + created.toString());
                   }
                   @Override
                   public void setAttribute(String namespace, String localName, String value) {
                     if (!Constants.ATTR_MUST_UNDERSTAND.equals(localName)) {  //Or any other attribute name you'd want to avoid
                       super.setAttribute(namespace, localName, value);
                     }
                   }
                });
        security = header.get();
        security.setPrefix(WSSE);
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        security.addAttribute(soapFactory.createName("soap:mustUnderstand"),"1");
        Name segundaParteSeg  = envelope.createName("xmlns:soap");
        security.addAttribute(segundaParteSeg, WS_SOAP_URL);
        security.setActor(null);
        security.setMustUnderstand(true);
        System.out.println("security completada: " + security.toString());
 
    } catch (SOAPException e) {
        // TODO Auto-generated catch block
        System.out.println("SOAPException. Fallo en el metodo generateHeaderBlock, Motivo: " + e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception. Fallo en el metodo generateHeaderBlock, Motivo: " + e.getMessage());
        e.printStackTrace();
    }
 
    System.out.println("Se sale en el metodo generateHeaderBlock con exito");
    return security;
}

Pero a la hora de ejecutar se forma lo siguiente:

1
2
3
4
5
6
7
8
<wsse:Security soap:mustUnderstand="1" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsu:Id="SecurityToken-5b047bce-1ac2-4e5d-bed0-5639706f8e93">
        <wsse:Username>APP-DCTPNSE-CAN-CR1</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">NgEsD3WVOn</wsse:Password>
        <wsse:Nonce>NWIwNDdiY2UtMWFjMi00ZTVkLWJlZDAtNTYzOTcwNmY4ZTkzXzE1NzQ2NzQyNzA=</wsse:Nonce>
        <wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2019-11-25T10:31:10Z</wsu:Created>
    </wsse:UsernameToken>
</wsse:Security>

Me gustaría saber cómo deber desarrollar el código para que el NODO "UsernameToken" para que me aparezca "xmlns:wsu" en lugar de "xmlns:wsse". También me gustaría saber como dejar el Nodo "Created" solo con el valor de la fecha actual ya que me aparece con el atributo "xmlns:wsu". Tambien que el Nodo padre "Security" aparezca los atributos en orden de como quiero que aparezca en el cabecera que pongo primero

Cualquier duda o aclaración, decidmelo y lo reviso.

Muchas gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder