[英]making a function not to work for a few some seconds in java
我有一个登录功能,如果用户调用它超过5次,我希望我的功能在10秒钟内不起作用,那我该怎么办?如果有人回答我的问题,我会很高兴。
You need to record failed login attempts for a user (eg in database
) and once it reaches the threshold, you need to return error response from login
method for configurable amount of time from the last failed login attempt, eg: 您需要记录用户的失败登录尝试(例如,在
database
),并且一旦达到阈值,就需要从上次失败的登录尝试开始,在可配置的时间内从login
方法返回错误响应,例如:
x
minutes from that timestamp x
分钟之内,则阻止呼叫 function not work for example 10 seconds
功能不起作用,例如10秒
You need to be specific on what exactly you mean by function not to work . 您需要具体说明不起作用的确切含义。 But if you simply wanted to block the method call for
x
seconds, follow the below approach: 但是,如果您只是想阻塞
x
秒的方法调用,请遵循以下方法:
You need to create a counter
to count the number of attempts and then if(counter>5)
then use Thread.sleep(10000) to make the request to hold (sleep) for 10 seconds. 您需要创建一个
counter
来计算尝试次数,然后创建if(counter>5)
并使用Thread.sleep(10000)来使请求保持(睡眠)10秒钟。 Ideally, the counter
value should be persisted to a database (or any other storage) and read it again to validate the number of attempts. 理想情况下,
counter
值应保存在数据库(或任何其他存储)中,然后再次读取该值以验证尝试次数。
But, I strongly suggest you follow @Darshan's answer which makes more sense. 但是,我强烈建议您遵循@Darshan的回答,这更有意义。
First you need to identify the user . 首先,您需要识别用户。
This can be done on the basis of IP . 这可以基于IP来完成。
Have a ConcurrentHashMap 有一个ConcurrentHashMap
ConcurrentHashMap<String,Long>IpAddressValidationMap // String is the Ip address and Long is the timeInMilleseconds
You make a filter and put a check 您做一个过滤器并进行检查
public void prefilterFunction(RequestObject object)
{
String ip=object.getIP(); // just an example not the standard call for getting Ip addr
Long time=IpAddressValidationMap.get(ip);
if(time!=null)
{
if(time<THRESHOLD_TIME)
{
return ;
}
else
{
// We allow the user to proceed
}
}
}
public void validateLogin(RequestObject object)
{
// if the login fails
IpAddressValidationMap.put(ipAddr,System.currentTimeinMIllis());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.