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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3975694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:13:58  来源:igfitidea点击:

How to access HTTP headers in Spring-ws endpoint?

javaspringhttp-headersspring-ws

提问by Juha Syrj?l?

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

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

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()?

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

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

一种可能可行的方法是创建一个 Servlet 过滤器,将标头值存储到ThreadLocal变量中,然后在 内部访问该变量invokeInternal(),但是有没有更好、更像弹簧的方法来做到这一点?

回答by Juha Syrj?l?

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

您可以添加这些方法。在TransportContextHolder将持有线程局部变量(在这种情况下HTTP)与交通有关的一些数据。您可以HttpServletRequestTransportContext.

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

回答by Vladimir

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

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

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

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

SOAP request:

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>

@Endpointjava class

@EndpointJava 类

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

回答by Guillaume

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.

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