Java中SpringSecurity密码错误5次锁定用户的实现方法
SpringSecurity简介
SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了SpringIoC,DI(控制反转InversionofControl,DI:DependencyInjection依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
下面看下实例代码:
第一步:创建AuthenticationSuccessEventListener.Java 用来处理登录成功的事件。
packagecom.dcits.yft.auth; importcom.dcits.yft.system.dao.UserDao; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.context.ApplicationListener; importorg.springframework.security.authentication.event.AuthenticationSuccessEvent; importorg.springframework.stereotype.Component; importjava.util.Map; /** *登陆成功监听 * *@authorShaoj3/2/2017. */ @Component publicclassAuthenticationSuccessEventListenerimplementsApplicationListener{ @Autowired privateUserDaouserDao; @Override publicvoidonApplicationEvent(AuthenticationSuccessEventauthenticationSuccessEvent){ YftUserDetailsyftUserDetails=(YftUserDetails)authenticationSuccessEvent.getAuthentication().getPrincipal(); Stringaccount=yftUserDetails.getUsername(); Map user=userDao.queryUserByAccount(account); userDao.updateStatusByAccount(account,user.get("ENABLE").toString(),0); } }
第二步:新建AuthenticationFailureListener.java用来处理登录失败的事件。
packagecom.dcits.yft.auth; importcom.dcits.yft.system.dao.ParamsDao; importcom.dcits.yft.system.dao.UserDao; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.context.ApplicationListener; importorg.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent; importorg.springframework.stereotype.Component; importjava.util.Map; /** *登陆失败监听 * *@authorShaoj3/2/2017. */ @Component publicclassAuthenticationFailureListenerimplementsApplicationListener{ @Autowired privateUserDaouserDao; @Autowired privateParamsDaoparamsDao; @Override publicvoidonApplicationEvent(AuthenticationFailureBadCredentialsEventauthenticationFailureBadCredentialsEvent){ Stringaccount=authenticationFailureBadCredentialsEvent.getAuthentication().getPrincipal().toString(); Map user=userDao.queryUserByAccount(account); if(user!=null){ //用户失败次数 intfails=Integer.parseInt(user.get("FAILS").toString()); fails++; //系统配置失败次数 intFAILS_COUNT=Integer.parseInt(paramsDao.queryParamsValue("FAILS_COUNT")); //超出失败次数,停用账户 if(fails>=FAILS_COUNT){ userDao.updateStatusByAccount(account,"false",fails); //失败次数++ }else{ userDao.updateStatusByAccount(account,user.get("ENABLE").toString(),fails); } } } }
第三步:在UserDao.java中加入登录状态更新的代码
/** *更新用户登录次数 * *@paramaccount账户 *@paramlogin_counts登录次数 *@return */ publicvoidupdateLoginCounts(Stringaccount){ daoUtil.update("updatet_yft_usersetlogin_counts=login_counts+1whereaccount=?",account); }
第四步:数据库中添加登录次数字段
altertableT_YFT_USERadd(FAILSnumber(11)default0); commentoncolumnT_YFT_USER.FAILSis'失败尝试次数'; [sql]viewplaincopy INSERTINTOt_yft_params(ID,CODE,NAME,VALUE,UNIT,REMARK,CRT_DATE) VALUES(66,'FAILS_COUNT','登陆尝试次数','5','','',to_date('2017-03-02','yyyy-mm-dd'));
以上所述是小编给大家介绍的Java中SpringSecurity密码错误5次锁定用户的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!