最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java的Shiro框架認證流程詳解

 更新時間:2024年01月04日 08:41:10   作者:Splaying  
這篇文章主要介紹了Java的Shiro框架認證流程詳解,Shiro 是一個功能強大和易于使用的安全框架,為開發(fā)人員提供一個直觀而全面的解決方案的認證,授權(quán),加密,會話管理四大功能,需要的朋友可以參考下

1、Shiro框架介紹

在這里插入圖片描述

  • Shiro 是一個功能強大和易于使用的安全框架,為開發(fā)人員提供一個直觀而全面的解決方案的認證,授權(quán),加密,會話管理四大功能!
  • Subject:主體,subject記錄了當前操作用戶,將當前登錄訪問的用戶信息存在其中。
  • Authenticator:身份認證/登錄,驗證用戶賬號密碼是否正確。
  • Authorizer:授權(quán),根據(jù)不同的用戶發(fā)放不同的權(quán)限。
  • SessionManager:會話管理,它不依賴web容器的session,因此Shiro也可以使用在非web應(yīng)用上獨立使用
  • Realm:領(lǐng)域范圍,securityManager進行安全認證需要通過Realm獲取數(shù)據(jù),然后與Subject中的數(shù)據(jù)進行認證授權(quán)!
  • Cryptography:加密功能,將隱私數(shù)據(jù)進行加密。
  • SessionDAO:會話dao,是對session會話操作的一套接口,可以通過jdbc將會話數(shù)據(jù)存儲到數(shù)據(jù)庫。
  • CacheManager:緩存管理,Shiro提供的緩存機制;為了提高效率。

2、入門案例

2.1、項目依賴

<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.7.1</version>
</dependency>

2.2、模擬數(shù)據(jù)

  • resource/shiro.ini文件
  • 一定是ini配置文件,并且使用鍵值對的形式
[users]
zhangsan=123
admin=123456
splay=123456

2.3、案例

  • 可以看到Shiro最核心的東西是SecurityManager安全管理器
  • 然后初始化ini中的數(shù)據(jù)給Realm、Realm的數(shù)據(jù)正常是從數(shù)據(jù)庫中獲取的。
  • 將SecurityManager安全管理器注入到SecurityUtil全局工具類中
  • 最后創(chuàng)建令牌模擬用戶登錄進行校驗!
  • 當用戶名不正確時拋出UnknownAccountException異常、密碼不對時拋出IncorrectCredentialsException異常!
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.text.IniRealm;
import org.apache.shiro.subject.Subject;
public class Application {
    public static void main(String[] args) {
        // 1. 創(chuàng)建安全管理器對象
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        // 2. 給安全管理器對象設(shè)置realm(模擬數(shù)據(jù)庫中的數(shù)據(jù))
        securityManager.setRealm(new IniRealm("classpath:shiro.ini"));
        // 3. SecurityUtil設(shè)置全局的安全管理器
        SecurityUtils.setSecurityManager(securityManager);
        // 4. 關(guān)鍵對象Subject主體對象
        Subject subject = SecurityUtils.getSubject();
        // 5. 模擬登錄(用戶名、密碼)
        UsernamePasswordToken user = new UsernamePasswordToken("splay", "123456");
        try{
            System.out.println("認證狀態(tài): " + subject.isAuthenticated());
            //登錄校驗
            subject.login(user);
            System.out.println("認證狀態(tài): " + subject.isAuthenticated());
        } catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("認證失敗: 用戶名密碼錯誤!");
        } catch (UnknownAccountException e){
            e.printStackTrace();
            System.out.println("認證失敗: 用戶名錯誤!");
        }
    }
}

3、源碼解析

  • Shiro對整個登錄用戶的認證分為兩個步驟:先校驗用戶名、其次校驗密碼;
  • 主要涉及三個類:AuthenticatingRealm、AuthorizingRealm、SimpleAccountRealm

3.1、用戶名校驗

  • 這里只校驗賬戶名是否被鎖定、證書是否過期這兩項。
  • 并且這里的account返回為空是可以的。
public class SimpleAccountRealm extends AuthorizingRealm {
	protected SimpleAccount getUser(String username) {
	    USERS_LOCK.readLock().lock();				//ReentrantReadWriteLock可重入讀寫鎖
	    try {
	        return this.users.get(username);
	    } finally {
	        USERS_LOCK.readLock().unlock();
	    }
	}
	
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)  {
	    UsernamePasswordToken upToken = (UsernamePasswordToken) token;
	    SimpleAccount account = getUser(upToken.getUsername());
		
	    if (account != null) {				
	        if (account.isLocked()) {		//賬戶被鎖定
	            throw new LockedAccountException("Account [" + account + "] is locked.");
	        }
	        if (account.isCredentialsExpired()) {		//憑證過期
	            String msg = "The credentials for account [" + account + "] are expired";
	            throw new ExpiredCredentialsException(msg);
	        }
	    }
	    return account;
	}
}

