How to access HTTP headers in Spring-ws endpoint?

后端 未结 3 681
余生分开走
余生分开走 2021-01-04 18:49

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

My code looks like this:

public class MyEndpoint extends AbstractMarshallingPayloadEndpoint {
           


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 19:54

    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)
    
    
       
       
          
          
       
    
    

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

提交回复
热议问题