- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Android SafetyNet ReCaptcha 在我的 Android 应用中显示 Google 验证码。
我的应用的用户可以注册、登录、注销。当用户启动我的应用程序时,会出现一个启动屏幕。如果用户未连接,则会邀请他触摸按钮...:
摘要:我将 onClick
监听器附加到按钮。如果单击后者,则我会在(同步!并且它是自愿的)Executor
中调用 verifyWithRecaptcha
。然后,我调用 Google 的服务器来确保验证码是由人类而不是机器人完成的,这要归功于我的类 NetworkUseRecaptcha
,它提供了 Google 服务器的结果。
由于我将 Executor
与 AppCompatActivity
(名为 SplashScreen
)一起使用,所以不是异步模式,而是实际上同步模式(因此,在与 Activity 及其 UI 相同的线程中),是否存在抛出异常
(内存泄漏或其他问题)的风险?
我已遵循此文档: https://developer.android.com/training/safetynet/recaptcha#send-request但我没有找到任何有关我的问题的有用信息。
我没有找到任何与我的问题相关的 Stackoverflow 问题。
注意:final Context that = this
是当前的 AppCompatActivity
SplashScreen
对象。
final Context that = this;
button_splash_screen_recaptcha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Executor executor = new Executor() {
@Override
public void execute(@NonNull Runnable command) {
command.run();
}
};
executor.execute(new Runnable() {
@Override
public void run() {
SafetyNet.getClient(that).verifyWithRecaptcha("PUBLIC KEY")
.addOnSuccessListener(executor,
new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
@Override
public void onSuccess(final SafetyNetApi.RecaptchaTokenResponse response) {
String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
String[] parameters = new String[2];
parameters[0] = "SECRET KEY";
parameters[1] = userResponseToken;
new NetworkUseRecaptcha(new RecaptchaPostExecuteCallback() {
@Override
public void onTaskCompleted(String result, boolean background_error) {
if(background_error) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(that,"Error N°2: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
});
return;
}
try {
final JSONObject json_response = new JSONObject(result);
if(!json_response.isNull("success") && json_response.getBoolean("success")) {
final List<AuthUI.IdpConfig> providers = ImmutableList.of(
new AuthUI.IdpConfig.GoogleBuilder().build()
);
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setAlwaysShowSignInMethodScreen(true)
.setLogo(R.drawable.yellow_logo)
.setTheme(R.style.LoginTheme)
.build(),
REQUEST_CODE_SIGN_IN
);
} else {
Toast.makeText(that,"Error N°4: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(that,"Error N°3: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
});
}
}
}).execute(parameters);
}
}
})
.addOnFailureListener(executor, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.err.println(e);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(that,"Error N°1: Unable to check the captcha.", Toast.LENGTH_SHORT).show();
}
});
}
});
}
});
}
});
class NetworkUseRecaptcha extends AsyncTask<String, Void, String> {
private final RecaptchaPostExecuteCallback post_execute_callback;
private boolean background_error;
NetworkUseRecaptcha(RecaptchaPostExecuteCallback post_execute_callback) {
this.post_execute_callback = post_execute_callback;
background_error = false;
}
@Override
protected String doInBackground(String[] parameters) {
StringBuilder string_builder = new StringBuilder();
try {
URL url = new URL("https://www.google.com/recaptcha/api/siteverify");
HttpsURLConnection https_url_connection = (HttpsURLConnection) url.openConnection();
https_url_connection.setRequestMethod("POST");
https_url_connection.setDoOutput(false);
https_url_connection.setUseCaches(false);
OutputStream os = https_url_connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
writer.write("secret=" + parameters[0] + "&response=" + parameters[1]);
writer.flush();
writer.close();
os.close();
InputStream input_stream = https_url_connection.getInputStream();
BufferedReader buffered_reader = new BufferedReader(new InputStreamReader(input_stream));
String line;
while((line = buffered_reader.readLine()) != null) {
string_builder.append(line);
}
buffered_reader.close();
} catch (Exception e) {
background_error = true;
}
return string_builder.toString();
}
@Override
protected void onPostExecute(String result) {
post_execute_callback.onTaskCompleted(result, background_error);
}
}
最佳答案
不会,从主线程调用 reCaptcha 时不存在异常风险。
原因是,虽然您已从主线程同步调用 reCaptcha API,但它是安全的,因为 SafetyNet
API 本身异步工作并生成它自己的工作线程以避免 NetworkOnMainThread
code> 异常并将其结果传递回 MainThread
。
关于java - Android ReCaptcha : Does instanciating a (synchronous! )我的 Activity 中的执行程序对象可能会导致其线程(和 UI)崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56036226/
我是一名优秀的程序员,十分优秀!