3.2、密碼校驗

當用戶校驗返回值不為空時說明賬戶存在、沒有被鎖定、證書沒有過期時進行密碼校驗。

public abstract class AuthenticatingRealm extends CachingRealm implements Initializable {

	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;
    }
    protected void assertCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {
        CredentialsMatcher cm = getCredentialsMatcher();
        if (cm != null) {
            if (!cm.doCredentialsMatch(token, info)) {
                // 密碼錯誤異常
                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.");
        }
    }
}

3.3、賬戶不存在異常

這里調(diào)用3.2中的方法、3.2中的調(diào)用3.1;實則Shiro封裝了很多層這里只是最重要的代碼。

public class ModularRealmAuthenticator extends AbstractAuthenticator {
	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;
    }
}

到此這篇關(guān)于Java的Shiro框架認證流程詳解的文章就介紹到這了,更多相關(guān)Shiro框架認證流程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring中的注入list集合

    spring中的注入list集合

    這篇文章主要介紹了spring中的注入list集合問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java之Jackson使用案例詳解

    Java之Jackson使用案例詳解

    這篇文章主要介紹了Java之Jackson使用案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Java之jpa入門教程講解

    Java之jpa入門教程講解

    這篇文章主要介紹了Java之jpa入門教程講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • JSR-107緩存規(guī)范介紹

    JSR-107緩存規(guī)范介紹

    JSR是Java Specification Requests的縮寫,意思是Java規(guī)范提案,下面給大家介紹JSR-107緩存規(guī)范的相關(guān)知識,感興趣的朋友一起看看吧
    2025-05-05
  • spring boot接收請求常用注解示例詳解

    spring boot接收請求常用注解示例詳解

    這篇文章介紹了Spring Boot中常用的接收請求的注解,包括`@RequestBody`、`@PathVariable`、`@RequestParam`和`@DateTimeFormat`,并提供了每個注解的示例,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • Spring Cloud 中@FeignClient注解中的contextId屬性詳解

    Spring Cloud 中@FeignClient注解中的contextId屬性詳解

    這篇文章主要介紹了Spring Cloud 中@FeignClient注解中的contextId屬性詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java生成CSV文件實例詳解

    Java生成CSV文件實例詳解

    這篇文章主要介紹了Java生成CSV文件的方法,很實用的功能,需要的朋友可以參考下
    2014-07-07
  • Java日期操作方法工具類實例【包含日期比較大小,相加減,判斷,驗證,獲取年份等】

    Java日期操作方法工具類實例【包含日期比較大小,相加減,判斷,驗證,獲取年份等】

    這篇文章主要介紹了Java日期操作方法工具類,結(jié)合完整實例形式分析了java針對日期的各種常見操作,包括日期比較大小,相加減,判斷,驗證,獲取年份、天數(shù)、星期等,需要的朋友可以參考下
    2017-11-11
  • SpringBoot中@RestControllerAdvice 全局異常處理的實現(xiàn)

    SpringBoot中@RestControllerAdvice 全局異常處理的實現(xiàn)

    本文主要介紹了SpringBoot中@RestControllerAdvice 全局異常處理的實現(xiàn),通過定義統(tǒng)一響應(yīng)格式、自定義異常類及測試驗證,確保接口異常時返回指定格式的提示信息,提升錯誤處理一致性
    2025-06-06
  • IDEA包下不能建包問題的解決過程

    IDEA包下不能建包問題的解決過程

    在IDEA中,新建包時出現(xiàn)問題,是因為勾選了“Compact Middle Packages”選項,導致包結(jié)構(gòu) becoming緊湊型,取消該選項后,包結(jié)構(gòu)恢復(fù)正常
    2026-03-03

最新評論

沂源县| 宣威市| 乌恰县| 会泽县| 裕民县| 宁乡县| 建德市| 邹平县| 磐安县| 延庆县| 南通市| 德格县| 宁德市| 济宁市| 台湾省| 察雅县| 汽车| 南昌市| 双鸭山市| 甘肃省| 且末县| 克什克腾旗| 长乐市| 贡嘎县| 绥德县| 育儿| 高尔夫| 龙泉市| 宁阳县| 绥阳县| 博兴县| 景德镇市| 饶河县| 新竹市| 陇西县| 宣汉县| 田东县| 洪江市| 金平| 连州市| 南汇区|