Axis2身份验证-采用Module方式验证Soap Header实现

Posted 嫣然居

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Axis2身份验证-采用Module方式验证Soap Header实现相关的知识,希望对你有一定的参考价值。

1.创建UserCheckModule类

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisDescription;
import org.apache.axis2.description.AxisModule;
import org.apache.axis2.modules.Module;
import org.apache.neethi.Assertion;
import org.apache.neethi.Policy;

/**
 * <P>Description: TODO</P>
 * @ClassName: UserCheckModule
 */
public class UserCheckModule implements Module {

    /**
     * <p>Title: init</p>
     * @see org.apache.axis2.modules.Module#init(org.apache.axis2.context.ConfigurationContext, org.apache.axis2.description.AxisModule)
     */
    @Override
    public void init(ConfigurationContext configContext, AxisModule module) throws AxisFault {
        // TODO Auto-generated method stub

    }

    /**
     * <p>Title: engageNotify</p>
     * @see org.apache.axis2.modules.Module#engageNotify(org.apache.axis2.description.AxisDescription)
     */
    @Override
    public void engageNotify(AxisDescription axisDescription) throws AxisFault {
        // TODO Auto-generated method stub

    }

    /**
     * <p>Title: canSupportAssertion</p>
     * @see org.apache.axis2.modules.Module#canSupportAssertion(org.apache.neethi.Assertion)
     */
    @Override
    public boolean canSupportAssertion(Assertion assertion) {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * <p>Title: applyPolicy</p>
     * @see org.apache.axis2.modules.Module#applyPolicy(org.apache.neethi.Policy, org.apache.axis2.description.AxisDescription)
     */
    @Override
    public void applyPolicy(Policy policy, AxisDescription axisDescription) throws AxisFault {
        // TODO Auto-generated method stub

    }

    /**
     * <p>Title: shutdown</p>
     * @see org.apache.axis2.modules.Module#shutdown(org.apache.axis2.context.ConfigurationContext)
     */
    @Override
    public void shutdown(ConfigurationContext configurationContext) throws AxisFault {
        // TODO Auto-generated method stub

    }

}

2.创建UserCheckHandler

import java.util.Iterator;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;


public class UserCheckHandler extends AbstractHandler implements Handler {


    @Override
    public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
        // 获取Head
        OMElement fistElement = msgContext.getEnvelope().getHeader().getFirstElement();
        if (fistElement!=null&&fistElement.getChildren() != null) {
            Iterator<?> list = (Iterator<?>) fistElement.getChildren();
            String Username = "";
            String Password = "";
            while (list.hasNext()) {
                OMElement element = (OMElement) list.next();
                if (element.getLocalName().equals("Username")) {
                    Username = element.getText();
                }
                if (element.getLocalName().equals("Password")) {
                    Password = element.getText();
                }
            }
            if (!Username.equals("toone") || !Password.equals("111111")) {
                throw new AxisFault(" Authentication Fail! Check username/password ");
            }
            return InvocationResponse.CONTINUE;
        } else {
            throw new AxisFault(" Authentication Fail! Check username/password ");
        }
    }
}

3.创建module.xml

<?xml version="1.0" encoding="UTF-8"?>
<moudle name="userCheck" class="com.ehonglin.axis.module.UserCheckModule">
    <InFlow>
        <handler name="InFlowLogHandler" class="com.ehonglin.axis.module.UserCheckHandler">
            <order phase="userCheckPhase"/>
        </handler>
    </InFlow>
</moudle>

4.修改axis.xml

5.在services.xml中使用userCheck

<?xml version="1.0" encoding="UTF-8"?>
<service name="EmptyBoxService" targetNamespace="http://tempuri.org/">
    <description>web service</description>
    <schema schemaNamespace="http://tempuri.org/"/>
    <module ref="userCheck"/>
    <parameter name="ServiceObjectSupplier">
        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
    </parameter>
    <parameter name="SpringBeanName">emptyBoxService</parameter>
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only"
            class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out"
            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </messageReceivers>
</service>

 

以上是关于Axis2身份验证-采用Module方式验证Soap Header实现的主要内容,如果未能解决你的问题,请参考以下文章

SOA:Rails 多个客户端的用户身份验证和 api 授权

WCF 客户端使用 WS-Security UsernameToken PasswordDigest 身份验证方案使用 Axis 2 Web 服务时出错

如何采用SQL Server身份验证登录

6. 802.11x用户身份验证

安全验证必备——语音验证码短信 API

mongoDB身份验证