和你一起终身学习,这里是程序员Android
本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:
一、加强APP 安全沟通
- 建议显示使用应用选择器
- 应用共享数据时,建议使用 签名权限
- 禁止其他应用访问自己的ContentProvider
- 获取敏感信息,需要提前询问凭据
- 提高应用网络安全措施
- 创建自己的信任管理器
- 谨慎使用 WebView 对象
- 使用HTML消息通道
二、提供正确的权限permission
- 使用Intent 进行权限申请
- 跨应用 安全访问数据
三、安全的进行数据存储
- 将私有数据存储在内部存储中
- 谨慎使用外部存储
- 检查数据的有效性
- 仅将非敏感数据存储在缓存文件中
- 请在私有模式SharedPreferences
四、使用安全的三方服务
- 使用 Google Play服务
- 更新所有应用依赖项
通过提高应用程序的安全性,您可以帮助保持用户信任和设备完整性。
此文主要介绍了几种对您的应用安全性产生重大积极影响的最佳做法。
一、加强APP 安全沟通
当您保护在应用程序与其他应用程序之间或应用程序与网站之间交换的数据时,可以提高应用程序的稳定性并保护您发送和接收的数据。
1. 建议显示使用应用选择器
如果隐式意图可以在用户的设备上启动至少两个可能的应用程序,请明确显示应用程序选择器。此交互策略允许用户将敏感信息传输到他们信任的应用程序。
举例如下:
Intent intent = new Intent(Intent.ACTION_SEND);List possibleActivitiesList = queryIntentActivities(intent, PackageManager.MATCH_ALL);// Verify that an activity in at least two apps on the user's device// can handle the intent. Otherwise, start the intent only if an app// on the user's device can handle the intent.if (possibleActivitiesList.size() > 1) { // Create intent to show chooser. // Title is something similar to "Share this photo with". String title = getResources().getString(R.string.chooser_title); Intent chooser = Intent.createChooser(intent, title); startActivity(chooser);} else if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent);}
2. 应用共享数据时,建议使用 签名权限
在您控制或拥有的两个应用程序之间共享数据时,请使用 基于签名的权限。这些权限不需要用户确认,而是检查访问数据的应用程序是否使用相同的签名密钥进行签名。因此,这些权限提供了更简化,安全的用户体验。
举例如下:
3. 禁止其他应用访问自己的ContentProvider
除非您打算将数据从应用程序发送到您不拥有的其他应用程序,否则您应明确禁止其他开发人员的应用程序访问ContentProvider您的应用程序包含的对象。如果您的应用可以安装在运行Android 4.1.1(API级别16)或更低级别的设备上,则此设置尤其重要,因为 默认情况下,这些Android版本android:exported的元素属性true。
举例如下:
...
4.获取敏感信息,需要提前询问凭据
在向用户请求凭据以便他们可以访问应用中的敏感信息或高级内容时,请询问PIN/Password/Pattern或 生物识别凭证,例如指纹,人脸识别、虹膜等。
本节重点介绍推荐的生物识别登录方法。
所述生物识别库 指纹等显示系统提示,要求用户登录使用的生物统计凭证。对话框外观的最终一致性创建了更可靠的用户体验。图1中显示了一个示例对话框。
注意:生物识别库扩展了其功能 FingerprintManager。
要为您的应用提供生物识别身份验证,请完成以下步骤:
- 在app/build.gradle 中添加以下代码
dependencies { implementation 'androidx.biometric:biometric:1.0.0-alpha04'}
- 在创建承载生物识别登录对话框的activity或fragment时,使用以下代码段中显示的逻辑显示dialog:
private Handler handler = new Handler();private Executor executor = new Executor() { @Override public void execute(Runnable command) { handler.post(command); }};@Overrideprotected void onCreate(Bundle savedInstanceState) { // ... // Prompt appears when user clicks "Log in" Button biometricLoginButton = findViewById(R.id.biometric_login); biometricLoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showBiometricPrompt(); } });}private void showBiometricPrompt() { BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder() .setTitle("Biometric login for my app") .setSubtitle("Log in using your biometric credential") .setNegativeButtonText("Cancel") .build(); BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new BiometricPrompt.AuthenticationCallback() { @Override public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) { super.onAuthenticationError(errorCode, errString); Toast.makeText(getApplicationContext(), "Authentication error: " + errString, Toast.LENGTH_SHORT) .show(); } @Override public void onAuthenticationSucceeded( @NonNull BiometricPrompt.AuthenticationResult result) { super.onAuthenticationSucceeded(result); BiometricPrompt.CryptoObject authenticatedCryptoObject = result.getCryptoObject(); // User has verified the signature, cipher, or message // authentication code (MAC) associated with the crypto object, so // you can use it in your app's crypto-driven workflows. } @Override public void onAuthenticationFailed() { super.onAuthenticationFailed(); Toast.makeText(getApplicationContext(), "Authentication failed