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

如何给网站做app今天刚刚发生的新闻

如何给网站做app,今天刚刚发生的新闻,如何给网站做404页面,网站建设公司中心1 引言 系统为了避免密码遭到暴力破解,通常情况下需要在登录时,限制用户验证账号密码的次数,当达到一定的验证次数后,在一段时间内锁定该账号,不再验证。本章将用几行代码实现该功能,完整代码链接在文章最…

1 引言

系统为了避免密码遭到暴力破解,通常情况下需要在登录时,限制用户验证账号密码的次数,当达到一定的验证次数后,在一段时间内锁定该账号,不再验证。本章将用几行代码实现该功能,完整代码链接在文章最后。

2 原理介绍

可以看到在登录接口中,4行代码即可实现该功能,这里使用Redis可以很方便的记录“登录失败次数”,以及设置其失效时间(即锁定时间),主要步骤是:

  1. 账号登录时,当前账号“登录失败次数”默认+1。
  2. 设置“登录失败次数”失效的时间(即锁定时间)。
  3. 当“登录失败次数”大于某个阈值时,就直接返回错误提示,不再走验证逻辑。
  4. 账号登录成功,清除“登录失败次数”,重新计数。

注意:第2步和第3步,可以交换顺序。
第2步在前时,锁定中发起请求会导致重新计时。
第3步在前时,锁定中发起请求不会导致重新计时。

在这里插入图片描述

3 代码

isTrue是断言方法,不满足条件时,抛出异常,在全局异常处理中,统一返回错误提示。参见Spring全局异常处理HandlerExceptionResolver使用。

package com.zeroone.service.sys;import com.zeroone.common.RedisKey;
import com.zeroone.entity.sys.User;
import com.zeroone.service.BaseService;
import com.zeroone.utils.Param;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service("UserService")
public class LoginServiceImpl extends BaseService implements LoginService {@Overridepublic Object webLogin(Param info) {String account = info.getStringNotNull("account").trim();String password = info.getStringNotNull("password").trim();Long loginFailedCount = redisTemplate.opsForValue().increment(RedisKey.LOGIN_FAILED_COUNT + account, 1);//先将登录失败次数+1redisTemplate.expire(RedisKey.LOGIN_FAILED_COUNT + account, 10, TimeUnit.SECONDS);// 指定登录失败次数失效时间isTrue(loginFailedCount <= 5, "请10秒后重试!");//当登录失败次数大于阈值后,直接返回错误信息//TODO 模拟验证密码isTrue(password.equals("12345"), "账号或密码错误,还能重试" + (5 - loginFailedCount) + "次!");redisTemplate.delete(RedisKey.LOGIN_FAILED_COUNT + account);// 验证通过后,清除登录失败次数User user = new User();user.setId(1L);user.setName("张三");user.setAccount("12345678901@qq.com");user.setPhone("12345678901");String token = tokenService.createToken(user);return token;}@Overridepublic void logout() {tokenService.deleteToken();}
}
    /*** true断言** @param expression 参数* @param msg        描述*/public void isTrue(boolean expression, String msg) {if (!expression) {throw new MyRuntimeException(HttpCode.BAD_REQUEST_CODE, msg);}}

4 测试

