How do i modify HTTP headers for a JAX-WS response in CXF?

前端 未结 1 393
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 05:05

I have been working on developing CXF web services that sit behind a security proxy which asks for HTTP basic authentication prior service invocation. These services communi

相关标签:
1条回答
  • 2020-12-22 05:41

    One approach would be creating a CXF interceptor.

    public class BasicAuthOutInterceptor extends AbstractPhaseInterceptor<Message> {
    
        public BasicAuthOutInterceptor() {
            super(Phase.PRE_STREAM);
        }
    
        @Override
        public void handleMessage(Message message) throws Fault {
            String token = "basic auth token";
    
            @SuppressWarnings("unchecked")
            Map<String, List<String>> headers = (Map<String, List<String>>) message
                    .get(Message.PROTOCOL_HEADERS);
            if (headers == null) {
                headers = new TreeMap<String, List<String>>(
                        String.CASE_INSENSITIVE_ORDER);
                message.put(Message.PROTOCOL_HEADERS, headers);
            }
    
            headers.put("Authentication", Arrays.asList("Basic "+ token));
        }
    
    }
    

    and registering it as an out and outFault interceptor.

        <bean id="basicAuthOutInterceptor class="BasicAuthOutInterceptor" />
    
        <cxf:bus>
            <cxf:outInterceptors>
                <ref bean="basicAuthOutInterceptor"/>
            </cxf:outInterceptors>
            <cxf:outFaultInterceptors>
                <ref bean="basicAuthOutInterceptor"/>
            </cxf:outFaultInterceptors>        
        </cxf:bus> 
    
    0 讨论(0)
提交回复
热议问题