如何在 Spring-ws 端点中访问 HTTP 标头?

发布于 2024-09-28 16:36:42 字数 550 浏览 3 评论 0原文

如何在 Spring-ws 端点中访问 HTTP 标头?

我的代码如下所示:

public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
  protected Object invokeInternal(Object arg) throws Exception {
      MyReq request = (MyReq) arg;
      // need to access some HTTP headers here
      return createMyResp();
  }
}

invokeInternal() 仅获取未编组的 JAXB 对象作为参数。如何访问 invokeInternal() 中请求附带的 HTTP 标头?

一种可能有效的方法是创建一个 Servlet 过滤器,将标头值存储到 ThreadLocal 变量,然后在 invokeInternal() 内部访问该变量,但是有没有更好、更弹簧的方法-类似的方法吗?

How can I access HTTP headers in Spring-ws endpoint?

My code looks like this:

public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
  protected Object invokeInternal(Object arg) throws Exception {
      MyReq request = (MyReq) arg;
      // need to access some HTTP headers here
      return createMyResp();
  }
}

invokeInternal() gets only the unmarshalled JAXB object as the parameter. How can I access HTTP headers that came with the request inside invokeInternal()?

One way that would probably work is to create a Servlet filter that stores header values to ThreadLocal variable that is then accessed inside invokeInternal(), but is there a nicer, more spring-like way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林散布 2024-10-05 16:36:42

您可以添加这些方法。 TransportContextHolder 将在线程局部变量中保存一些与传输(本例中为 HTTP)相关的数据。您可以从 TransportContext

protected HttpServletRequest getHttpServletRequest() {
    TransportContext ctx = TransportContextHolder.getTransportContext();
    return ( null != ctx ) ? ((HttpServletConnection ) ctx.getConnection()).getHttpServletRequest() : null;
}

protected String getHttpHeaderValue( final String headerName ) {
    HttpServletRequest httpServletRequest = getHttpServletRequest();
    return ( null != httpServletRequest ) ? httpServletRequest.getHeader( headerName ) : null;
}

You can add these methods. The TransportContextHolder will hold some data related to transport (HTTP in this case) in a thread local variable. You can access HttpServletRequest from the TransportContext.

protected HttpServletRequest getHttpServletRequest() {
    TransportContext ctx = TransportContextHolder.getTransportContext();
    return ( null != ctx ) ? ((HttpServletConnection ) ctx.getConnection()).getHttpServletRequest() : null;
}

protected String getHttpHeaderValue( final String headerName ) {
    HttpServletRequest httpServletRequest = getHttpServletRequest();
    return ( null != httpServletRequest ) ? httpServletRequest.getHeader( headerName ) : null;
}
轻许诺言 2024-10-05 16:36:42

您可以通过注入 HttpServletRequest 来访问 Spring SOAP Endpoint 中的 HTTP 标头。

例如,您需要获取 Autorization 标头(您使用基本身份验证)。

SOAP 请求:

POST http://localhost:8025/ws HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Authorization: Basic YWRtaW46YWRtaW4=
Content-Length: 287
Host: localhost:8025
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tok="http://abcdef.com/integration/adapter/services/Token">
   <soapenv:Header/>
   <soapenv:Body>
      <tok:GetTokenRequest>
      </tok:GetTokenRequest>
   </soapenv:Body>
</soapenv:Envelope>

@Endpoint java 类

@Endpoint
@Slf4j
public class TokenEndpoint {

    public static final String NAMESPACE_URI = "http://abcdef.com/integration/adapter/services/Token";
    private static final String AUTH_HEADER = "Authorization";

    private final HttpServletRequest servletRequest;
    private final TokenService tokenService;

    public TokenEndpoint(HttpServletRequest servletRequest, TokenService tokenService) {
        this.servletRequest = servletRequest;
        this.tokenService = tokenService;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetTokenRequest")
    @ResponsePayload
    public GetTokenResponse getToken(@RequestPayload GetTokenRequest request) {
        String auth = servletRequest.getHeader(AUTH_HEADER);
        log.debug("Authorization header is {}", auth);
        return tokenService.getToken(request);
    }
}

You can access to HTTP headers in Spring SOAP Endpoint by injecting HttpServletRequest.

For example, you need to get Autorization header (you use Basic authentication).

SOAP request:

POST http://localhost:8025/ws HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Authorization: Basic YWRtaW46YWRtaW4=
Content-Length: 287
Host: localhost:8025
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tok="http://abcdef.com/integration/adapter/services/Token">
   <soapenv:Header/>
   <soapenv:Body>
      <tok:GetTokenRequest>
      </tok:GetTokenRequest>
   </soapenv:Body>
</soapenv:Envelope>

@Endpoint java class

@Endpoint
@Slf4j
public class TokenEndpoint {

    public static final String NAMESPACE_URI = "http://abcdef.com/integration/adapter/services/Token";
    private static final String AUTH_HEADER = "Authorization";

    private final HttpServletRequest servletRequest;
    private final TokenService tokenService;

    public TokenEndpoint(HttpServletRequest servletRequest, TokenService tokenService) {
        this.servletRequest = servletRequest;
        this.tokenService = tokenService;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GetTokenRequest")
    @ResponsePayload
    public GetTokenResponse getToken(@RequestPayload GetTokenRequest request) {
        String auth = servletRequest.getHeader(AUTH_HEADER);
        log.debug("Authorization header is {}", auth);
        return tokenService.getToken(request);
    }
}
梦魇绽荼蘼 2024-10-05 16:36:42

我遇到了同样的问题(请参阅此其他问题)。我需要向我的 WS 添加一个 Content-Type 标头。我走上了Servlet Filter的道路。大多数时候,您不需要更改 Web 服务中的 HTTP 标头。但是......有时理论与实践之间存在差异。

I had the same kind of problem (see this other question). I needed to add a Content-Type header to my WS. I went the road of the Servlet Filter. Most of the time, you should not need to change HTTP headers in a webservice. But ... there is sometime a diference between theory and practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文