Search code examples
javaspring-bootsoapjax-rswebservice-client

How to properly add HTTP header in WebService Client (Spring Web Services)


Does anyone know how to properly add HTTP headers in a Soap Web Service client generated by jaxws from a wsdl file?

I can access the services from SoapUI, but when I try to access them from my Spring project, I keep getting 403 response.

Below is the request details from SoapUI interface:

POST https://WEB_SERVICE_URL HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
AUTH_HEADER: AUTH_HEADER_VALUE
Content-Length: 224
Host: WEB_SERVICE_HOST
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.5 (Java/16.0.2)

SoapUI raw response:

HTTP/1.1 200 OK
Content-Type: text/xml;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
server: Apache-Coyote/1.1
access-control-allow-origin: https://pje.trt13.jus.br
access-control-allow-headers: Authorization, Content-Type, x-xsrf-token
access-control-allow-credentials: true
set-cookie: JSESSIONID=08a467eac8b5a940~3Gv3nJT+VOBPIT-lNSphT3lO; Path=/primeirograu; Secure
date: Thu, 12 Dec 2024 19:22:03 GMT
strict-transport-security: max-age=15768000; includeSubDomains; preload
cache-control: public, no-transform
content-security-policy: frame-ancestors 'self' jte.csjt.jus.br jte.trt5.jus.br *.jte.trt5.jus.br
x-content-type-options: nosniff
referrer-policy: no-referrer-when-downgrade
feature-policy: camera 'none'; microphone 'none'
access-control-expose-headers: Date
vary: Accept-Encoding
X-Cache: Miss from cloudfront
Via: 1.1 f9bea17c7a13cd7f1b28e1a7cb598810.cloudfront.net (CloudFront)
X-Amz-Cf-Pop: FOR50-P3
X-Amz-Cf-Id: bXKBzXIxFnWPmG2Fa3U5GMQ9TquiYguIJyGRCBzTiMt0Sih2pl6TvQ==

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns2:consultarJurisdicoesResponse xmlns:ns2="http://ws.pje.cnj.jus.br/">
            <return><descricao>Santa Rita</descricao><id>41</id></return>
            <return><descricao>Itaporanga</descricao><id>46</id></return>
            <return><descricao>Guarabira</descricao><id>48</id></return>
            <return><descricao>Catolé do Rocha</descricao><id>49</id></return>
            <return><descricao>Patos</descricao><id>50</id></return>
            <return><descricao>Sousa</descricao><id>51</id></return>
            <return><descricao>Campina Grande</descricao><id>55</id></return>
            <return><descricao>João Pessoa</descricao><id>44</id></return>
        </ns2:consultarJurisdicoesResponse>
    </soap:Body>
</soap:Envelope>

Here is my client factory:

@Component
public class TRT13Client {

    private ConsultaPJeService222 consultaPJeTrtService = new ConsultaPJeService222();

    public ConsultaPJe222 getConsultaPJeTrtService() {
        ConsultaPJe222 consultaPJeTRT = consultaPJeTrtService.getConsultaPJePort();

        Map<String, List<String>> requestHeaders = new HashMap<>();
        requestHeaders.put(AUTH_HEADER, List.of(AUTH_HEADER_VALUE));

        BindingProvider bindingProvider = (BindingProvider) consultaPJeTRT;
        bindingProvider.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestHeaders);

        return consultaPJeTRT;
    }
}

And here is the Controller where I inject the client:

@RestController
@RequestMapping("trt13/jurisdicao")
@RequiredArgsConstructor
public class JurisdicaoTRT13Controller {

    private final TRT13Client trt13Client;

    @GetMapping
    public ResponseEntity<List<Jurisdicao>> buscarJurisdicoes() {
        return ResponseEntity.ok(trt13Client.getConsultaPJeTrtService().consultarJurisdicoes());
    }

}

I found this solution in this SO post: java web service client, adding http headers but for some reason it seems not to be working. I've also tried a couple of solutions similar to this one, but had success in none of them.

All types, including ConsultaPJeService222 were generated by jaxws wsimport task.

Does anyone know what am I doing wrong?


Solution

  • To everyone that maybe comes to this post, it turned out that the header was being properly added. The whole problem was that the web service that I was trying to access doesn't accept some specific user agents, such as: JAX-WS RI 3.0.0 git-revision#af8101a which was the one from the client generated by JAX-WS lib, and PostmanRuntime/7.35.0.

    But the server accepts SoapUI UserAgent: Apache-HttpClient/4.5.5 (Java/16.0.2).

    I would never imagine that the server had this restriction, I only figured it out by searching in the HTTP request logs.