============================
============================
今天這個操作其實很簡單,只是結(jié)合之前的第8篇和第10篇文章
1.png
自己寫了一個簡單的方法,根據(jù)name去查詢數(shù)據(jù)庫中的數(shù)據(jù),返回一個User實體類蜀漆。
主要是在Realm中使用這個方法流码。
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("執(zhí)行認(rèn)證邏輯");
//編寫shiro邏輯判斷,判斷用戶名和密碼
//1.判斷用戶名
UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
User user=userService.findByName(token.getUsername());
if (user==null){
//用戶名不存在
return null;//shiro底層會拋出一個UnknownAccountException
}
//判斷密碼
return new SimpleAuthenticationInfo("",user.getPassword(),"");
}
UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
將傳來的Token轉(zhuǎn)成 UsernamePasswordToken
于是用Token中的name去查數(shù)據(jù)庫,返回的user中的密碼去用SimpleAuthenticationInfo
校驗 就是這么簡單。
關(guān)注我自建博客站
http://blog.petchk.cn/
關(guān)注我git博客https://chenhuakai.github.io/