欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解shrio的認(rèn)證(登錄)過程

 更新時(shí)間:2021年02月01日 15:46:55   作者:超人不會(huì)飛  
這篇文章主要介紹了shrio的認(rèn)證(登錄)過程,幫助大家更好的理解和使用shrio框架,感興趣的朋友可以了解下

shrio是一個(gè)比較輕量級(jí)的安全框架,主要的作用是在后端承擔(dān)認(rèn)證和授權(quán)的工作。今天就講一下shrio進(jìn)行認(rèn)證的一個(gè)過程。
首先先介紹一下在認(rèn)證過程中的幾個(gè)關(guān)鍵的對(duì)象:

  • Subject:主體

訪問系統(tǒng)的用戶,主體可以是用戶、程序等,進(jìn)行認(rèn)證的都稱為主體;

  • Principal:身份信息

是主體(subject)進(jìn)行身份認(rèn)證的標(biāo)識(shí),標(biāo)識(shí)必須具有唯一性,如用戶名、手機(jī)號(hào)、郵箱地址等,一個(gè)主體可以有多個(gè)身份,但是必須有一個(gè)主身份(Primary Principal)。

  • credential:憑證信息

是只有主體自己知道的安全信息,如密碼、證書等。
接著我們就進(jìn)入認(rèn)證的具體過程:
首先是從前端的登錄表單中接收到用戶輸入的token(username + password):

@RequestMapping("/login")
public String login(@RequestBody Map user){
  Subject subject = SecurityUtils.getSubject();
  UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(user.get("email").toString(), user.get("password").toString());
   try {
      subject.login(usernamePasswordToken);
   } catch (UnknownAccountException e) {
      return "郵箱不存在!";
   } catch (AuthenticationException e) {
      return "賬號(hào)或密碼錯(cuò)誤!";
   }
    return "登錄成功!";
  }

這里的usernamePasswordToken(以下簡(jiǎn)稱token)就是用戶名和密碼的一個(gè)結(jié)合對(duì)象,然后調(diào)用subject的login方法將token傳入開始認(rèn)證過程。
接著會(huì)發(fā)現(xiàn)subject的login方法調(diào)用的其實(shí)是securityManager的login方法:

Subject subject = securityManager.login(this, token);

再往下看securityManager的login方法內(nèi)部:

public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {
  AuthenticationInfo info;
   try {
      info = authenticate(token);
   } catch (AuthenticationException ae) {
      try {
        onFailedLogin(token, ae, subject);
   } catch (Exception e) {
        if (log.isInfoEnabled()) {
          log.info("onFailedLogin method threw an " +
              "exception. Logging and propagating original AuthenticationException.", e);
   }
      }
      throw ae; //propagate
   }
    Subject loggedIn = createSubject(token, info, subject);
   onSuccessfulLogin(token, info, loggedIn);
   return loggedIn;
}

上面代碼的關(guān)鍵在于:

info = authenticate(token);

即將token傳入authenticate方法中得到一個(gè)AuthenticationInfo類型的認(rèn)證信息。
以下是authenticate方法的具體內(nèi)容:

public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
  if (token == null) {
    throw new IllegalArgumentException("Method argument (authentication token) cannot be null.");
  }
  log.trace("Authentication attempt received for token [{}]", token);
  AuthenticationInfo info;
  try {
    info = doAuthenticate(token);
  if (info == null) {
      String msg = "No account information found for authentication token [" + token + "] by this " +
          "Authenticator instance. Please check that it is configured correctly.";
  throw new AuthenticationException(msg);
  }
  } catch (Throwable t) {
    AuthenticationException ae = null;
  if (t instanceof AuthenticationException) {
      ae = (AuthenticationException) t;
  }
    if (ae == null) {
      //Exception thrown was not an expected AuthenticationException. Therefore it is probably a little more
  //severe or unexpected. So, wrap in an AuthenticationException, log to warn, and propagate: String msg = "Authentication failed for token submission [" + token + "]. Possible unexpected " +
          "error? (Typical or expected login exceptions should extend from AuthenticationException).";
  ae = new AuthenticationException(msg, t);
  if (log.isWarnEnabled())
        log.warn(msg, t);
  }
    try {
      notifyFailure(token, ae);
  } catch (Throwable t2) {
      if (log.isWarnEnabled()) {
        String msg = "Unable to send notification for failed authentication attempt - listener error?. " +
            "Please check your AuthenticationListener implementation(s). Logging sending exception " +
            "and propagating original AuthenticationException instead...";
  log.warn(msg, t2);
  }
    }
    throw ae;
  }
  log.debug("Authentication successful for token [{}]. Returned account [{}]", token, info);
  notifySuccess(token, info);
  return info;
}

