IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Services Web Java Discussion :

WSS4J securite The prefix "ds" for element "ds:KeyInfo" is not bound.


Sujet :

Services Web Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    1 716
    Détails du profil
    Informations personnelles :
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 716
    Par défaut WSS4J securite The prefix "ds" for element "ds:KeyInfo" is not bound.
    Bonjour,

    Je regarde actuellement des pb de securite avec WSS4J.
    J'ai le message d'erreur suivant
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     by: org.xml.sax.SAXParseException: The prefix "ds" for element "ds:KeyInfo" is not bound.
    Que me manque t il ?

    Merci d'avance
    Philippe

  2. #2
    Membre éprouvé
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 141
    Par défaut
    Hello,

    ds est le préfixe concernant les informations de signature.
    Les informations concernant le certificat sont elles présentes dans le message ?

    UbuntuMan

  3. #3
    Membre éprouvé
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    1 716
    Détails du profil
    Informations personnelles :
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 716
    Par défaut
    Bonjour,

    L'exemple dont je pars est WSSecuritySample.java de l'article http://www.devx.com/Java/Article/28816/1963
    Cet exemple met en oeuvre tous les aspects de la securité que ce soit mot de passe ou cryptage d'enveloppe.
    J'ai dans mon repertoire :
    - un fichier crypto.properties qui fait reference a un certificat.
    - Le certificat est bien present phyisiquement également
    - J'ai rajouté pour la sécurité le "US_export_policy.jar" dans le repertoire sécurity.

    Mais la je seche ?

    Voici le code de l'article en question
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
     
    package com.jeffhanson.ws.security;
     
    import org.apache.axis.Message;
    import org.apache.axis.MessageContext;
    import org.apache.axis.SOAPPart;
    import org.apache.axis.client.AxisClient;
    import org.apache.axis.configuration.NullProvider;
    import org.apache.axis.message.SOAPEnvelope;
    import org.apache.axis.utils.XMLUtils;
    import org.apache.ws.axis.security.util.AxisUtil;
    import org.apache.ws.security.components.crypto.Crypto;
    import org.apache.ws.security.components.crypto.CryptoFactory;
    import org.apache.ws.security.message.WSSignEnvelope;
    import org.apache.ws.security.message.WSEncryptBody;
    import org.apache.ws.security.message.WSSAddUsernameToken;
    import org.apache.ws.security.message.token.SecurityTokenReference;
    import org.apache.ws.security.message.token.Reference;
    import org.apache.ws.security.WSSecurityEngine;
    import org.apache.ws.security.WSConstants;
    import org.apache.ws.security.WSSConfig;
    import org.apache.ws.security.util.WSSecurityUtil;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
     
    import java.io.ByteArrayInputStream;
    import java.io.InputStream;
    import java.io.PrintWriter;
     
    /**
     * Enter description here.
     *
     * @author <a href="mailto:jeff@jeffhanson.com">Jeff Hanson</a>
     * @version $Revision: 1.1 $
     *          <p/>
     *          <p><b>Revisions:</b>
     *          <p/>
     *          <p><b>Jul 26, 2005 jhanson:</b>
     *          <ul>
     *          <li> Created file.
     *          </ul>
     */
     
    public class WSSecuritySample
    {
       private static final String soapMsg =
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
          "<SOAP-ENV:Envelope" +
          "   xmlns:SOAP-ENV=\"http://www.w3.org/2003/05/soap-envelope\"\n" +
          "   xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
          "   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
          "   <SOAP-ENV:Body>" +
          "      <sayHello xmlns=\"http://jeffhanson.com/services/helloworld\">" +
          "         <value xmlns=\"\">Hello world!</value>" +
          "      </sayHello>" +
          "   </SOAP-ENV:Body>" +
          "</SOAP-ENV:Envelope>";
     
       // The following initializes the security engine to the
       // default WS-Security settings
       private static final WSSecurityEngine secEngine =
          new WSSecurityEngine();
     
       // The following creates a crypto provider according to the
       // class name specified by the system property:
       //    org.apache.ws.security.crypto.provider
       //
       // If the provider property is not set, the default class,
       // org.apache.ws.security.components.crypto.BouncyCastle, is
       // used.
       //
       // The provider is initialized to the values specified in
       // the crypto.properties file. The crypto.properties file
       // found in the wss4j jar file specifies
       //    org.apache.ws.security.components.crypto.Merlin
       // as the provider class.
       private static final Crypto crypto =
          CryptoFactory.getInstance();
     
       private AxisClient engine = null;
       private MessageContext msgContext = null;
     
       /**
        * Main method
        */
       public static void main(String[] args)
       {
          try
          {
             WSSecuritySample app = new WSSecuritySample();
     
             Message axisMessage = app.getAxisMessage(soapMsg);
             SOAPEnvelope unsignedEnvelope = axisMessage.getSOAPEnvelope();
     
             System.out.println("<<<<<< Unsigned and Unencrypted >>>>>>");
             XMLUtils.PrettyElementToWriter(unsignedEnvelope.getAsDOM(),
                                            new PrintWriter(System.out));
    /*
             Message samlMsg = app.addUserTokens(unsignedEnvelope);
             System.out.println("\n<<<<<< User Tokens >>>>>>");
             XMLUtils.PrettyElementToWriter(samlMsg.getSOAPEnvelope().getAsDOM(),
                                            new PrintWriter(System.out));
     
             Message encryptedMsg = app.encryptSOAPEnvelope(unsignedEnvelope,
                                                            axisMessage);
             System.out.println("\n<<<<<< Encrypted >>>>>>");
             XMLUtils.PrettyElementToWriter(encryptedMsg.getSOAPEnvelope().getAsDOM(),
                                            new PrintWriter(System.out));
    */
             Message signedMsg = app.signSOAPEnvelope(unsignedEnvelope);
             System.out.println("\n<<<<<< Signed >>>>>>");
             XMLUtils.PrettyElementToWriter(signedMsg.getSOAPEnvelope().getAsDOM(),
                                            new PrintWriter(System.out));                                       
          }
          catch (Exception e)
          {
             e.printStackTrace();
          }
       }
     
       /**
        * WSSecuritySample constructor
        */
       public WSSecuritySample()
       {
          engine = new AxisClient(new NullProvider());
          msgContext = new MessageContext(engine);
       }
     
       /**
        * Creates and returns an Axis message from a
        * SOAP envelope string.
        *
        * @param unsignedEnvelope   a string containing a SOAP
        *                           envelope
        * @return <code>Message</code>   the Axis message
        */
       private Message getAxisMessage(String unsignedEnvelope)
       {
          InputStream inStream =
             new ByteArrayInputStream(unsignedEnvelope.getBytes());
          Message axisMessage = new Message(inStream);
          axisMessage.setMessageContext(msgContext);
          return axisMessage;
       }
     
       /**
        * Creates a signed SOAP message in compliance with WS-Security.
        *
        * @return <code>Message</code>  the signed SOAP envelope
        *         as an Axis message
        * @throws Exception on error
        */
       public Message signSOAPEnvelope(SOAPEnvelope unsignedEnvelope)
          throws Exception
       {
          // WSSignEnvelope signs a SOAP envelope according to the
          // WS Specification (X509 profile) and adds the signature data
          // to the envelope.
          WSSignEnvelope signer = new WSSignEnvelope();
     
          String alias = "16c73ab6-b892-458f-abf5-2f875f74882e";
          String password = "security";
          signer.setUserInfo(alias, password);
     
          Document doc = unsignedEnvelope.getAsDocument();
     
          // The "build" method, creates the signed SOAP envelope.
          // It takes a SOAP Envelope as a W3C Document and adds
          // a WSS Signature header to it. The signed elements
          // depend on the signature parts that are specified by
          // the WSBaseMessage.setParts(java.util.Vector parts)
          // method. By default, SOAP Body is signed.
          // The "crypto" parameter is the object that implements
          // access to the keystore and handling of certificates.
          // A default implementation is included:
          //    org.apache.ws.security.components.crypto.Merlin
          Document signedDoc = signer.build(doc, crypto);
     
          // Convert the signed document into a SOAP message.
          Message signedSOAPMsg =
             (org.apache.axis.Message)AxisUtil.toSOAPMessage(signedDoc);
     
          return signedSOAPMsg;
       }
     
       /**
        * Adds user tokens to a SOAP envelope in compliance with WS-Security.
        *
        * @return <code>Message</code>  the signed SOAP envelope
        *         as an Axis message
        * @throws Exception on error
        */
       public Message addUserTokens(SOAPEnvelope unsignedEnvelope)
          throws Exception
       {
          WSEncryptBody wsEncrypt = new WSEncryptBody();
     
          // Get the message as document
          Document doc = unsignedEnvelope.getAsDocument();
     
          String username = "joedoe";
          String password = "this is a lot of foobar ";
          byte[] key = password.getBytes();
     
          // Add the UserNameToken.
          WSSAddUsernameToken builder =
             new WSSAddUsernameToken("", false);
          builder.setPasswordType(WSConstants.PASSWORD_TEXT);
          builder.build(doc, username, password);
     
          // Add an Id to it.
          Element usrEle =
              (Element)(doc.getElementsByTagNameNS(WSConstants.WSSE_NS,
                                                   "UsernameToken").item(0));
          String idValue = "7654";
          usrEle.setAttribute("Id", idValue);
     
          // Create a Reference to the UserNameToken.
          Reference ref = new Reference(WSSConfig.getDefaultWSConfig(),
                                        doc);
          ref.setURI("#" + idValue);
          ref.setValueType("UsernameToken");
          SecurityTokenReference secRef =
              new SecurityTokenReference(WSSConfig.getDefaultWSConfig(),
                                         doc);
          secRef.setReference(ref);
     
          // adding the namespace
          WSSecurityUtil.setNamespace(secRef.getElement(),
                                      WSConstants.WSSE_NS,
                                      WSConstants.WSSE_PREFIX);
     
          // Setting necessary parameters in WSEncryptBody.
          wsEncrypt.setKeyIdentifierType(WSConstants.EMBED_SECURITY_TOKEN_REF);
          wsEncrypt.setSecurityTokenReference(secRef);
          wsEncrypt.setKey(key);
     
          // Encrypt using the using the key
          Document encDoc = wsEncrypt.build(doc, crypto);
     
          // Convert the document into a SOAP message.
          Message signedMsg =
             (Message)AxisUtil.toSOAPMessage(encDoc);
     
          return signedMsg;
       }
     
       /**
        * Encrypts a SOAP envelope in compliance with WS-Security.
        *
        * @return <code>Message</code>  the signed SOAP envelope
        *         as an Axis message
        * @throws Exception on error
        */
       public Message encryptSOAPEnvelope(SOAPEnvelope unsignedEnvelope,
                                          Message axisMessage)
          throws Exception
       {
          WSEncryptBody encrypt = new WSEncryptBody();
          encrypt.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e");
     
          // Before Encryption
          Document doc = unsignedEnvelope.getAsDocument();
          Document encryptedDoc = encrypt.build(doc, crypto);
     
          // Convert the document into a SOAP message.
          Message encryptedMsg = (Message) AxisUtil.toSOAPMessage(encryptedDoc);
          String soapPart = encryptedMsg.getSOAPPartAsString();
          ((SOAPPart)axisMessage.getSOAPPart()).setCurrentMessage(soapPart,
                                                                  SOAPPart.FORM_STRING);
     
          encryptedDoc = axisMessage.getSOAPEnvelope().getAsDocument();
     
          // Convert the document into a SOAP message.
          Message encryptedSOAPMsg =
             (Message)AxisUtil.toSOAPMessage(encryptedDoc);
     
          return encryptedSOAPMsg;
       }
    }
    Merci d'avance
    Philippe

  4. #4
    Membre éprouvé
    Profil pro
    Inscrit en
    Décembre 2009
    Messages
    141
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2009
    Messages : 141
    Par défaut
    J'ai pas voulu m'enregistrer sur le site, surtout qu'à priori l'article n'est pas récent. C'est toujours le problème avec ces articles dépassés qui utilisent des bibliothèques qui datent.
    Généralement c'est du temps perdu et pire ça peut être source de confusion.

    Le mieux c'est quand même d'utiliser un framework qui prend en charge WS-Security
    genre Apache CXF. Bon tout ne fonctionne pas : les assertion SAML par exemple le support
    est partiel voir "expérimental" pour les v2. Pour le UserName Token je ne sais pas faut tester
    mais ça semble bon : y'a 3 profiles de supportés (plain text, digest et none).

    Après y'a parait-il Metro qui semble plus en avance là dessus. Faudrait que je m'y mette
    mais les produits Sun ça ne m'attire plus trop.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. The method createStatement() is undefined for the type Connection
    Par Taoufik109 dans le forum Plateformes (Java EE, Jakarta EE, Spring) et Serveurs
    Réponses: 2
    Dernier message: 21/01/2010, 22h00
  2. Réponses: 0
    Dernier message: 06/03/2009, 15h44
  3. Réponses: 8
    Dernier message: 27/11/2008, 19h40
  4. Réponses: 8
    Dernier message: 19/07/2004, 14h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo