码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-07 23:28:47      阅读:875      评论:0      收藏:0      [点我收藏+]

标签:身份验证   shu   continue   -o   not   first   boolean   lin   ica   

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

标签:身份验证   shu   continue   -o   not   first   boolean   lin   ica   

原文地址:http://www.cnblogs.com/huangzhex/p/axis2.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!