WSS4J WSSecUsernameToken problem

Posted on 2007-08-08
Last Modified: 2013-11-05
I'm having trouble adding a Username Token header to an existing SOAP message. My code looks like this:

            MessageFactory mf = MessageFactory.newInstance();
            SOAPMessage sm = mf.createMessage();
            SOAPPart soappart = sm.getSOAPPart();
            SOAPEnvelope se = soappart.getEnvelope();
            SOAPHeader header = se.getHeader();
            String username = "wss4j";
            String password = "security";        
            WSSecHeader wsheader = new WSSecHeader();
            WSSecUsernameToken  token = new WSSecUsernameToken();
            token.setUserInfo(username, password);
  , wsheader);

What it produces is this:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="" xmlns:xsd="" xmlns:xsi="">
    <wsse:Security xmlns:wsse="" soapenv:mustUnderstand="0"/>
    <soapenv:client-session-id soapenv:actor="" soapenv:mustUnderstand="0">876351D116F8A209E609BAA99683CF71</soapenv:client-session-id>

As you can see, the wsse:Security element does not contain the Username Token as it should. What I'm trying to produce is something like this:

<wsse:Security xmlns:wsse="" soapenv:actor="test" soapenv:mustUnderstand="1">
  <wsse:UsernameToken xmlns:wsu="" wsu:Id="UsernameToken-32699917">
    <wsse:Password Type="">this is a lot of foobar </wsse:Password>

A co-worker ran the code and it produced the right SOAP. But when I run it, the Username Token isn't there.
Question by:FeralCTO
    LVL 23

    Accepted Solution

    Are you using the article  link

    check the sample code or Listing 2 thats what you are suppose to do.

    Any way just try this

    Replace the following lines
    WSSecUsernameToken  token = new WSSecUsernameToken();
                token.setUserInfo(username, password);
      , wsheader);


    WSSAddUsernameToken token=  new WSSAddUsernameToken("", false);
    //as your example shows password is text.
    token.setPasswordType(WSConstants.PASSWORD_TEXT);, username, password);

    Actually i don't see any major mistake in your code. any way try the above code and let me know whats the result.

    LVL 1

    Author Comment

    I get the same result. It's odd. I can step through code, into the wss4j source, and see that the Username Token gets created and looks the way it should. I can even see it get added to the security header. But once I come out of that code and back into mine, the security header doesn't contain the Username Token.
    LVL 1

    Author Comment

    I got this working by moving the Username Token code further down to just before the method is invoked. I don't know why that made a difference, but it did.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now