JAX-WS客户端基本身份验证

Bry*_*her 1 java spring jax-ws

我被迫从我的网络服务中删除CXF.为了删除对身份验证的任何依赖性,我设置了一个HttpAuthSupplier类来处理基本身份验证.

public class ExchangeAuthSupplier implements HttpAuthSupplier{
       @Override
       public boolean requiresRequestCaching() {
           return false;
       }

       @Override
       public String getAuthorization(AuthorizationPolicy authPolicy, URL url, Message message, String fullHeader) {
           // Lookup authentication information and return appropriate header
       }

}
Run Code Online (Sandbox Code Playgroud)

我正在试图弄清楚如何使用常规JAX-WS API和Spring来做类似的事情......

Bry*_*her 8

要回答我自己的问题......我决定使用Handler.所以我创建了一个类似于上面的ExchangeAuthSupplier的SOAPHandler:

public class MyAuthenticationHandler implements SOAPHandler<SOAPMessageContext> {
    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        final Boolean outInd = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outInd.booleanValue()) {
            try {
                UserNamePasswordPair userNamePasswordPair = getAuthorization(); // Method to retrieve credentials from somewhere

                context.put(BindingProvider.USERNAME_PROPERTY, userNamePasswordPair.getUsername());
                context.put(BindingProvider.PASSWORD_PROPERTY, userNamePasswordPair.getPassword());

            } catch (final Exception e) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        logger.error("error occurred when getting auth.");
        return false;
    }

    @Override
    public void close(MessageContext context) {
        logger.debug("closing handler for auth...");
    }

    @Override
    public Set<QName> getHeaders() {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建了HandlerResolver以将解析器添加到链中:

public class MyHandlerResolver implements HandlerResolver {
    private List<Handler> chain;

    public MyHandlerResolver() {
        chain = new ArrayList<Handler>();
        chain.add(new MyAuthenticationHandler();
    }

    @Override
    public List<Handler> getHandlerChain(PortInfo portInfo) {
        return chain;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在Spring,就像这样把它全部搞定:

<bean id="myJAXWSClient" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
    <property name="serviceInterface" value="Interface to implement"/>
    <property name="wsdlDocumentUrl" value="classpath:/wsdl/theWsdl.wsdl"/>
    <property name="namespaceUri" value="namespace"/>
    <property name="serviceName" value="ServiceName"/>
    <property name="endpointAddress" value="/endpoint"/>
    <property name="handlerResolver" ref="myHandlerResolver"/>
</bean>

<bean id="myHandlerResolver" class="com.mystuff.ExchangeHandlerResolver"/>
Run Code Online (Sandbox Code Playgroud)


Paw*_*ski 5

对于Basic auth和Spring的JaxWsPortProxyFactoryBean,您可以简单地使用:

<bean id="myJAXWSClient" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
    <property name="serviceInterface" value="Interface to implement"/>
    <property name="wsdlDocumentUrl" value="classpath:/wsdl/theWsdl.wsdl"/>
    <property name="namespaceUri" value="namespace"/>
    <property name="serviceName" value="ServiceName"/>
    <property name="endpointAddress" value="/endpoint"/>
    <property name="username" value="username"/>
    <property name="password" value="password"/>
</bean>
Run Code Online (Sandbox Code Playgroud)