English 中文(简体)
JAX-WS 客户端基本认证
原标题:JAX-WS Client Basic Authentication

我被迫将CXF从我的网络服务中删除。为了消除任何依赖认证的情况,我设置了一个 HttpAuth Supplier 类来处理基本认证问题。

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

最佳回答

为了回答我自己的问题,我决定使用手持者。所以我创造了一个类似于上述交易所供应商的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;
    }
}

创建了处理器解析器, 将解析器添加到链上 :

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"/>
问题回答

对于基本鉴定和春季的 JaxWs PortProxy Factory Bean, 您可以简单地使用 :

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

Adding to kaczors answer for Basic authentication with Spring s JaxWsPortProxyFactoryBean, if the wsdl or xsd is not in the classpath and need to be accessed from an url which requires basic authentication, create a custom JaxWsPortProxyFactoryBean which will enable WSDL url http basic authentication.

公共类 自定义 JaxWs PortProxyFactorialBean 扩展 JaxWs PortProxyFactorialBean 扩展为 JaxWs PortProxyFactorial Bea { 公共类自定义 JaxWs PortProxyFactorialBean 扩展为 JaxWs PortProxyBean {

@Override
public Service createJaxWsService() {

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

感谢sundarams spring论坛





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签