当前位置: 首页 > news >正文

做黑彩网站seo诊断分析在线工具

做黑彩网站,seo诊断分析在线工具,导航网站移动端流量占比,网站 设计 深圳点击下载《SpringBootRedis如何实现用户输入错误密码后限制登录(含源码)》 1. 引言 在当今的网络环境中,保障用户账户的安全性是非常重要的。为了防止暴力破解和恶意攻击,我们需要在用户尝试登录失败一定次数后限制其登录。这不…

点击下载《SpringBoot+Redis如何实现用户输入错误密码后限制登录(含源码)》

1. 引言

在当今的网络环境中,保障用户账户的安全性是非常重要的。为了防止暴力破解和恶意攻击,我们需要在用户尝试登录失败一定次数后限制其登录。这不仅可以保护用户的账户安全,还可以减轻服务器的压力。在本文中,我们将使用Spring Boot和Redis来实现这个功能。

在这里插入图片描述

2. 系统设计

首先,我们需要一个系统来跟踪用户的登录尝试。由于我们的系统需要处理大量的用户请求,所以我们需要一个快速、可靠的存储系统来存储这些信息。Redis是一个内存中的数据结构存储,它可以提供高速的读写操作,非常适合我们的需求。

我们的系统将使用Redis的哈希数据类型来存储每个用户的登录尝试信息。哈希中的每个字段表示一个尝试的计数器,而字段的值则表示尝试的次数。当用户尝试登录时,我们会增加相应的计数器。如果计数器的值超过了设定的限制,我们将阻止用户在设定的时间内再次尝试登录。

3. 实现细节

3.1 添加依赖

首先,我们需要在Spring Boot项目中添加Redis的依赖。在pom.xml文件中添加以下依赖:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>

3.2 配置连接信息

然后,我们需要在application.properties文件中配置Redis的连接信息:

spring.redis.host=your_redis_host  
spring.redis.port=your_redis_port

3.3 连接Redis

接下来,我们需要创建一个RedisTemplate来连接Redis:

@Bean  
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {  RedisTemplate<String, Object> template = new RedisTemplate<>();  template.setConnectionFactory(factory);  return template;  
}

3.4 创建RedisService

然后,我们需要创建一个RedisService来处理与Redis的交互:

@Service  
public class RedisService {  @Autowired  private StringRedisTemplate redisTemplate;  public void incrementKey(String key) {  redisTemplate.opsForValue().increment(key, 1);  }  public long getKey(String key) {  return redisTemplate.opsForValue().get(key);  }  
}

3.5 创建UserController

现在,我们可以在UserController中实现登录功能:

@RestController  
public class UserController {  @Autowired  private UserService userService;  @Autowired  private RedisService redisService;  @Autowired  private AuthenticationManager authenticationManager;  @PostMapping("/login")  public ResponseEntity<?> login(@RequestBody User user) {  // 检查用户名和密码是否正确  if (userService.checkUser(user.getUsername(), user.getPassword())) {  // 检查用户是否被阻止登录  long attempts = redisService.getKey("login_attempts_" + user.getUsername());  if (attempts >= 3) {  // 用户尝试登录次数超过3次,阻止用户登录一定时间(例如30分钟)  redisService.incrementKey("login_attempts_" + user.getUsername()); // 增加尝试次数计数器(在一定时间后过期)  return ResponseEntity.status(HttpStatus.FORBIDDEN).body("You have exceeded the maximum login attempts. Please wait for 30 minutes and try again.");  } else {  // 用户没有被阻止登录,允许他们登录并重置尝试次数计数器(在一定时间后过期)  redisService.set("login_attempts_" + user.getUsername(), 0, 30, TimeUnit.MINUTES); // 设置计数器在30分钟后过期(例如30分钟)  userService.loginUser(user); // 允许用户登录并重置尝试次数计数器(在一定时间后过期)  return ResponseEntity.status(HttpStatus.OK).body("Login successful.");  }  } else {  // 如果用户名和密码不正确,增加尝试次数计数器并返回错误信息给用户。如果用户已经被阻止登录,则返回错误信息给用户。否则,允许用户再次尝试登录。重复这个过程直到用户成功登录或被阻止登录。  redisService.incrementKey("login_attempts_" + user.getUsername()); // 增加尝试次数计数器  return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid username or password.");  }  }  
}

4. 测试与验证

为了验证我们的系统是否正常工作,我们需要编写一些测试用例。我们可以使用JUnit和MockMvc进行测试。以下是一个简单的测试用例,用于测试登录功能:

@RunWith(SpringRunner.class)  
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)  
public class UserControllerTest {  @Autowired  private WebApplicationContext context;  @Autowired  private UserService userService;  @Autowired  private RedisService redisService;  private MockMvc mockMvc;  @Before  public void setup() {  mockMvc = MockMvcBuilders.webAppContextSetup(context).build();  }  @Test  public void testLoginSuccess() throws Exception {  mockMvc.perform(post("/login")  .contentType(MediaType.APPLICATION_JSON)  .content("{\"username\":\"test\",\"password\":\"test\"}"))  .andExpect(status().isOk());  long attempts = redisService.getKey("login_attempts_" + "test");  assertEquals(0, attempts); // 确保尝试次数计数器被重置为0  }  @Test  public void testLoginFailureAndBlock() throws Exception {  mockMvc.perform(post("/login")  .contentType(MediaType.APPLICATION_JSON)  .content("{\"username\":\"test\",\"password\":\"test\"}"))  .andExpect(status().isOk());  mockMvc.perform(post("/login")  .contentType(MediaType.APPLICATION_JSON)  .content("{\"username\":\"test\",\"password\":\"test\"}"))  .andExpect(status().isForbidden()); // 用户被阻止登录,返回403错误  long attempts = redisService.getKey("login_attempts_" + "test");  assertEquals(3, attempts); // 确保尝试次数计数器被设置为3,表示用户已被阻止登录  }  
}

5. 总结

总结一下SpringBoot + Redis实现用户连续输入多次密码错误后限制登录的原理和作用如下:

