android - 如何修复包含不安全的 TrustManager 实现的应用

标签 android android-security android-securityexception

我正在努力使我的 Android 应用程序符合 Android 的新政策,即根据此 requirement and instructions 拥有安全应用程序.

1) 我首先将 SSL 和 https 添加到我应用程序的 url 2) 然后我开始使用类 HttpsURLConnection 而不是 HttpURLConnection

下面是我使用的远程调用的例子:

   public void sendFeedback(String name , String email , String password ) 
   {  
        String[] params = new String[] { "https://www.problemio.com/auth/create_profile_mobile.php", name , email , password };

        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(params);        
   }

   public class DownloadWebPageTask extends AsyncTask<String, Void, String> 
   {       
        private boolean connectionError = false;


     @Override
     protected void onPreExecute( ) 
     {
          dialog = new Dialog(CreateProfileActivity.this);

          dialog.setContentView(R.layout.please_wait);
          dialog.setTitle("Creating Profile");

          TextView text = (TextView) dialog.findViewById(R.id.please_wait_text);
          text.setText("Please wait while your profile is created... ");
          dialog.show();
     }             

    @Override
    protected String doInBackground(String... theParams) 
    {
        String myUrl = theParams[0];
        final String name = theParams[1];
        final String email = theParams[2];
        final String password = theParams[3];

        String charset = "UTF-8";                       
        String response = null;

        try 
        {               
            String query = String.format("name=%s&email=%s&password=%s", 
                     URLEncoder.encode(name, charset), 
                     URLEncoder.encode(email, charset), 
                     URLEncoder.encode(password, charset));

            final URL url = new URL( myUrl + "?" + query );

            final HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

            conn.setDoOutput(true); 
            conn.setRequestMethod("POST");

            conn.setDoOutput(true);

            conn.setUseCaches(false);

            conn.connect();

            final InputStream is = conn.getInputStream();
            final byte[] buffer = new byte[8196];
            int readCount;
            final StringBuilder builder = new StringBuilder();
            while ((readCount = is.read(buffer)) > -1) 
            {
                builder.append(new String(buffer, 0, readCount));
            }

            response = builder.toString();      
        } 
        catch (Exception e) 
        {
              connectionError = true;
        }

        return response;
    }

    @Override
    protected void onPostExecute(String result) 
    {       
        // Some code

            // Make an intent to go to the home screen
            Intent myIntent = new Intent(CreateProfileActivity.this, MainActivity.class);
            CreateProfileActivity.this.startActivity(myIntent);
        }
    }    
}

但它并没有移除我的开发者控制台上的警告标志。知道我做错了什么以及如何解决这个问题吗?

最佳答案

假设 Google 的扫描器没有损坏,它提示的 X509TrustManager 可能来自两个地方之一。

它可能来自您自己的源代码。通常,您会记得这样做,因为您在某个类中输入了 implements X509TrustManager 并覆盖了一堆看起来很恶心的方法。快速搜索您的源代码应该可以确定是否属于这种情况。

如果不是,它来自某个图书馆。许多——希望是大多数——图书馆会清理它。但是,它可能只在比您当前使用的库的新版本中被清理,因为您在依赖项中列出了旧版本,或者您正在使用本地 JAR 或其他东西。坏消息是,在这里追踪罪魁祸首可能会很痛苦,尽管它仅限于需要 Internet 访问的图书馆(例如,recyclerview-v7 不会有问题)。好消息是,解决这个问题可能就像更新库一样简单,或者如果它与您不再使用的应用程序的过去实现有关,则将其删除。

虽然我不能谈论 Flurry,但这个问题确实存在于旧版本的 ACRA 中。最近几个月对 ACRA 进行了各种其他修复,因此我建议您无论如何都升级到当前版本。

关于android - 如何修复包含不安全的 TrustManager 实现的应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36391986/

相关文章:

android - 将 textView 动态添加到 linearLayout

Android调整VirtualDisplay的大小

android - 针对不安全的 TrustManager 的 Google Play 安全警报

android - 为什么 NotificationManagerCompat::cancelAll() 得到 SecurityException?

android - 使用 Line Control Nutiteq SDK 平滑转弯

android - 在 SciChart 中实现 TA-lib

java - 如何将文件资源加密/解密到 Android 中?

android - 没有详细信息的 java.lang.SecurityException

android - java.security.cert.CertPathValidatorException : Trust anchor for certification path not found. on api less 24

android-studio - OkHttp:<-- HTTP 失败:java.net.UnknownServiceException:网络安全策略不允许与 10.0.2.2 的 CLEARTEXT 通信