복붙노트

[SPRING] Spring-ws 엔드 포인트에서 HTTP 헤더에 액세스하는 방법?

SPRING

Spring-ws 엔드 포인트에서 HTTP 헤더에 액세스하는 방법?

스프링 엔드 포인트에서 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 헤더에 어떻게 액세스합니까?

아마도 작동 할 수있는 한 가지 방법은 헤더 값을 ThreadLocal 변수에 저장하고 그 값을 invokeInternal ()에서 액세스하는 서블릿 필터를 만드는 것입니다.하지만이를 수행하는 데 더 좋은 봄 같은 방법이 있습니까?

해결법

  1. ==============================

    1.이러한 메소드를 추가 할 수 있습니다. TransportContextHolder는 전송과 관련된 일부 데이터 (이 경우 HTTP)를 스레드 로컬 변수에 보유합니다. TransportContext에서 HttpServletRequest에 액세스 할 수 있습니다.

    이러한 메소드를 추가 할 수 있습니다. TransportContextHolder는 전송과 관련된 일부 데이터 (이 경우 HTTP)를 스레드 로컬 변수에 보유합니다. TransportContext에서 HttpServletRequest에 액세스 할 수 있습니다.

    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. ==============================

    2.나는 같은 종류의 문제를 겪었다 (이 다른 질문을 보라). 내 WS에 Content-Type 헤더를 추가해야했습니다. 나는 서블릿 필터의 길을 갔다. 대부분의 경우 웹 서비스에서 HTTP 헤더를 변경할 필요가 없습니다. 그러나 ... 언젠가 이론과 실천 사이에는 차이가 있습니다.

    나는 같은 종류의 문제를 겪었다 (이 다른 질문을 보라). 내 WS에 Content-Type 헤더를 추가해야했습니다. 나는 서블릿 필터의 길을 갔다. 대부분의 경우 웹 서비스에서 HTTP 헤더를 변경할 필요가 없습니다. 그러나 ... 언젠가 이론과 실천 사이에는 차이가 있습니다.

  3. ==============================

    3.HttpServletRequest를 삽입하여 Spring SOAP Endpoint의 HTTP 헤더에 액세스 할 수있다.

    HttpServletRequest를 삽입하여 Spring SOAP Endpoint의 HTTP 헤더에 액세스 할 수있다.

    예를 들어, 인증 헤더 (기본 인증 사용)를 얻어야합니다.

    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 자바 클래스

    @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);
        }
    }
    
  4. from https://stackoverflow.com/questions/3975694/how-to-access-http-headers-in-spring-ws-endpoint by cc-by-sa and MIT license