Java 在请求的方法中访问 SOAP 消息头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18534493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:51:33  来源:igfitidea点击:

Access SOAP message header in the requested method

javaweb-servicessoapjax-wshandler

提问by Gfaramaz

EDIT : Message cleared and code added

编辑:清除消息并添加代码

I develop a jax-ws based web service with a basic Java client.

我使用基本的 Java 客户端开发了一个基于 jax-ws 的 Web 服务。

I use SOAP handlers to authenticate the user. One on the client side which add the userId and the token to the SOAP header and another one on the server side which get these informations and authenticate the user with the database.

我使用 SOAP 处理程序来验证用户。一个在客户端,将 userId 和令牌添加到 SOAP 标头,另一个在服务器端,用于获取这些信息并使用数据库对用户进行身份验证。

Client side (simplified) :

客户端(简化):

import static modules.auth.AuthClient.userID;
import static modules.auth.AuthClient.token;

public boolean handleMessage(SOAPMessageContext context) {

    //Getting SOAP headers
    SOAPMessage soapMsg = context.getMessage();
    SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
    SOAPHeader soapHeader = soapEnv.getHeader();

    QName qname;
    SOAPHeaderElement soapHeaderElement;

    //Add userID in SOAP header
    qname = new QName("****","UserID");
    soapHeaderElement = soapHeader.addHeaderElement(qname);
    soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
    soapHeaderElement.addTextNode(userID);

    //Add token in SOAP header
    qname = new QName("****","Token");
    soapHeaderElement = soapHeader.addHeaderElement(qname);
    soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
    soapHeaderElement.addTextNode(token);

    soapMsg.saveChanges();

    return true;
}

Server side (simplified) :

服务器端(简化):

public boolean handleMessage(SOAPMessageContext context) {  
    Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (!isRequest) {
        //Getting SOAP headers
        SOAPMessage soapMsg = context.getMessage();
        SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
        SOAPHeader soapHeader = soapEnv.getHeader();

        Iterator it = soapHeader.extractHeaderElements(SOAPConstants.URI_SOAP_ACTOR_NEXT);

        Node userIDNode = (Node) it.next();
        String userID = (userIDNode == null) ? null : userIDNode.getValue();

        Node tokenNode = (Node) it.next();
        String token = (tokenNode == null) ? null : tokenNode.getValue();

        //Return if the user is connected
        User u = AuthWS.validToken(userID, token);

        //Here I have my user but I don't know how to get it in my requested method
    }
    return true;
}

In order to manage users rights I would like to access my user directly in the method requested.

为了管理用户权限,我想直接在请求的方法中访问我的用户。

Example of method that can be requested :

可以请求的方法示例:

public Project getProject(@WebParam(name = "name") String name) throws WebServiceFailure, EntityNotFoundException {

    //Here I would like to verify the user's rights

    try {
        PreparedStatement ps = DBConnect.getStatement("SELECT name FROM projects WHERE name = '" + name + "'");
        ResultSet res = ps.executeQuery();
        if(res.next()){
            Project p = new Project(res.getString("name"));
            ps.close();
            return p;
        } else {
            ps.close();
            throw new EntityNotFoundException("Can't find the project '"+name+"'");
        }
    } catch (SQLException ex) {
        throw new WebServiceFailure(ex.getMessage());
    }
}

Many thanks

非常感谢

采纳答案by Gfaramaz

Finally, I found exactly what I was searching for :

最后,我找到了我正在寻找的内容:

Follow this link.

按照这个链接。

In the section "Passing Information from the Handler Level to the Application"

在“将信息从处理程序级别传递到应用程序”部分中