第八章限制密码重试次数1、实现原理保证原子性:
单系统:AtomicLong计数
?集群系统:RedissionClient提供的RAtomicLong计数
1、获取系统中是否已有登录次数缓存,缓存对象结构预期为:"用户名--登录次数" 。 2、如果之前没有登录缓存 , 则创建一个登录次数缓存 。 3、如果缓存次数已经超过限制 , 则驳回本次登录请求 。 4、将缓存记录的登录次数加1,设置指定时间内有效5、验证用户本次输入的帐号密码 , 如果登录登录成功 , 则清除掉登录次数的缓存思路有了 , 那我们在哪里实现呢?我们知道AuthenticatingRealm里有比较密码的入口doCredentialsMatch方法
packagecom.itheima.shiro.core.impl;importcom.itheima.shiro.constant.CacheConstant;importcom.itheima.shiro.constant.SuperConstant;importcom.itheima.shiro.core.SimpleCacheManager;importcom.itheima.shiro.core.base.ShiroUser;importcom.itheima.shiro.core.base.SimpleToken;importcom.itheima.shiro.core.ShiroDbRealm;importcom.itheima.shiro.core.bridge.UserBridgeService;importcom.itheima.shiro.pojo.User;importcom.itheima.shiro.utils.*;importorg.apache.shiro.authc.AuthenticationInfo;importorg.apache.shiro.authc.AuthenticationToken;importorg.apache.shiro.authc.SimpleAuthenticationInfo;importorg.apache.shiro.authc.UnknownAccountException;importorg.apache.shiro.authc.credential.HashedCredentialsMatcher;importorg.apache.shiro.authz.AuthorizationInfo;importorg.apache.shiro.subject.PrincipalCollection;importorg.apache.shiro.util.ByteSource;importorg.redisson.api.RedissonClient;importorg.springframework.beans.factory.annotation.Autowired;importjavax.annotation.Resource;/***@Description:自定义shiro的实现*/publicclassShiroDbRealmImplextendsShiroDbRealm{@AutowiredprivateUserBridgeServiceuserBridgeService;@AutowiredprivateSimpleCacheManagersimpleCacheManager;@Resource(name="redissonClientForShiro")privateRedissonClientredissonClient;/***@Description认证方法*@paramauthcToken校验传入令牌*@returnAuthenticationInfo*/@OverridepublicAuthenticationInfodoGetAuthenticationInfo(AuthenticationTokenauthcToken){SimpleTokentoken=(SimpleToken)authcToken;Useruser=userBridgeService.findUserByLoginName(token.getUsername());if(EmptyUtil.isNullOrEmpty(user)){thrownewUnknownAccountException("账号不存在");}ShiroUsershiroUser=BeanConv.toBean(user,ShiroUser.class);StringsessionId=ShiroUserUtil.getShiroSessionId();StringcacheKeyResourcesIds=CacheConstant.RESOURCES_KEY_IDS+sessionId;shiroUser.setResourceIds(userBridgeService.findResourcesIdsList(cacheKeyResourcesIds,user.getId()));Stringsalt=user.getSalt();Stringpassword=user.getPassWord();returnnewSimpleAuthenticationInfo(shiroUser,password,ByteSource.Util.bytes(salt),getName());}/***@Description授权方法*@paramprincipalsSimpleAuthenticationInfo对象第一个参数*@return*/@OverridepublicAuthorizationInfodoGetAuthorizationInfo(PrincipalCollectionprincipals){ShiroUsershiroUser=(ShiroUser)principals.getPrimaryPrincipal();returnuserBridgeService.getAuthorizationInfo(shiroUser);}/***@Description清理缓存*/@OverridepublicvoiddoClearCache(PrincipalCollectionprincipalcollection){StringsessionId=ShiroUtil.getShiroSessionId();simpleCacheManager.removeCache(CacheConstant.ROLE_KEY+sessionId);simpleCacheManager.removeCache(CacheConstant.RESOURCES_KEY+sessionId);simpleCacheManager.removeCache(CacheConstant.TOKEN+sessionId);}/***@Description加密方式*/@OverridepublicvoidinitCredentialsMatcher(){RetryLimitCredentialsMatchermatcher=newRetryLimitCredentialsMatcher(SuperConstant.HASH_ALGORITHM,redissonClient);matcher.setHashIterations(SuperConstant.HASH_INTERATIONS);setCredentialsMatcher(matcher);}}3、测试访问 , 使用admin账号输入错误密码5次
推荐阅读
-
心脑血管|血管堵塞,身体会出现4种情况,拖成脑梗就麻烦了
-
『星轨体育TB』日本资深专家解析伊藤美诚技术强点:别看电视解说他们都是瞎扯
-
海峡导报NEWS|两男子被警方抓获,安装猫池等设备供人实施诈骗
-
-
-
-
逃脱不了对他的思念|前国安球员指裁判别出去丢人,戴琳回应粤媒;我想铲人梅方站不起来
-
猎云网蚂蚁特工与全国KTV系统70%占有率的K米达成战略合作,共同深度打造KTV新互娱
-
『运输任务』是否考虑包机从一些重点国家撤侨?民航局回应
-
-
-
-
知顿|玩的一手好牌,超越中国、日本,3万亿美债“接盘侠”浮出水面
-
-
日本|美国终于醒悟,最大威胁不是中俄,强敌早已不声不响造7艘航母
-
基金|“真贵”还是“珍贵”?首批科创板50ETF即将落地,科创板再迎增量资金
-
蒂娜时尚说|还没学会怎么搭配?先了解自己是什么身材吧!“对症下药”更时髦
-
西沙群岛的海里一半是水一半是鱼是用的什么修辞手法 西沙群岛的海里一半是水一半是鱼是啥修辞手法
-
我想请问一下各路大神,我这个条件在北京能找到啥样子的另一半
-
野鸡大学|教育部下达招录“铁调”!至少释放了4大信号,全体家长需清醒