2016-07-31 4 views
3

Android Studio에서 NTLM 인증을 수행하는 방법을 알아 내려고 많은 시간을 보냈습니다. 나는 NTLM이 안드로이드에 고유하지 않다는 것을 알고 있습니다. 최근에, 나는 JCIFS 라이브러리NTLM 인증 Android Studio

jcifs.Config.registerSmbURLHandler(); 
URL url = new URL("https://domain%5cuser:[email protected]"); 
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 

을 사용하고있다 그러나 나는

"Unable to find default handler for protocol: https" 

동일한 코드는 표준 자바에서 작동 오류가 발생했습니다. 이 시점에서 나는 내가 찾은 모든 제안을 다 써 버렸고 무엇을 해야할지 전혀 모른다.

답변

0

저도 같은 문제를 해결하기 위해 시도하고, (클래스가 죽은 링크의 경우 아래에 붙여) 다음 링크를 통해 온거야 : https://lists.samba.org/archive/jcifs/2013-July/010105.html

난 후 처리기의 사용을 강제로 다음을 사용하십시오

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"); 

응답이 비어 나오면 문제가 발생하므로 더 많은 조사가 필요합니다.

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; 
    } 
} 

}

은 (는 SO의 코드 블록 파서를 혼동 유지로 등 저작권이 중단 된)

+0

그런데도 작업; Handler와 테스트 클래스를 자바 라이브러리 모듈로 옮겼다. null 응답 스트림 대신 이제 401을 반환합니다. –