I already know two ways to do this. One of them is to create a SOAP handler and register it as a JAX-WS handler in the Spring configuration.
Check out my answer here how to create a SOAP handler. Since you want the header to appear in the response (outgoing request), do not forget that you need to check if the message is outgoing, something like this:
Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outbound) {
Another, maybe easier way. Place the header directly in the context of the CXF response. Please note that this example is just a proof of concept, I donβt know the actual situation where you need the credentials in the answer. It will show how to add a user credential object to the header, you should change it depending on your needs.
private void modifyResponse(String username, String password) { UserCredentials authHeader = new UserCredentials(); authHeader.setUsername(username); authHeader.setPassword(password); ArrayList<Header> headers = new ArrayList<Header>(1); try { Header soapHeader = new Header( new QName("http://yournamespaceuri.com/something", "UserCredentials"), authHeader, new JAXBDataBinding(UserCredentials.class)); headers.add(soapHeader); } catch (JAXBException ex) { LOGGER.error("Exception trying to serialize header: {}", ex); } ((BindingProvider) proxy).getResponseContext().put(Header.HEADER_LIST, headers); }
This method must be called immediately after the request of your client.
source share