Zimbra SOAP API 在 java 中使用 zm_auth_token 进行身份验证

发布于 2024-11-16 01:44:42 字数 364 浏览 6 评论 0原文

我想使用我处置的 zm_auth_token 对用户进行身份验证:

目前,我正在这样做:

    LmcAuthRequest auth = new LmcAuthRequest();
    auth.setUsername(userName);
    auth.setPassword(password);
    LmcAuthResponse authResp = (LmcAuthResponse) auth.invoke(serverURL);
    LmcSession session = authResp.getSession();

但我想使用我拥有的 zm_auth_token。如何做到这一点??? 谢谢

I want to authenticate a user using the zm_auth_token that I dispose :

For the moment, I'm doing this :

    LmcAuthRequest auth = new LmcAuthRequest();
    auth.setUsername(userName);
    auth.setPassword(password);
    LmcAuthResponse authResp = (LmcAuthResponse) auth.invoke(serverURL);
    LmcSession session = authResp.getSession();

But I want to use the zm_auth_token that I have. How to do this ???
Thnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左秋 2024-11-23 01:44:42

zimbra Lmc 方法现已弃用......
如果你想使用 SOAP,他们更喜欢使用 ZMailBox(它对我不起作用),我使用了这个方法:

// Create the connection where we're going to send the file.
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;

        String  postContent = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">"+
        "<soap:Header>" +
        "<context xmlns=\"urn:zimbra\">" +
        "<format type=\"js\"/>" +
        "<authToken>" + authToken + "</authToken>" +
        "</context>" +
        "</soap:Header>" +
        "<soap:Body>" + 
        "<GetFolderRequest xmlns=\"urn:zimbraMail\" />" +
        "</soap:Body>" +
        "</soap:Envelope>";

        // insert your SOAP XML!!!
        byte[] b = postContent.getBytes();

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty( "Content-Length", String.valueOf( b.length ) );
        httpConn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
        httpConn.setRequestMethod( "POST" );
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);

        // Everything's set up; send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();
        out.write( b );
        out.close();

        // Read the response and write it to standard out.
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        // read & do something with input stream...
        String s = null;
        String response = "";
        while((s=in.readLine()) != null){
            response += s;
        }
        in.close();

The zimbra Lmc methods are deprecated now ...
If you want to use SOAP they prefer doing it using ZMailBox (It doesn't work for me), I used this method :

// Create the connection where we're going to send the file.
        URL url = new URL(SOAPUrl);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConn = (HttpURLConnection) connection;

        String  postContent = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">"+
        "<soap:Header>" +
        "<context xmlns=\"urn:zimbra\">" +
        "<format type=\"js\"/>" +
        "<authToken>" + authToken + "</authToken>" +
        "</context>" +
        "</soap:Header>" +
        "<soap:Body>" + 
        "<GetFolderRequest xmlns=\"urn:zimbraMail\" />" +
        "</soap:Body>" +
        "</soap:Envelope>";

        // insert your SOAP XML!!!
        byte[] b = postContent.getBytes();

        // Set the appropriate HTTP parameters.
        httpConn.setRequestProperty( "Content-Length", String.valueOf( b.length ) );
        httpConn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
        httpConn.setRequestMethod( "POST" );
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);

        // Everything's set up; send the XML that was read in to b.
        OutputStream out = httpConn.getOutputStream();
        out.write( b );
        out.close();

        // Read the response and write it to standard out.
        InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
        BufferedReader in = new BufferedReader(isr);

        // read & do something with input stream...
        String s = null;
        String response = "";
        while((s=in.readLine()) != null){
            response += s;
        }
        in.close();
春夜浅 2024-11-23 01:44:42
SOAPConnectionFactory soapfactory=SOAPConnectionFactory.newInstance();
SOAPConnection soapconnection=soapfactory.createConnection();
MessageFactory messagefactory=MessageFactory.newInstance();
SOAPMessage messege=messagefactory.createMessage();
SOAPEnvelope envelop=messege.getSOAPPart().getEnvelope();
SOAPHeader header=messege.getSOAPHeader();
SOAPBody body=messege.getSOAPBody();
Name header_context=envelop.createName("context", null,"urn:zimbra");
Name auth_request=envelop.createName("AuthRequest",null,"urn:zimbraAccount");
Name account=envelop.createName("account");
Name password=envelop.createName("password");
header.addHeaderElement(header_context);
SOAPBodyElement auth_body=body.addBodyElement(auth_request);
        auth_body.addChildElement(account).addAttribute(envelop.createName("by"),"name").addTextNode("abc");//(abc==your username)
auth_body.addChildElement(password).addTextNode("1234");//(1234=your password)
URL url=new URL("http://192.168.1.67/service/soap/AuthRequest");
SOAPMessage response=soapconnection.call(messege, url);
SOAPConnectionFactory soapfactory=SOAPConnectionFactory.newInstance();
SOAPConnection soapconnection=soapfactory.createConnection();
MessageFactory messagefactory=MessageFactory.newInstance();
SOAPMessage messege=messagefactory.createMessage();
SOAPEnvelope envelop=messege.getSOAPPart().getEnvelope();
SOAPHeader header=messege.getSOAPHeader();
SOAPBody body=messege.getSOAPBody();
Name header_context=envelop.createName("context", null,"urn:zimbra");
Name auth_request=envelop.createName("AuthRequest",null,"urn:zimbraAccount");
Name account=envelop.createName("account");
Name password=envelop.createName("password");
header.addHeaderElement(header_context);
SOAPBodyElement auth_body=body.addBodyElement(auth_request);
        auth_body.addChildElement(account).addAttribute(envelop.createName("by"),"name").addTextNode("abc");//(abc==your username)
auth_body.addChildElement(password).addTextNode("1234");//(1234=your password)
URL url=new URL("http://192.168.1.67/service/soap/AuthRequest");
SOAPMessage response=soapconnection.call(messege, url);
岁月流歌 2024-11-23 01:44:42

您可以使用 Zimbra 库来调用 SOAP API。请查看此答案

You can use Zimbra libraries for calling SOAP API. Please check this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文