暂时禁用登录失败后, [英] Temporarily disable login after failed attempts
本文介绍了暂时禁用登录失败后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建我的应用程序禁用5分钟登录按钮登录屏幕后3次失败的登录尝试。
不过,在我的应用程序,该按钮可以很容易地再次通过关闭应用程序,并再次启动启用。
我该如何解决这个问题?
code:
公共类的LogIn延伸活动{ 用户名的EditText = NULL;
密码的EditText = NULL; 按钮loginbutton = NULL; INT计数器= 2; 字符串用户=admin的;
字符串PW =密码; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_log_in);
用户名=(的EditText)findViewById(R.id.username);
密码=(EditText上)findViewById(R.id.password); loginbutton =(按钮)findViewById(R.id.loginbutton);
loginbutton.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v)
{字符串名称= username.getText()的toString();
。字符串传递= password.getText()的toString(); 如果(name.equals(用户)及和放大器; pass.equals(PW))
//成功登录
{意向意图=新意图(LogIn.this,MainApp.class);
startActivity(意向);
} 否则如果(name.equals()|| pass.equals())
//缺少用户名或密码
{吐司警报= Toast.makeText(LogIn.this,请输入用户名或密码,Toast.LENGTH_SHORT);
alert.show();
} 否则,如果(反== 0)
//关闭按钮后,3次失败的尝试
{loginbutton.setEnabled(假); 吐司警报= Toast.makeText(LogIn.this,禁用登录5分钟,Toast.LENGTH_SHORT);
alert.show(); 最后的处理程序处理程序=新的处理程序();
handler.postDelayed(新的Runnable()
@覆盖
公共无效的run()
{loginbutton.setEnabled(真);
计数器= 2;
}
} 300000);
} 其他
// 密码错误
{吐司警报= Toast.makeText(LogIn.this,错凭证,Toast.LENGTH_SHORT);
alert.show();
计数器 - ;
};
}
});
};
}
解决方案
在这种情况下,您可以保存CURRENTTIME在preference并尝试重新登录前检查
这是一个preference是如何工作的。
//启动与preference文件名和访问模式的preference其他应用共享preferences preF = getShared preferences(LoginTrack,Context.MODE_PRIVATE); //LoginTrack是preference名称//读取preference长L = prefs.getLong(LastLoginDateTime,新的Date()的getTime());
字符串尝试= pref.getString(MaxAttempts,);
//修改并重新保存preferences当您尝试登录日期DT =新的日期(); //当前时间跟踪loginTime
。prefs.edit()putLong(LastLoginDateTime,dt.getTime())提交();
prefs.edit()putString(MaxAttempts,5)提交()。
检索数据主持人它们和验证,如你所愿后
I am trying to create a login screen for my app that disables the login button for 5 minutes after 3 failed login attempts.
However, in my app, the button can be easily enabled again by closing the app and starting it again.
How do I fix this?
Code:
public class LogIn extends Activity {
EditText username = null;
EditText password = null;
Button loginbutton = null;
int counter = 2;
String user = "admin";
String pw = "password";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
loginbutton = (Button) findViewById(R.id.loginbutton);
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{ String name = username.getText().toString();
String pass = password.getText().toString();
if (name.equals(user) && pass.equals(pw))
// Successful Login
{ Intent intent = new Intent(LogIn.this, MainApp.class);
startActivity(intent);
}
else if (name.equals("") || pass.equals(""))
// Missing username or password
{ Toast alert = Toast.makeText(LogIn.this, "Please enter a username or password", Toast.LENGTH_SHORT);
alert.show();
}
else if (counter == 0)
// Disable button after 3 failed attempts
{ loginbutton.setEnabled(false);
Toast alert = Toast.makeText(LogIn.this, "Login Disabled for 5 mins", Toast.LENGTH_SHORT);
alert.show();
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{ @Override
public void run()
{ loginbutton.setEnabled(true);
counter = 2;
}
}, 300000);
}
else
// Wrong password
{ Toast alert = Toast.makeText(LogIn.this, "Wrong Credentials", Toast.LENGTH_SHORT);
alert.show();
counter--;
};
}
});
};
}
解决方案
In that case you can save the CurrentTime in the preference and check it before attempting to login again
This is how a preference works
// Initiate the preference with Preference Filename and Access Mode for other Apps
SharedPreferences pref = getSharedPreferences("LoginTrack", Context.MODE_PRIVATE); // "LoginTrack" is the preference Name
// read the preference
long l = prefs.getLong("LastLoginDateTime", new Date().getTime());
String Attempts= pref.getString("MaxAttempts", "");
//To edit and save preferences again when you try login
Date dt = new Date(); // Current Time to Track loginTime
prefs.edit().putLong("LastLoginDateTime", dt.getTime()).commit();
prefs.edit().putString("MaxAttempts", "5").commit();
After Retrieve the data compere them and validate as you wish
这篇关于暂时禁用登录失败后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文