首先就是判斷token是否為空,不為空再將token傳入doAuthenticate方法中:

protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
  assertRealmsConfigured();
  Collection<Realm> realms = getRealms();
  if (realms.size() == 1) {
    return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
  } else {
    return doMultiRealmAuthentication(realms, authenticationToken);
  }
}

這一步是判斷是有單個(gè)Reaml驗(yàn)證還是多個(gè)Reaml驗(yàn)證,單個(gè)就執(zhí)行doSingleRealmAuthentication()方法,多個(gè)就執(zhí)行doMultiRealmAuthentication()方法。
一般情況下是單個(gè)驗(yàn)證:

protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {
  if (!realm.supports(token)) {
    String msg = "Realm [" + realm + "] does not support authentication token [" +
        token + "]. Please ensure that the appropriate Realm implementation is " +
        "configured correctly or that the realm accepts AuthenticationTokens of this type.";
    throw new UnsupportedTokenException(msg);
  }
  AuthenticationInfo info = realm.getAuthenticationInfo(token);
  if (info == null) {
    String msg = "Realm [" + realm + "] was unable to find account data for the " +
        "submitted AuthenticationToken [" + token + "].";
    throw new UnknownAccountException(msg);
  }
  return info;
}

這一步中首先判斷是否支持Realm,只有支持Realm才調(diào)用realm.getAuthenticationInfo(token)獲取info。

public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
  AuthenticationInfo info = getCachedAuthenticationInfo(token);
  if (info == null) {
    //otherwise not cached, perform the lookup:
    info = doGetAuthenticationInfo(token);
    log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);
    if (token != null && info != null) {
      cacheAuthenticationInfoIfPossible(token, info);
    }
  } else {
    log.debug("Using cached authentication info [{}] to perform credentials matching.", info);
  }
  if (info != null) {
    assertCredentialsMatch(token, info);
  } else {
    log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}]. Returning null.", token);
  }
  return info;
}

首先查看Cache中是否有該token的info,如果有,則直接從Cache中去即可。如果是第一次登錄,則Cache中不會(huì)有該token的info,需要調(diào)用doGetAuthenticationInfo(token)方法獲取,并將結(jié)果加入到Cache中,方便下次使用。而這里調(diào)用的doGetAuthenticationInfo()方法就是我們?cè)谧约褐貙懙姆椒?,具體的內(nèi)容是自定義了對(duì)拿到的這個(gè)token的一個(gè)處理的過程:

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
  if (authenticationToken.getPrincipal() == null)
    return null;
  String email = authenticationToken.getPrincipal().toString();
  User user = userService.findByEmail(email);
  if (user == null)
    return null;
  else return new SimpleAuthenticationInfo(email, user.getPassword(), getName());
}

這其中進(jìn)行了幾步判斷:首先是判斷傳入的用戶名是否為空,在判斷傳入的用戶名在本地的數(shù)據(jù)庫(kù)中是否存在,不存在則返回一個(gè)用戶名不存在的Exception。以上兩部通過之后生成一個(gè)包括傳入用戶名和密碼的info,注意此時(shí)關(guān)于用戶名的驗(yàn)證已經(jīng)完成,接下來進(jìn)入對(duì)密碼的驗(yàn)證。
將這一步得到的info返回給getAuthenticationInfo方法中的

assertCredentialsMatch(token, info);

此時(shí)的info是正確的用戶名和密碼的信息,token是輸入的用戶名和密碼的信息,經(jīng)過前面步驟的驗(yàn)證過程,用戶名此時(shí)已經(jīng)是真是存在的了,這一步就是驗(yàn)證輸入的用戶名和密碼的對(duì)應(yīng)關(guān)系是否正確。

protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
  CredentialsMatcher cm = getCredentialsMatcher();
  if (cm != null) {
    if (!cm.doCredentialsMatch(token, info)) {
      //not successful - throw an exception to indicate this:
      String msg = "Submitted credentials for token [" + token + "] did not match the expected credentials.";
      throw new IncorrectCredentialsException(msg);
    }
  } 
  else {
    throw new AuthenticationException("A CredentialsMatcher must be configured in order to verify " +
        "credentials during authentication. If you do not wish for credentials to be examined, you " +
        "can configure an " + AllowAllCredentialsMatcher.class.getName() + " instance.");
  }
}