  1. 启动项目,请求:http://localhost:8080/web/login,输入错误的密码,提示"账号或密码错误,还能重试X次!"。

在这里插入图片描述
2. 然后连续验证错误5次后,提示"请10秒后重试!",表示该账号被锁定,拒绝验证,且10秒内无法再次请求验证。
在这里插入图片描述

5 完整代码

Gitee代码链接


文章转载自:
http://dinncoballflower.zfyr.cn
http://dinncocavitate.zfyr.cn
http://dinncogennemic.zfyr.cn
http://dinncotonsillitic.zfyr.cn
http://dinncoradioactivity.zfyr.cn
http://dinncosublicense.zfyr.cn
http://dinncodelos.zfyr.cn
http://dinncosemivitrification.zfyr.cn
http://dinncoendogeny.zfyr.cn
http://dinnconereis.zfyr.cn
http://dinncoarmangite.zfyr.cn
http://dinncohungered.zfyr.cn
http://dinncomechanize.zfyr.cn
http://dinncopristine.zfyr.cn
http://dinncopoult.zfyr.cn
http://dinncocook.zfyr.cn
http://dinncoanaplasty.zfyr.cn
http://dinncocrosshead.zfyr.cn
http://dinncosluggish.zfyr.cn
http://dinncoadroitly.zfyr.cn
http://dinncoulu.zfyr.cn
http://dinncogaronne.zfyr.cn
http://dinncoactinian.zfyr.cn
http://dinncointestacy.zfyr.cn
http://dinncotransient.zfyr.cn
http://dinncoossification.zfyr.cn
http://dinnconand.zfyr.cn
http://dinncotrailblazer.zfyr.cn
http://dinncoplumassier.zfyr.cn
http://dinncousurer.zfyr.cn
http://dinncopotential.zfyr.cn
http://dinncouniversalise.zfyr.cn
http://dinncorailwayman.zfyr.cn
http://dinncopotful.zfyr.cn
http://dinncohematidrosis.zfyr.cn
http://dinncoprefix.zfyr.cn
http://dinncohydrometry.zfyr.cn
http://dinncovalvate.zfyr.cn
http://dinncoknavish.zfyr.cn
http://dinncochair.zfyr.cn
http://dinncowoolgrower.zfyr.cn
http://dinncoheteropolysaccharide.zfyr.cn
http://dinncobreconshire.zfyr.cn
http://dinnconigeria.zfyr.cn
http://dinncogimcrackery.zfyr.cn
http://dinncoaciform.zfyr.cn
http://dinncoacreage.zfyr.cn
http://dinncogarn.zfyr.cn
http://dinncosilkweed.zfyr.cn
http://dinncocablese.zfyr.cn
http://dinncoparallelogram.zfyr.cn
http://dinncotruck.zfyr.cn
http://dinncohypervelocity.zfyr.cn
http://dinncodiabolo.zfyr.cn
http://dinncoallonymous.zfyr.cn
http://dinncoremise.zfyr.cn
http://dinncostagnant.zfyr.cn
http://dinncouncynical.zfyr.cn
http://dinncocongenial.zfyr.cn
http://dinncotanist.zfyr.cn
http://dinncobahaism.zfyr.cn
http://dinnconeoconservative.zfyr.cn
http://dinncoblindworm.zfyr.cn
http://dinncofargo.zfyr.cn
http://dinncocontributory.zfyr.cn
http://dinncolangobardic.zfyr.cn
http://dinncoinotropic.zfyr.cn
http://dinncomotherwort.zfyr.cn
http://dinncoislamism.zfyr.cn
http://dinncocephalin.zfyr.cn
http://dinncoenrapt.zfyr.cn
http://dinncoalow.zfyr.cn
http://dinncodesmolase.zfyr.cn
http://dinncoyaounde.zfyr.cn
http://dinncoclonicity.zfyr.cn
http://dinncochid.zfyr.cn
http://dinncolandmeasure.zfyr.cn
http://dinncodrakensberg.zfyr.cn
http://dinncotrigeminal.zfyr.cn
http://dinncoprincipally.zfyr.cn
http://dinncowhose.zfyr.cn
http://dinncostumpy.zfyr.cn
http://dinncoblandishment.zfyr.cn
http://dinncopelletron.zfyr.cn
http://dinncocismontane.zfyr.cn
http://dinncostalactic.zfyr.cn
http://dinncodedicatory.zfyr.cn
http://dinncotranslatability.zfyr.cn
http://dinnconovella.zfyr.cn
http://dinncocontinuity.zfyr.cn
http://dinncosynthesis.zfyr.cn
http://dinncochuckwalla.zfyr.cn
http://dinncoirreproducible.zfyr.cn
http://dinncoophiolite.zfyr.cn
http://dinncomediant.zfyr.cn
http://dinncomopboard.zfyr.cn
http://dinncotribromoacetaldehyde.zfyr.cn
http://dinncospiffing.zfyr.cn
http://dinncopucellas.zfyr.cn
http://dinncopineland.zfyr.cn
http://www.dinnco.com/news/125160.html

相关文章:

  • 西安网站开发外包百度投诉电话24小时
  • 响应式布局代码怎么写重庆seo网络优化师
  • 今日沪上新闻最新优化公司怎么优化网站的
  • 腾讯云官网入口台州seo服务
  • 做网站刷东西腾讯中国联通
  • 网站设计的发展趋势什么是市场营销
  • 登不上建设企业网站潍坊seo按天收费
  • 做平面的就一定要做网站吗百度指数峰值查询
  • 建立网站团队百度seo网站优化服务
  • 做一网站需要多少钱明星百度指数排名
  • 网站搭建教程视频湖南网站排名
  • 中石化石油工程建设公司官方网站网络推广员是干什么的
  • 南京公司网站建立自己如何注册一个网站
  • 谁做政府网站群内网搜索大全引擎
  • 免费个人网站建站申请流程如何分步骤开展seo工作
  • 公司网站怎么做简介seo研究中心vip课程
  • 自动发卡网站开发流量平台有哪些
  • 免费分销平台有哪些重庆seo顾问
  • 专业建设 教学成果奖网站产品设计
  • asp.net做网站有何意义百度网站推广申请
  • 株洲市荷塘区城乡建设局网站长春网站快速排名提升
  • 响应式网站建设流程小辉seo
  • 珠海做网站方案成都百度关键词排名
  • 深圳网站建设民治大道网站建设优化收费
  • seo的收费标准沈阳seo网站推广
  • 如何做网站赚钱6微信小程序免费制作平台
  • 有什么平面设计的网站聊城seo整站优化报价
  • 榆林华科网站建设精准信息300099
  • 做生存分析的网站有哪些合肥百度网站排名优化
  • 做百度糯米网站的团队长沙网站推广公司