【问题标题】:Java onvif gsoap authorization (http 401 error)Java onvif gsoap 授权(http 401 错误)
【发布时间】:2020-04-12 04:32:09
【问题描述】:

这是我相机的授权功能。我在任何请求中都有错误 http 401(未经授权)。未经授权的请求正常工作。我使用文档编写了这段代码:https://www.oasis-open.org/committees/download.php/13392/wss-v1.1-spec-pr-UsernameTokenProfile-01.htm 并嗅探 OnvifDeviceManager 的流量(授权在这里工作!)。我使用 post-method 从这个函数发送返回值。问题出在哪里?可能是字符串随机数?是随机数吗?

public static String getAuthXML(String body)
{
        Date date = new Date(System.currentTimeMillis() - 3 * 3_600_000);
        //time of camera - UTC, time of my PC - GMT+3:00 (3 hours difference)

        String now = dateformat.format(date).concat("T").concat(timeformatptz.format(date).concat("Z"));

        String nonce = "";
        //generate random hex-number (21 symbol)
        while (nonce.length() < 21)
            nonce = nonce.concat(new BigInteger(String.valueOf(date.getTime())).toString(16));
        nonce = nonce.substring(0, 21);

        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        md.update(nonce.concat(now).concat(pass).getBytes(StandardCharsets.UTF_8));
        String sha1 = new String(md.digest());

        return "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Header><Security s:mustUnderstand=\"1\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>"
                .concat(login)
                .concat("</Username><Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">")
                .concat(pass)
                .concat("</Password><Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">")
                .concat(Base64.getEncoder().encodeToString(sha1.getBytes(StandardCharsets.UTF_8)))
                .concat("</Password><Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">")
                .concat(Base64.getEncoder().encodeToString(nonce.getBytes(StandardCharsets.UTF_8)))
                .concat("</Nonce><Created xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">")
                .concat(now)
                .concat("</Created></UsernameToken></Security></s:Header><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">")
                .concat(body)
                .concat("</s:Body></s:Envelope>");
}

P.S.:对不起我的英语,我是俄罗斯人)

【问题讨论】:

  • 如果我没看错,while 循环没有括号。还是只想循环 while 条件下方的第一行?
  • 我明白了,你编辑了它。
  • 我使用“while”循环生成具有 21 个或更多符号长度的随机十六进制数。 'while' 不需要括号,我知道。我删除了第 22 个和下一个符号。当我从 OnvifDeviceManager 的流量中获取原始随机数时 - 我的代码仍然有 401 错误。

标签: java base64 authorization sha1 onvif


【解决方案1】:
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.reset();
    md.update(nonce.concat(now).concat(pass).getBytes(StandardCharsets.UTF_8));

    return "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\"><s:Header><Security s:mustUnderstand=\"1\" xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><UsernameToken><Username>"
            .concat(login)
            .concat("</Username><Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">")
            .concat(Base64.getEncoder().encodeToString(md.digest()))
            .concat("</Password><Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">")
            .concat(Base64.getEncoder().encodeToString(nonce.getBytes(StandardCharsets.UTF_8)))
            .concat("</Nonce><Created xmlns=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">")
            .concat(now)
            .concat("</Created></UsernameToken></Security></s:Header><s:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">")
            .concat(body)
            .concat("</s:Body></s:Envelope>");

【讨论】:

    猜你喜欢
    • 2013-12-29
    • 2012-02-22
    • 2014-06-24
    • 2014-02-26
    • 2012-05-30
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 2018-10-21
    相关资源
    最近更新 更多