第八章限制密码重试次数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次
推荐阅读
-
-
林有有许幻山手机备注具体什么情况?林有有许幻山手机备注背后的真相
-
『星座小知识分享』谁娶谁有福气,农历这几天出生的女人最旺夫
-
【韭菜饺子】三月份抓紧吃这菜,手脚冰凉最怕它,隔三差五吃一次,一年都健康
-
后知后觉直播|新疆天业等个股纷纷上涨,棉花概念股异动走强
-
■网上买个几块钱的东西,不仅便宜还免邮,店家图啥?
-
#运势#【傲视星座】十二星座一周运势06/17-06/23:暮色苍茫,孤注一掷
-
-
智通财经|澳洲成峰高教(01752)6月8日回购11万股
-
乌克兰总理:将派专家参与坠机搜寻工作 吁停止无谓猜测
-
|阳台包进客厅,中间有根60公分的梁吊顶咋做?老师傅2招化解压顶
-
5个国家出品的微信小程序,强大且实用,每个都不容错过
-
-
气温|36度的气温下,这汤不要放过,每天喝一碗,告别黑皮肤,不懂可惜
-
-
-
-
王者荣耀躺赢解说|队友直呼你为野王!,王者荣耀打野很难?掌握这几个英雄打野
-
-
#车讯精选#不用炫富,大家都知道你发财了,这3款车只要拥有1辆