How to access HTTP headers in Spring-ws endpoint?

2019-01-26 10:24发布

问题:

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?

回答1:

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


回答2:

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.



回答3:

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