Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8496515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:52:34+00:00 2026-06-10T23:52:34+00:00

I need to incorporate an authentication header (i.e. as a part of SOAP header

  • 0

I need to incorporate an authentication header (i.e. as a part of SOAP header request) in my new web service. That authentication header will verify the userId and password details. I have to verify the content of request header details for authentication in my Web Service. If authenticated, then the SOAP body of the request will be processed, else Invalid Authentication message will be send back by the Web Service to the client application invoking the service.

I am not able to understand how to create a web service where the SOAP Header will contain some elements(in my case, authentication elements such as userId and password).

Normally, whatever method exposed in the service will come as a part of the SOAP Body. Hence confused how to proceed with adding authentication elements in the SOAP Header.

Please help

Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:52:35+00:00Added an answer on June 10, 2026 at 11:52 pm

    Recently I have wrote a class which adds user credentials to SOAP header. To do that you need to create a class which implements SOAPHandler<SOAPMessageContext> interface. For e.g.:

    public class MyHandler implements SOAPHandler<SOAPMessageContext> {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(MyHandler.class);
    
        private String username;
    
        private String password;
    
        /**
         * Handles SOAP message. If SOAP header does not already exist, then method will created new SOAP header. The
         * username and password is added to the header as the credentials to authenticate user. If no user credentials is
         * specified every call to web service will fail.
         *
         * @param context SOAP message context to get SOAP message from
         * @return true
         */
        @Override
        public boolean handleMessage(SOAPMessageContext context) {
            try {
                SOAPMessage message = context.getMessage();
                SOAPHeader header = message.getSOAPHeader();
                SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
                if (header == null) {
                    header = envelope.addHeader();
                }
                QName qNameUserCredentials = new QName("https://your.target.namespace/", "UserCredentials");
                SOAPHeaderElement userCredentials = header.addHeaderElement(qNameUserCredentials);
    
                QName qNameUsername = new QName("https://your.target.namespace/", "Username");
                SOAPHeaderElement username = header.addHeaderElement(qNameUsername );
                username.addTextNode(this.username);
                QName qNamePassword = new QName("https://your.target.namespace/", "Password");
                SOAPHeaderElement password = header.addHeaderElement(qNamePassword);
                password.addTextNode(this.password);
    
                userCredentials.addChildElement(username);
                userCredentials.addChildElement(password);
    
                message.saveChanges();
                //TODO: remove this writer when the testing is finished
                StringWriter writer = new StringWriter();
                message.writeTo(new StringOutputStream(writer));
                LOGGER.debug("SOAP message: \n" + writer.toString());
            } catch (SOAPException e) {
                LOGGER.error("Error occurred while adding credentials to SOAP header.", e);
            } catch (IOException e) {
                LOGGER.error("Error occurred while writing message to output stream.", e);
            }
            return true;
        }
    
        //TODO: remove this class after testing is finished
        private static class StringOutputStream extends OutputStream {
    
            private StringWriter writer;
    
            public StringOutputStream(StringWriter writer) {
                this.writer = writer;
            }
    
            @Override
            public void write(int b) throws IOException {
                writer.write(b);
            }
        }
    
        @Override
        public boolean handleFault(SOAPMessageContext context) {
            LOGGER.debug("handleFault has been invoked.");
            return true;
        }
    
        @Override
        public void close(MessageContext context) {
            LOGGER.debug("close has been invoked.");
        }
    
        @Override
        public Set<QName> getHeaders() {
            LOGGER.debug("getHeaders has been invoked.");
            return null;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    Note that I am just adding the credentials to the header and returning true. You do what ever you want with whole message and return false if something that is expected fails.

    I have implemented this one the client:

    <bean id="soapHandler" class="your.package.MyHandler">
        <property name="username" value="testUser"/>
        <property name="password" value="testPassword"/>
    </bean>
    
    <jaxws:client "...">
        <jaxws:handlers>
            <ref bean="soapHandler"/>
        </jaxws:handlers>
    </jaxws:client>
    

    But it also can be implemented on the endpoint.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I am working with a FatWire CS, and need to incorporate Web services.
I'm currently writing a program which will need to incorporate writing its output to
We are planning to incorporate a traffic reporting system into our web application that
I need to incorporate the printing of mailing labels in my web application. The
I'm building a java/spring application, and i may need to incorporate a stateful web
I'm about to start building a Rails app that will eventually need to vary
I'm trying configure Request-Header Authentication using spring 2.0 security, and I'm a complete newbie
I like uploadify but I need to incorporate a drag'n drop module for the
Need help with a query that I wrote: I have three tables Company id
We are using Hudson to automate our Android build. I need to incorporate the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.