在WebView中进行NTLM认证[英] NTLM Authentication in a WebView

本文是小编为大家收集整理的关于在WebView中进行NTLM认证的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试加载一个将在WebView中显示图像的URL. 在加载URL之前,我需要通过凭据(用户名和密码).

此处,URL从具有NTLM身份验证的服务器托管.

我能够击中另一个这样的网址并获取数据.但是如何在Android中为WebView做同样的东西?

推荐答案

您可以使用 chilkat库用于NTLM身份验证.

  1. 下载Java类(从下载部分中包含在ZIP/RAR文件中) 并将包添加到/src文件夹.包名称是com.chilkatsoft
  2. 将库添加到/libs文件夹. Chilkat Libraries文件夹是:

    • armeabi
    • Armeabi-V7a
    • mips
    • x86

      public class MainActivity extends Activity {
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          boolean success;
          success = http.UnlockComponent("anything");
          if (success != true) {
              return;
          }
      
          http.put_Login("<Username>");
          http.put_Password("<Password>");
          http.put_NtlmAuth(true);
          http.put_SessionLogFilename("ntlmAuthLog.txt");
          String html;
          html = http.quickGetStr("http://websitewithntlmnauthentication.com");
          //load the data to a webview from the string "html".
          webView.loadUrl(html,"","UTF-8"); }
      

      并在onCreate():

      之后添加它
      static {
                // Important: Make sure the name passed to loadLibrary matches the shared library
                // found in your project's libs/armeabi directory.
                //  for "libchilkat.so", pass "chilkat" to loadLibrary
                //  for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
                //  etc.
                // 
                System.loadLibrary("chilkat");
      
                // Note: If the incorrect library name is passed to System.loadLibrary,
                // then you will see the following error message at application startup:
                //"The application <your-application-name> has stopped unexpectedly. Please try again."
            }
      

其他推荐答案

问题有:"我需要在加载URL之前传递凭据(用户名和密码)."但是,在URL之前传递凭据不是一个很好的做法.

任何第三方库都不需要.它可以通过在WebViewClient中覆盖以下方法来实现:

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                HttpAuthHandler handler, String host, String realm)
        {
            Log.d(TAG + "_onReceivedHttpAuthRequest", "host = " + host + " realm = " + realm);
//Show dialog and accept credentials from end-user. Hard-coding username and password is strict NO as it can be easlity reverse engineered.
            handler.proceed("username", "password");
        }

本文地址:https://www.itbaoku.cn/post/102589.html

问题描述

I am trying to load a URL which will show a image in a WebView. I need to pass the credentials (username & password) before loading the URL.

Here, the URL is hosted from a server which has NTLM authentication.

I am able to hit another such URL and get the data. But how do I do the same stuff for a WebView in Android?

推荐答案

You can use the Chilkat Library for NTLM authentication.

  1. download the java classes (contained in the zip/rar file from the downloads section) and add the package to the /src folder. the package name is com.chilkatsoft
  2. add the libraries to the /libs folder. the chilkat libraries folder are:

    • armeabi
    • armeabi-v7a
    • mips
    • x86

      public class MainActivity extends Activity {
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          boolean success;
          success = http.UnlockComponent("anything");
          if (success != true) {
              return;
          }
      
          http.put_Login("<Username>");
          http.put_Password("<Password>");
          http.put_NtlmAuth(true);
          http.put_SessionLogFilename("ntlmAuthLog.txt");
          String html;
          html = http.quickGetStr("http://websitewithntlmnauthentication.com");
          //load the data to a webview from the string "html".
          webView.loadUrl(html,"","UTF-8"); }
      

      and add this after the onCreate():

      static {
                // Important: Make sure the name passed to loadLibrary matches the shared library
                // found in your project's libs/armeabi directory.
                //  for "libchilkat.so", pass "chilkat" to loadLibrary
                //  for "libchilkatemail.so", pass "chilkatemail" to loadLibrary
                //  etc.
                // 
                System.loadLibrary("chilkat");
      
                // Note: If the incorrect library name is passed to System.loadLibrary,
                // then you will see the following error message at application startup:
                //"The application <your-application-name> has stopped unexpectedly. Please try again."
            }
      

其他推荐答案

The questions has: " I need to pass the credentials (username & password) before loading the URL." But, passing credentials before url is loaded is not a good practice.

There is no need for any third party library. It can be achieved by overriding the following method in WebViewClient:

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                HttpAuthHandler handler, String host, String realm)
        {
            Log.d(TAG + "_onReceivedHttpAuthRequest", "host = " + host + " realm = " + realm);
//Show dialog and accept credentials from end-user. Hard-coding username and password is strict NO as it can be easlity reverse engineered.
            handler.proceed("username", "password");
        }