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

时间:2012-05-24 15:15:46

标签: 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
       }

}

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

3 个答案:

答案 0 :(得分:7)

要回答我自己的问题......我决定使用处理程序。所以我创建了一个类似于上面的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;
    }
}

创建一个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;
    }
}

然后在春天,就这样把它全部搞定了:

<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"/>

答案 1 :(得分: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>

答案 2 :(得分:0)

使用Spring的JaxWsPortProxyFactoryBean添加kaczors回答基本身份验证, 如果wsdl或xsd不在类路径中并且需要从需要基本身份验证的url访问,请创建一个自定义JaxWsPortProxyFactoryBean,它将启用WSDL url http基本身份验证。

public class CustomJaxWsPortProxyFactoryBean扩展JaxWsPortProxyFactoryBean {

@Override
public Service createJaxWsService() {

    Authenticator.setDefault(new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(
                    getUsername(),
                    getPassword().toCharArray());
        }
    });
    return super.createJaxWsService();
}

感谢spring forums

的sundaramss