如何在WebService客户端(Spring Web Services)中正确添加HTTP header

问题描述 投票:0回答:1

有谁知道如何在 jaxwswsdl 文件生成的 Soap Web 服务客户端中正确添加 HTTP 标头

我可以从 SoapUI 访问服务,但是当我尝试从 Spring 项目访问它们时,我不断收到 403 响应。

以下是SoapUI接口的请求详情:

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 原始响应:

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>

这是我的客户工厂

@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;
    }
}

这是我注入客户端的控制器:

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

    private final TRT13Client trt13Client;

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

}

我在这篇SO帖子中找到了这个解决方案:java web服务客户端,添加http标头但由于某种原因它似乎不起作用。我还尝试了几种与此类似的解决方案,但没有一个成功。

所有类型,包括 ConsultaPJeService222 均由 jaxws wsimport 任务生成。

有人知道我做错了什么吗?

java spring-boot soap jax-rs webservice-client
1个回答
0
投票

对于可能看到这篇文章的每个人来说,事实证明标题已正确添加。 整个问题是我尝试访问的 Web 服务不接受某些特定的用户代理,例如:JAX-WS RI 3.0.0 git-revision#af8101a,这是客户端生成的通过 JAX-WS lib 和 PostmanRuntime/7.35.0

但是服务器接受 SoapUI UserAgent:Apache-HttpClient/4.5.5 (Java/16.0.2).

我从来没有想到服务器有这个限制,我只是在 HTTP 请求日志中搜索才发现的。

© www.soinside.com 2019 - 2024. All rights reserved.