Android Authentication scheme ntlm not supported

前端 未结 4 2039
别那么骄傲
别那么骄傲 2021-02-10 16:31

I am using asynhttpClient for basic authentication

http://loopj.com/android-async-http/

that is looj lib..

below is my code:

usernameRandomPassw

相关标签:
4条回答
  • 2021-02-10 16:43

    I have same problem. I want to use android asynk http, but i not found ntlm auth I found solution: 1)use above answer,download and import jcifs-1.3.17.jar 2)then i download https://github.com/loopj/android-async-http (not JAR file) and import in my project, 3) then in file AsynkHttpClient.java after

      httpClient = new DefaultHttpClient(cm, httpParams);
    

    insert

     httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
        httpClient.getCredentialsProvider().setCredentials(
                // Limit the credentials only to the specified domain and port
                new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                // Specify credentials, most of the time only user/pass is needed
               new NTCredentials("username", "pass","", "")
    
    
        );
    

    !!!!!!! then very important you must comment like this

     httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
                @Override
                public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
                    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
                    CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
                            ClientContext.CREDS_PROVIDER);
                    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    
    //                if (authState.getAuthScheme() == null) {
    //                    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
    //                    Credentials creds = credsProvider.getCredentials(authScope);
    //                    if (creds != null) {
    //                        authState.setAuthScheme(new BasicScheme());
    //                        authState.setCredentials(creds);
    //                    }
    //                }
                }
            }, 0);
    

    that all you need :)

    0 讨论(0)
  • 2021-02-10 16:44

    here is the full fledged working for this.

    try
      {
         DefaultHttpClient httpclient = new DefaultHttpClient();
    
      // register ntlm auth scheme
         httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
    
         httpclient.getCredentialsProvider().setCredentials(
                 new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                 new NTCredentials("username","password"));
    
                 //xx = ip address yy = port
         HttpPost httpPost = new HttpPost("http://xx.xx.xx.xx:yy/");
    
         Log.e(TAG, "executing request" + httpPost.getRequestLine());
         HttpResponse response = httpclient.execute(httpPost);
    
         HttpEntity entity = response.getEntity();
    
         Log.e(TAG, "" + response.getStatusLine());
         if (entity != null)
         {
            Log.e(TAG, "Response content length: " + entity.getContentLength());
         }
         if (entity != null)
         {
            Log.e(TAG, "Response stream: " + getMessage(entity.getContent()));
            entity.consumeContent();
         }
    
      }
      catch (Exception e)
      {
         Log.e(TAG, "" + e.getMessage());
      }
    

    please note to include these imports only

    import java.net.HttpURLConnection;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.NTCredentials;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.auth.NTLMSchemeFactory;
    import org.apache.http.impl.client.DefaultHttpClient;
    

    and do use httpclient-android-4.3.5.1.jar only.

    0 讨论(0)
  • 2021-02-10 16:52

    Looks to me that you are possibly behind a proxy? NTLM looks to be a largely undocumented Microsoft protocol:

    http://www.innovation.ch/personal/ronald/ntlm.html

    You cannot simply use Basic Auth because this is some different authentication scheme required by the server to whom you are speaking, or by a proxy in between you and your destination.

    0 讨论(0)
  • 2021-02-10 16:57

    here is the answer through code:

    add below code to your android file

                DefaultHttpClient httpclient = new DefaultHttpClient();
                // register ntlm auth scheme
                httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
                httpclient.getCredentialsProvider().setCredentials(
                        // Limit the credentials only to the specified domain and port
                        new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                        // Specify credentials, most of the time only user/pass is needed
                        new NTCredentials(username, password, "", "")
                );
    
                HttpUriRequest httpget = new HttpGet(your_URL);
                HttpResponse response = httpclient.execute(httpget);
                String responseBody = EntityUtils.toString(response.getEntity());
                Log.i(tag,"responseBody =>>>>>>>>>>"+responseBody);
    

    now download lib and java file from

    https://github.com/masconsult/android-ntlm

    and copy jcifs-1.3.17.jar to your lib folder and JCIFSEngine and NTLMSchemeFactory to your package. (you can change package if you want..)

    Thats it your app is ready to run.

    More useful Links:

    http://www.developergarden.com/en/marketplace/components/details/cmp/android-ntlm-authentication/

    0 讨论(0)
提交回复
热议问题