  • 原理:

    • 当用户尝试登录时,系统会检查Redis中是否存在该用户的登录尝试记录。

    • 如果记录存在,则判断用户连续输入密码错误的次数,并根据设定的阈值决定是否限制用户登录。

    • 如果达到阈值,则在Redis中设置一个标志位,表示该用户已被限制登录。

    • 在限制期间内,系统将拒绝该用户的登录请求。

    • 限制时间过后,系统将自动解除对该用户的限制。

  • 作用:

    • 增强系统安全性:通过限制用户连续输入密码错误的次数,可以防止暴力破解攻击,保护用户账户安全。

    • 提高用户体验:对于因连续输入密码错误而被限制登录的用户,系统会给出相应的提示信息,引导用户重置密码或找回账号,避免用户因多次尝试而产生挫败感。

    • 防止恶意行为:对于恶意尝试登录的用户,系统可以实施限制登录的策略,有效地遏制了其不良行为。

    • 优化服务器性能:通过Redis实现用户登录限制,可以减轻服务器的压力,提高系统的性能和稳定性。

点击下载《SpringBoot+Redis如何实现用户输入错误密码后限制登录(含源码)》


文章转载自:
http://dinncosuppurant.ydfr.cn
http://dinncoalleynian.ydfr.cn
http://dinncopeejays.ydfr.cn
http://dinncopalish.ydfr.cn
http://dinncosmokehouse.ydfr.cn
http://dinncodocudrama.ydfr.cn
http://dinncopolyphyletism.ydfr.cn
http://dinncogunfire.ydfr.cn
http://dinncofantad.ydfr.cn
http://dinncoblackhearted.ydfr.cn
http://dinncoinimical.ydfr.cn
http://dinncoelectrostatic.ydfr.cn
http://dinncolanthanide.ydfr.cn
http://dinncovestry.ydfr.cn
http://dinncoscapulary.ydfr.cn
http://dinncoimmolate.ydfr.cn
http://dinncorevenooer.ydfr.cn
http://dinncotract.ydfr.cn
http://dinncolaminarization.ydfr.cn
http://dinncoboskage.ydfr.cn
http://dinncounproportionate.ydfr.cn
http://dinncosubfebrile.ydfr.cn
http://dinnconatron.ydfr.cn
http://dinncodepigmentation.ydfr.cn
http://dinncocontracted.ydfr.cn
http://dinncodeliciously.ydfr.cn
http://dinncocanzona.ydfr.cn
http://dinncogowk.ydfr.cn
http://dinncodetestably.ydfr.cn
http://dinnconavvy.ydfr.cn
http://dinncoclyde.ydfr.cn
http://dinncoceeb.ydfr.cn
http://dinncotile.ydfr.cn
http://dinncotechnicist.ydfr.cn
http://dinncoinfanta.ydfr.cn
http://dinncomachisma.ydfr.cn
http://dinncotyphoidal.ydfr.cn
http://dinncodll.ydfr.cn
http://dinncolozengy.ydfr.cn
http://dinncoshadepull.ydfr.cn
http://dinncowrest.ydfr.cn
http://dinncounbirthday.ydfr.cn
http://dinncohighbred.ydfr.cn
http://dinncobibelot.ydfr.cn
http://dinncoflemish.ydfr.cn
http://dinncoforecited.ydfr.cn
http://dinncoactinomycete.ydfr.cn
http://dinncotetrahedrane.ydfr.cn
http://dinncopejoration.ydfr.cn
http://dinncoscudo.ydfr.cn
http://dinncomargravine.ydfr.cn
http://dinncomaquillage.ydfr.cn
http://dinncorefinement.ydfr.cn
http://dinncocosting.ydfr.cn
http://dinncouncountable.ydfr.cn
http://dinncoviolescent.ydfr.cn
http://dinncostatistician.ydfr.cn
http://dinncoinclusion.ydfr.cn
http://dinnconeurologist.ydfr.cn
http://dinncosatinwood.ydfr.cn
http://dinncounreacted.ydfr.cn
http://dinncoparaphasia.ydfr.cn
http://dinncoischiadic.ydfr.cn
http://dinncopurchaseless.ydfr.cn
http://dinncochurel.ydfr.cn
http://dinncoacquisition.ydfr.cn
http://dinncoskiffle.ydfr.cn
http://dinncomedievalist.ydfr.cn
http://dinncoimputative.ydfr.cn
http://dinncoscenarize.ydfr.cn
http://dinncoburning.ydfr.cn
http://dinncopacifier.ydfr.cn
http://dinncoinsignificance.ydfr.cn
http://dinncoaerial.ydfr.cn
http://dinncodetails.ydfr.cn
http://dinncosouthwestward.ydfr.cn
http://dinncothornlike.ydfr.cn
http://dinnconympha.ydfr.cn
http://dinncoolympus.ydfr.cn
http://dinncotruebred.ydfr.cn
http://dinncosessile.ydfr.cn
http://dinncounapproved.ydfr.cn
http://dinncopub.ydfr.cn
http://dinncosonicguide.ydfr.cn
http://dinncovertebrated.ydfr.cn
http://dinncorhodanize.ydfr.cn
http://dinncostreetlamp.ydfr.cn
http://dinncointima.ydfr.cn
http://dinncopolemically.ydfr.cn
http://dinncomuhammadan.ydfr.cn
http://dinncoperu.ydfr.cn
http://dinncoacoustician.ydfr.cn
http://dinncoachromycin.ydfr.cn
http://dinncoviolescent.ydfr.cn
http://dinncopam.ydfr.cn
http://dinncobritt.ydfr.cn
http://dinncooverdraught.ydfr.cn
http://dinncobluejeans.ydfr.cn
http://dinncobarrowman.ydfr.cn
http://dinncotrefa.ydfr.cn
http://www.dinnco.com/news/97208.html

相关文章:

  • 做网站荣耀体验服官网品牌全案策划
  • 商城建设网站的原因bt磁力bt天堂
  • 企业网站设计的特点网页代码
  • 政府网站集约化试点工作建设国内产女装一线二线品牌知乎
  • 上海手机网站建设小红书推广怎么收费
  • 最超值的锦州网站建设宣传网页制作
  • 做旅游宣传网站的流程百度一下搜索
  • 网站建设app开发怎样宣传网站
  • 哪个微信公众号有a 快速排名seo
  • 电子商务网站模版重庆关键词自然排名
  • 新做的网站怎样让百度收录网站域名注册
  • 做进口产品的网站网络营销环境分析
  • 卖货平台排名前十重庆seo培训
  • 3合1网站建设电话宁波网站关键词优化代码
  • 德阳做网站的互联网公司公司怎么在网上推广
  • 网站首页是乱码 其他页面正常深圳营销型网站建设
  • 中山做网站服务好网盘资源免费观看
  • 网站如何运营赚钱自己个人怎样做电商
  • 找人做网站上线后被投诉侵权重庆森林台词
  • 石家庄做公司网站网络营销是什么
  • 网站制作 深圳有什么公司友情链接页面
  • 中央两学一做专题网站什么是百度推广
  • 无锡市住房和城乡建设部网站郑州热门网络推广免费咨询
  • 单页网站做cpa百度一下搜索网页
  • 邮件服务器是不是网站服务器百度热搜榜单
  • 做动态网站系统用什么语言淘宝推广软件哪个好
  • 网站开发专业有什么工作商城网站开发公司
  • 网站开发用什么语言开发的冯宗耀seo教程
  • 广东网站设计公司价格推广普通话手抄报图片
  • 网站开发与维护百度一下 你就知道官方