【问题标题】:NTLM Authentication in a WebViewWebView 中的 NTLM 身份验证
【发布时间】:2013-02-18 10:23:37
【问题描述】:

我正在尝试加载将在WebView 中显示图像的 URL。 我需要在加载 URL 之前传递凭据(用户名和密码)。

这里的 URL 是从具有 NTLM 身份验证的服务器托管的。

我可以点击另一个这样的 URL 并获取数据。但是我如何在 Android 中为 WebView 做同样的事情呢?

【问题讨论】:

  • 您能否通过单击左侧的勾号来标记答案?

标签: android android-webview ntlm


【解决方案1】:

您可以使用Chilkat Library 进行 NTLM 身份验证。

  1. 下载 java 类(包含在下载部分的 zip/rar 文件中) 并将包添加到 /src 文件夹。包名是 com.chilkatsoft
  2. 将库添加到 /libs 文件夹。 chilkat 库文件夹是:

    • 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."
            }
      

【讨论】:

  • 谢谢。 Chilkat 图书馆工作得很好。对于任何寻找示例代码的人,可以在这里查看example-code.com/android/http_authentication.asp
  • @Sufian 你可以把它标记为正确,这样每个人都可以看到它
  • 如果可以的话,我会的。 SO 版主需要有此权利将问题标记为正确,Microsoft 线程具有此有用功能。 :)
  • 我发现这个库和下载网页并显示在WebView的HTML中是一样的。我想在 NTLM 后面运行一个 Web 应用程序,使用户能够编辑他们的文档文件,但这无济于事,或者据我所知。尽管如此,这应该被标记为答案。
【解决方案2】:

问题是:“我需要在加载 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");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 2019-02-06
    • 2017-04-08
    • 1970-01-01
    相关资源
    最近更新 更多