简体   繁体   English

NTLM身份验证Android Studio

[英]NTLM Authentication Android Studio

I've spent many hours trying to figure out how to perform NTLM authentication on Android Studio with no luck. 我花了很多时间试图弄清楚如何在Android Studio上执行NTLM身份验证而没有运气。 I realize NTLM is not native to Android. 我意识到NTLM不是Android原生的。 Recently, I have been using the JCIFS library 最近,我一直在使用JCIFS库

jcifs.Config.registerSmbURLHandler();
URL url = new URL("https://domain%5cuser:pass@host");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

But I have getting the error 但我得到了错误

"Unable to find default handler for protocol: https"

The same code works in standard Java. 相同的代码适用于标准Java。 At this point I've exhausted every suggestion I have found and I have no idea what to do. 在这一点上,我已经用尽了我找到的每一个建议,我不知道该怎么做。

I'm attempting to resolve the same issue, and came across the following link (class is pasted below in case of link rot): https://lists.samba.org/archive/jcifs/2013-July/010105.html 我正在尝试解决相同的问题,并遇到以下链接(如果链接腐烂,则粘贴在下面的类): https//lists.samba.org/archive/jcifs/2013-July/010105.html

I then use the following to force the handler's use: 然后我使用以下命令强制使用处理程序:

    jcifs.Config.registerSmbURLHandler();

    System.setProperty("http.auth.ntlm.domain", domain);
    System.setProperty("jcifs.smb.client.domain", domain);
    System.setProperty("jcifs.smb.client.username", username);
    System.setProperty("jcifs.smb.client.password", password);
    System.setProperty("java.protocol.handler.pkgs", "domain.com.package");

I'm having issues, however, as the response comes back empty, so more investigation needs to be done there. 然而,我遇到了问题,因为回复是空的,所以需要在那里进行更多的调查。

(The copyright notice, etc. have been left off as it kept confusing SO's code block parser) (由于SO的代码块解析器一直令人困惑,因此版权声明等已被删除)

public class Handler extends URLStreamHandler {
/**
 * The default HTTP port (<code>80</code>).
 */
public static final int DEFAULT_HTTP_PORT = 80;

private static final Map PROTOCOL_HANDLERS = new HashMap();

private static final String HANDLER_PKGS_PROPERTY =
        "java.protocol.handler.pkgs";

/**
 * Vendor-specific default packages.  If no packages are specified in
 * "java.protocol.handler.pkgs", the VM uses one or more default
 * packages, which are vendor specific.  Sun's is included below
 * for convenience; others could be as well.  If a particular vendor's
 * package isn't listed, it can be specified in
 * "java.protocol.handler.pkgs".
 */
private static final String[] JVM_VENDOR_DEFAULT_PKGS = new String[] {
    "sun.net.www.protocol"
};

private static URLStreamHandlerFactory factory;

/**
 * Sets the URL stream handler factory for the environment.  This
 * allows specification of the factory used in creating underlying
 * stream handlers.  This can be called once per JVM instance.
*
 * @param factory The URL stream handler factory.
 */
public static void setURLStreamHandlerFactory(
        URLStreamHandlerFactory factory) {
    synchronized (PROTOCOL_HANDLERS) {
        if (Handler.factory != null) {
            throw new IllegalStateException(
                    "URLStreamHandlerFactory already set.");
        }
        PROTOCOL_HANDLERS.clear();
        Handler.factory = factory;
    }
}

/**
 * Returns the default HTTP port.
 *
 * @return An <code>int</code> containing the default HTTP port.
 */
protected int getDefaultPort() {
    return DEFAULT_HTTP_PORT;
}

@Override
protected URLConnection openConnection(URL url) throws IOException
{
    return this.openConnection(url, null);
}

@Override
protected URLConnection openConnection(URL url, Proxy proxy) throws IOException
{
    url = new URL(url, url.toExternalForm(), getDefaultStreamHandler(url.getProtocol()));

    final HttpURLConnection urlConnection;
    if (proxy == null) {
        urlConnection = (HttpURLConnection) url.openConnection();
    } else {
        urlConnection = (HttpURLConnection) url.openConnection(proxy);
    }

    return new NtlmHttpURLConnection(urlConnection);
}

private static URLStreamHandler getDefaultStreamHandler(String protocol)
        throws IOException {
    synchronized (PROTOCOL_HANDLERS) {
        URLStreamHandler handler = (URLStreamHandler)
                PROTOCOL_HANDLERS.get(protocol);
        if (handler != null) return handler;
        if (factory != null) {
            handler = factory.createURLStreamHandler(protocol);
        }
        if (handler == null) {
            String path = System.getProperty(HANDLER_PKGS_PROPERTY);
            StringTokenizer tokenizer = new StringTokenizer(path, "|");
            while (tokenizer.hasMoreTokens()) {
                String provider = tokenizer.nextToken().trim();
                if (provider.equals("jcifs")) continue;
                String className = provider + "." + protocol + ".Handler";
                try {
                    Class handlerClass = null;
                    try {
                        handlerClass = Class.forName(className);
                    } catch (Exception ex) { }
                    if (handlerClass == null) {
                        handlerClass = ClassLoader.getSystemClassLoader(
                                ).loadClass(className);
                    }
                    handler = (URLStreamHandler) handlerClass.newInstance();
                    break;
                } catch (Exception ex) { }
            }
        }
        if (handler == null) {
            for (int i = 0; i < JVM_VENDOR_DEFAULT_PKGS.length; i++) {
                String className = JVM_VENDOR_DEFAULT_PKGS[i] + "." +
                        protocol + ".Handler";
                try {
                    Class handlerClass = null;
                    try {
                        handlerClass = Class.forName(className);
                    } catch (Exception ex) { }
                    if (handlerClass == null) {
                        handlerClass = ClassLoader.getSystemClassLoader(
                                ).loadClass(className);
                    }
                    handler = (URLStreamHandler) handlerClass.newInstance();
                } catch (Exception ex) { }
                if (handler != null) break;
            }
        }
        if (handler == null) {
            throw new IOException(
                    "Unable to find default handler for protocol: " +
                            protocol);
        }
        PROTOCOL_HANDLERS.put(protocol, handler);
        return handler;
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM