springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)
1. 首先新建一个shiroconfig shiro的配置类,代码如下:
@configuration public class springshiroconfig { /** * @param realms 这儿使用接口集合是为了实现多验证登录时使用的 * @return */ @bean public securitymanager securitymanager(collection<realm> realms) { defaultwebsecuritymanager smanager = new defaultwebsecuritymanager(); smanager.setrealms(realms); return smanager; } @bean public shirofilterfactorybean shirofilterfactory(securitymanager securitymanager) { shirofilterfactorybean sfbean = new shirofilterfactorybean(); sfbean.setsecuritymanager(securitymanager); //如果是匿名访问时,访问了不能访问的资源跳转的位置 sfbean.setloginurl("/index"); //定义map指定请求过滤规则(哪些资源允许匿名访问,哪些必须认证访问) linkedhashmap<string, string> map = new linkedhashmap<>(); //静态资源允许匿名访问:"anon" 静态资源授权时不能写static下面所有的开放,要将static下面的所有文件夹一个一个的开放,templates同理 //map的key可以为文件的位置,也可以为请求的路径 map.put("/bower_components/**", "anon"); map.put("/json/**", "anon"); map.put("/pages", "anon"); map.put("/user/userpasswordlogin", "anon"); map.put("/user/login", "anon"); map.put("/user/reg", "anon"); //访问这个路径时不会进入controller,会在这儿直接拦截退出,问为什么的,自己想请求流程去 map.put("/user/userlogout", "logout"); //拦截除上面之外的所有请求路径 map.put("/**", "user"); sfbean.setfilterchaindefinitionmap(map); return sfbean; } @bean public lifecyclebeanpostprocessor lifecyclebeanpostprocessor() { return new lifecyclebeanpostprocessor(); }
2. 写realms的实现类,一般继承自authorizingrealm(这个是实现用户名,密码登录),代码如下:
@service public class shiouserrealm extends authorizingrealm { //注入userdao @autowired private userdao userdao; /** * 设置凭证匹配器 * * @param credentialsmatcher */ @override public void setcredentialsmatcher(credentialsmatcher credentialsmatcher) { /*这里设置了md5盐值加密,这儿就必须使用hashedcredentialsmatcher才能有下面两个方法*/ hashedcredentialsmatcher matcher = new hashedcredentialsmatcher(); //这里是设置加密方式 matcher.sethashalgorithmname("md5"); //这里是设置加密的次数 matcher.sethashiterations(2); super.setcredentialsmatcher(matcher); } /** * 这儿是设置授权的 * @param principalcollection * @return */ @override protected authorizationinfo dogetauthorizationinfo(principalcollection principalcollection) { return null; } /** * 通过此方法完成认证数据的获取及封装,系统底层会将认证数据传递认证管理器,有认证管理器完成认证操作 * @param authenticationtoken * @return * @throws authenticationexception */ @override protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception { //先判断这个是否是来及这个令牌的数据:我们这儿分为了usernamepasswordtoken(shiro给我们提供的。)、userphonetoken if (!(authenticationtoken instanceof usernamepasswordtoken)) { return null; } //获取controller传过来的数据 usernamepasswordtoken uptoken = (usernamepasswordtoken) authenticationtoken; //uptoken.setrememberme(true);shiro默认为false,是是否记住我的功能 //这儿为用户提交的username string username = uptoken.getusername(); //去数据更加name取到用户的信息 user user = userdao.finduserbyusername(username); //判断数据库是否有这用户 if (user == null) { throw new unknownaccountexception(); } //判断用户的状态是否被禁用(数据库的字段) if (user.getstate() == 0) { throw new lockedaccountexception(); } //这儿是取到用户信息中的盐值,盐值要转换为bytesource这个类型才能使用 bytesource credentialssalt = bytesource.util.bytes(user.getsalt()); //这儿是将这个用户的信息交给shiro(user为用户对象,user.getpassword()是要加密的对象,credentialssalt为盐值,getname()当前对象) simpleauthenticationinfo info = new simpleauthenticationinfo(user, user.getpassword(), credentialssalt, getname()); return info; } }
3. 此时用户的账号密码登录已经可以使用了controller代码如下:
@requestmapping("userpasswordlogin") @responsebody public jsonresult userpasswordlogin(string username, string password) { subject subject = securityutils.getsubject(); usernamepasswordtoken token = new usernamepasswordtoken(username, password); subject.login(token); return new jsonresult("login ok"); }
4. 我们现在来实现短信验证码登录实现:
4.1 先写userphonetoken,我放在l和springshiroconfig同一目录下:
@component public class userphonetoken extends usernamepasswordtoken implements serializable { private static final long serialversionuid = 6293390033867929958l; // 手机号码 private string phonenum; //无参构造 public userphonetoken(){} //获取存入的值 @override public object getprincipal() { if (phonenum == null) { return getusername(); } else { return getphonenum(); } } @override public object getcredentials() { if (phonenum == null) { return getpassword(); }else { return "ok"; } } public userphonetoken(string phonenum) { this.phonenum = phonenum; } public userphonetoken(final string username, final string password) { super(username, password); } public string getphonenum() { return phonenum; } public void setphonenum(string phonenum) { this.phonenum = phonenum; } @override public string tostring() { return "phonetoken [phonenum=" + phonenum + "]"; } }
4.2 在写shirouserphonerealm,代码如下:
@service public class shiouserphonerealm extends authorizingrealm { @autowired private userdao userdao; @override public void setcredentialsmatcher(credentialsmatcher credentialsmatcher) { //这儿的credentialsmatcher的new的对象必须是allowallcredentialsmatcher credentialsmatcher matcher = new allowallcredentialsmatcher(); super.setcredentialsmatcher(matcher); } @override protected authorizationinfo dogetauthorizationinfo(principalcollection principalcollection) { return null; } /** * 通过此方法完成认证数据的获取及封装,系统底层会将认证数据传递认证管理器,有认证管理器完成认证操作 * @param authenticationtoken * @return * @throws authenticationexception */ @override protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception { userphonetoken token = null; if (authenticationtoken instanceof userphonetoken) { token = (userphonetoken) authenticationtoken; }else { return null; } //获取我发送验证码是存入session中的验证码和手机号 string verificationcode = (string) securityutils.getsubject().getsession().getattribute("verificationcode"); string phone = (string) securityutils.getsubject().getsession().getattribute("phone"); //获取controller传过来的数据 string verificationcode1 = (string) token.getprincipal(); //去数据库根据手机号查询用户信息 user user = userdao.finduserbyuserphone(phone); if (stringutils.isempty(verificationcode)) { throw new serviceexception("网络错误"); } //比对手机号 if (!verificationcode.equals(verificationcode1)) { throw new serviceexception("验证码不正确"); } if (user == null) { throw new unknownaccountexception(); } if (user.getstate() == 0) { throw new lockedaccountexception(); } return new simpleauthenticationinfo(user,phone,getname()); } }
4.3 手机号码登录验证已经基本完成:controller代码如下:
@postmapping("verificationcodelogin") @responsebody public jsonresult verificationcodelogin(string password) { subject subject = securityutils.getsubject(); userphonetoken token = new userphonetoken(password); subject.login(token); return new jsonresult("login ok"); }
使用过程中遇到的bug
1.
org.apache.shiro.authc.unknownaccountexception: realm [cn.tedu.wxacs.service.impl.shiouserphonerealm@768d8431] was unable to find account data for the submitted authenticationtoken [org.apache.shiro.authc.usernamepasswordtoken - 张三, rememberme=false].
出现这个问题是我的是因为realm中的某个实现类没有加注解,我这儿演示时是应为shirouserrealm为加@service注解
2.
org.apache.shiro.authc.authenticationexception: authentication token of type [class org.apache.shiro.authc.usernamepasswordtoken] could not be authenticated by any configured realms. please ensure that at least one realm can authenticate these tokens.
这儿出现的问题是应为我的shiouserrealm的authenticationinfo方法的user user = userdao.finduserbyusername(username);这行代码出现的问题,debug的时候就发现这一句执行后就保错
原因:是因为我的application.yml文件中没有写dao对应的mapper文件的路径
3. 在shiouserphonerealm的dogetauthenticationinfo方法的new simpleauthenticationinfo(user,phone,getname())这个位置后就报错是应为shiouserphonerealm的这个方法中你没有将new的对象设置为allowallcredentialsmatcher();
@override public void setcredentialsmatcher(credentialsmatcher credentialsmatcher) { //这儿的credentialsmatcher的new的对象必须是allowallcredentialsmatcher credentialsmatcher matcher = new allowallcredentialsmatcher(); super.setcredentialsmatcher(matcher); }
注解中有一些需要注意的地方,建议看看,注解不对的地方还希望在下放评论指出或者联系我
应为我的知识有限,此方法本人实现目前没有问题,其中有什么不对的地方还希望各位指出,谢谢!
使用的是jdk8,spring boot 的2.2.1版本,shiro的1,.4.1版本
到此这篇关于springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)的文章就介绍到这了,更多相关springboot整合shiro验证登录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!