Sergio perez
Sergio perez

Reputation: 1

Consume JaxWs API SOAP with Security inside header section

Im trying to consume a soap api, more specifically the api of job_requisition. The type of authentication that this api has is through the header

The problem I have is that I generate the header correctly through my services, comparing it with the previous request it is exactly the same and replacing it in the soapUI request it works correctly.But when I send the request I get the following error: javax.xml.ws.soap.SOAPFaultException: class java.lang.String cannot be cast to class org.w3c.dom.Element (java.lang.String is in module java.base of loader 'bootstrap'; org.w3c.dom.Element is in module java.xml of loader 'bootstrap')

Dependencies

SoapClient class

@Configuration
public class soapClient {
    
    private final String USERNAME = "JhonDoe";
    private final String PASSWORD = "JhonDoePass";

    @Bean
    public ReportPort reportPort() throws ParserConfigurationException, JAXBException {
    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(ReportPort.class);
        factory.setAddress(ENDPOINT);
        
        List<Interceptor<? extends Message>> interceptors = new ArrayList<>();
        interceptors.add(new SoapHeaderInterceptor());
        ReportPort reportPort = (ReportPort) factory.create();
        
        ClientProxy.getClient(reportPort).getOutInterceptors().addAll(interceptors);
        
        return reportPort;
    }
    
    private class SoapHeaderInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

        public SoapHeaderInterceptor() {
            super(Phase.WRITE);
        }

        @Override
        public void handleMessage(SoapMessage message) {
            SoapHeader securityHeader = new SoapHeader(new QName("http://schemas.xmlsoap.org/ws/2002/07/secext", "Security"), getSecurityToken());
            securityHeader.setMustUnderstand(true);
            message.getHeaders().add(securityHeader);
        }
        
        private String getSecurityToken() {
            
            String securityToken = "<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" " +
                    "xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" +
                    "<wsse:UsernameToken wsu:Id=\"UsernameToken-3AB8D6EE3A619760EB17114621999163\">" +
                    "<wsse:Username>" + StringEscapeUtils.escapeXml10(USERNAME) + "</wsse:Username>" +
                    "<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">" + StringEscapeUtils.escapeXml10(PASSWORD) + "</wsse:Password>" +
                    "</wsse:UsernameToken>" +
                    "</wsse:Security>";

            return securityToken;
        }
    }
}

SoapTest class

@SpringBootTest
public class soapTest {

    @Autowired
    private ReportPort reportPort;

    @Test
    void soapClientTest() {
        try {
            ReportParametersType parameters = new ReportParametersType();
            List<ReportEntryType> test = reportPort.executeReport(parameters, null);
            System.out.println("estamos aqui");
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }

    }
}

The idea is to add to the request the authentication header through an interceptor.I have debugged this functionality and it generates the same header.

I dont know if the error is related with a conflict between dependencies, or the JaxWsProxyFactoryBean need extra configuration or something.

I would appreciate help in this topic, I have searched for information about it and I have hardly found anything.

I have tried many ways to make the soap request and none of them worked when adding the authentication header. I hope to get the solution to this bug

Upvotes: 0

Views: 37

Answers (0)

Related Questions