shiro登录认证过程讲解(转)
先粘出登錄的代碼
@RequestMapping(value="/submitLogin",method = RequestMethod.POST)@ResponseBodypublic Map<String, Object> submitLogin(String username,String password){System.out.println(username+"-----"+password);String msg = "";Map<String, Object> map = new HashMap<>();try {UsernamePasswordToken token = token = new UsernamePasswordToken(username, password);char[] password1 = token.getPassword();String s = password1.toString();System.out.println("*******"+s);SecurityUtils.getSubject().login(token);token.setRememberMe(true);map.put("status",200);} catch (UnknownAccountException e) {msg = "UnknownAccountException -- > 賬號不存在:";map.put("status",400);} catch (IncorrectCredentialsException e){msg = "IncorrectCredentialsException -- > 密碼不正確:";map.put("status",500);}catch (Exception exception){msg = "else >> "+exception;System.out.println("else -- >" + exception);}return map;}可以看到已經(jīng)獲取到了username和password ,為了接下來的認(rèn)證過程,我們需要獲取subject對象,也就是代表當(dāng)前登錄用戶,并且要將username和password兩個變量設(shè)置到UsernamePasswordToken對象的token中, 調(diào)用SecurityUtils.getSubject().login(token)方法,將 token傳入
接下來看看login方法的實現(xiàn):
主要還是用到了securityManager安全管理器
進入securityManager里邊的login方法,看看他的實現(xiàn):
?在這個方法中定義了AuthenticationInfo對象來接受從Realm傳來的認(rèn)證信息
進入authenticate方法中
public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {return this.authenticator.authenticate(token);}發(fā)現(xiàn)調(diào)用了authenticator的authenticate這個方法
進入this.authenticator.authenticate(token)這個方法中
在這個 類中調(diào)用了這個方法,再進去看他的實現(xiàn)
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {this.assertRealmsConfigured();Collection<Realm> realms = this.getRealms();return realms.size() == 1?this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken):this.doMultiRealmAuthentication(realms, authenticationToken);}在這里才是剛才前邊的那個authenticator的實現(xiàn),?this.assertRealmsConfigured() 這個方法是判斷realm是否存在,不存在則拋出異常,他會根據(jù)realm的個數(shù)來判斷執(zhí)行哪個方法,上篇中springboot整合shiro我只配置了一個realm,所以他只會執(zhí)行this.doSingleRealmAuthentication((Realm)realms.iterator().next(),?authenticationToken)這個方法,并且會將 realm和token作為參數(shù)傳入,這里的realm其實就是自己定義的MyShiroRealm
接下來再進入doSingleRealmAuthentication這個方法中,
在這里 他會先判斷realm是否支持token
接下來執(zhí)行else中的getAuthenticationInfo方法
this.getCachedAuthenticationInfo(token)這個方法是從shiro緩存中讀取用戶信息,如果沒有,才從realm中獲取信息。如果是第一次登陸,緩存中肯定沒有認(rèn)證信息,所以會執(zhí)行this.doGetAuthenticationInfo(token)這個方法。
查看this.doGetAuthenticationInfo(token)方法,會發(fā)現(xiàn)有這么幾個類提供我們選擇
其中就有我們自定義的realm,進去
?所以 ,上邊的doGetAuthorizationInfo是 執(zhí)行的我們自定義realm中重寫的doGetAuthorizationInfo這個方法。這個方法就會從數(shù)據(jù)庫中讀取我們所需要的信息,最后封裝成SimpleAuthorizationInfo返回去。
現(xiàn)在獲取到認(rèn)證信息了,接下來就是shiro怎么去進行認(rèn)證,我們返回去看
?獲取 完信息之后就是進行密碼匹配,進入assertCredentialsMatch方法中看一下,
首先 獲取一個CredentialsMatcher對象,譯為憑證匹配器,這個類的主要作用就是將用戶輸入的密碼一某種計算加密。
再看一下cm.doCredentialsMatch(token,info)
這里會用到equals方法對token中加密的密碼和從數(shù)據(jù)庫中取出來的info中的密碼進行對比,如果認(rèn)證相同就返回true,失敗就返回false,并拋出AuthenticationException,將info返回到defaultSecurityManager中,到此認(rèn)證過程結(jié)束。
總結(jié)
以上是生活随笔為你收集整理的shiro登录认证过程讲解(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java面试宝典系列之基础面试题-常见的
- 下一篇: MyBatis源码分析——MyBatis