上面步驟就是驗(yàn)證token中的密碼的和info中的密碼是否對(duì)應(yīng)的代碼。這一步驗(yàn)證完成之后,整個(gè)shrio認(rèn)證的過程就結(jié)束了。

以上就是詳解shrio的認(rèn)證(登錄)過程的詳細(xì)內(nèi)容,更多關(guān)于shrio的認(rèn)證(登錄)過程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 解決Springboot @Autowired 無法注入問題

    解決Springboot @Autowired 無法注入問題

    WebappApplication 一定要在包的最外層,否則Spring無法對(duì)所有的類進(jìn)行托管,會(huì)造成@Autowired 無法注入。接下來給大家介紹解決Springboot @Autowired 無法注入問題,感興趣的朋友一起看看吧
    2018-08-08
  • springboot+thymeleaf整合阿里云OOS對(duì)象存儲(chǔ)圖片的實(shí)現(xiàn)

    springboot+thymeleaf整合阿里云OOS對(duì)象存儲(chǔ)圖片的實(shí)現(xiàn)

    本文主要介紹了springboot+thymeleaf整合阿里云OOS對(duì)象存儲(chǔ)圖片的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Mybatis調(diào)用存儲(chǔ)過程的案例

    Mybatis調(diào)用存儲(chǔ)過程的案例

    這篇文章主要介紹了Mybatis如何調(diào)用存儲(chǔ)過程,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Java 多線程之兩步掌握

    Java 多線程之兩步掌握

    Java 多線程編程 Java給多線程編程提供了內(nèi)置的支持。一條線程指的是進(jìn)程中一個(gè)單一順序的控制流,一個(gè)進(jìn)程中可以并發(fā)多個(gè)線程,每條線程并行執(zhí)行不同的任務(wù)
    2021-10-10
  • 淺談Java面向?qū)ο笾畠?nèi)部類

    淺談Java面向?qū)ο笾畠?nèi)部類

    內(nèi)部類是一個(gè)非常有用的特性但又比較難理解使用的特性,我們從外面看是非常容易理解的,無非就是在一個(gè)類的內(nèi)部在定義一個(gè)類。其實(shí)使用內(nèi)部類最大的優(yōu)點(diǎn)就在于它能夠非常好的解決多重繼承的問題
    2021-06-06
  • Springboot2.3.x整合Canal的示例代碼

    Springboot2.3.x整合Canal的示例代碼

    canal是阿里開源mysql?binlog?數(shù)據(jù)組件,canal-server?才是canal的核心我們前邊所講的canal的功能,實(shí)際上講述的就是canal-server的功能,本文給大家介紹Springboot2.3.x整合Canal的示例代碼,需要的朋友可以參考下
    2022-02-02
  • Flowable執(zhí)行完畢的流程查找方法

    Flowable執(zhí)行完畢的流程查找方法

    這篇文章主要為大家介紹了Flowable執(zhí)行完畢的流程查找方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例

    Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例

    這篇文章主要介紹了Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例,迭代器模式能夠?qū)υL問者隱藏對(duì)象的內(nèi)部細(xì)節(jié),需要的朋友可以參考下
    2016-05-05
  • IntelliJ?IDEA無公網(wǎng)遠(yuǎn)程Linux服務(wù)器環(huán)境開發(fā)過程(推薦收藏)

    IntelliJ?IDEA無公網(wǎng)遠(yuǎn)程Linux服務(wù)器環(huán)境開發(fā)過程(推薦收藏)

    下面介紹如何在IDEA中設(shè)置遠(yuǎn)程連接服務(wù)器開發(fā)環(huán)境并結(jié)合Cpolar內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)無公網(wǎng)遠(yuǎn)程連接,然后實(shí)現(xiàn)遠(yuǎn)程Linux環(huán)境進(jìn)行開發(fā),感興趣的朋友跟隨小編一起看看吧
    2023-12-12
  • IDEA 2020 本土化,真的是全中文了(真香)

    IDEA 2020 本土化,真的是全中文了(真香)

    去年,JetBrains 網(wǎng)站進(jìn)行了本地化,提供了 8 種不同的語(yǔ)言版本,而現(xiàn)在,團(tuán)隊(duì)正在對(duì)基于 IntelliJ 的 IDE 進(jìn)行本地化
    2020-12-12

最新評(píng)論