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>

 

posted @ 2017-02-07 22:32  清晖皓月  阅读(2215)  评论(0编辑  收藏  举报