1. 程式人生 > >【Shiro】(一)Shiro初瞭解

【Shiro】(一)Shiro初瞭解

在ITOO裡面登入用到了Shrio,一直沒有好好的理解和學習,下面我們就來看一下Shiro到底是什麼東西。

(一)Shiro介紹

     shiro是apache的一個開源框架,是一個許可權管理的框架,實現使用者認證、使用者授權。沒有使用Shiro之前,我們的使用者登陸和授權,都是自己敲程式碼寫後臺實現,Shiro框架就封裝對使用者認證和授權進行了封裝,減少了我們的程式碼編寫。使用shiro實現系統 的許可權管理,有效提高開發效率,從而降低開發成本。

     spring中有spring security (原名Acegi),是一個許可權框架,它和spring依賴過於緊密,沒有shiro使用簡單。
shiro不依賴於spring,shiro不僅可以實現 web應用的許可權管理,還可以實現c/s系統,分散式系統許可權管理,shiro屬於輕量框架,越來越多企業專案開始使用shiro。

(二)Shiro架構




subject:主體,可以是使用者也可以是程式,主體要訪問系統,系統需要對主體進行認證、授權。

securityManager:安全管理器,主體進行認證和授權都 是通過securityManager進行。

authenticator:認證器,主體進行認證最終通過authenticator進行的。

authorizer:授權器,主體進行授權最終通過authorizer進行的。

sessionManager:web應用中一般是用web容器對session進行管理,shiro也提供一套session管理的方式。

sessionDao:  通過SessionDao管理session資料,針對個性化的session資料儲存需要使用sessionDao。

cache Manager:快取管理器,主要對session和授權資料進行快取,比如將授權資料通過cacheManager進行快取管理,和ehcache整合對快取資料進行管理。

realm:域,領域,相當於資料來源,通過realm存取認證、授權相關資料。

cryptography:密碼管理,提供了一套加密/解密的元件,方便開發。比如提供常用的雜湊、加/解密等功能。

(三)Shiro開發

1.shiro jar包。

	<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-core</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-web</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-ehcache</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-quartz</artifactId>
			<version>1.2.3</version>
		</dependency>

也可以通過引入shiro-all包括shiro所有的包:
	<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-all</artifactId>
			<version>1.2.3</version>
		</dependency>
2.使用shiro實現使用者認證
@Test
	public void testLoginAndLogout() {

		// 建立securityManager工廠,通過ini配置檔案建立securityManager工廠
		Factory<SecurityManager> factory = new IniSecurityManagerFactory(
				"classpath:shiro-first.ini");
		
		//建立SecurityManager
		SecurityManager securityManager = factory.getInstance();
		
		//將securityManager設定當前的執行環境中
		SecurityUtils.setSecurityManager(securityManager);
		
		//從SecurityUtils裡邊建立一個subject
		Subject subject = SecurityUtils.getSubject();
		
		//在認證提交前準備token(令牌)
		UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");

		try {
			//執行認證提交
			subject.login(token);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//是否認證通過
		boolean isAuthenticated =  subject.isAuthenticated();
		
		System.out.println("是否認證通過:" + isAuthenticated);
		
		//退出操作
		subject.logout();
		
		//是否認證通過
		isAuthenticated =  subject.isAuthenticated();
		
		System.out.println("是否認證通過:" + isAuthenticated);
		
		

	}

執行流程:

1、通過ini配置檔案建立securityManager
2、呼叫subject.login方法主體提交認證,提交的token
3、securityManager進行認證,securityManager最終由ModularRealmAuthenticator進行認證。
4、ModularRealmAuthenticator呼叫IniRealm(給realm傳入token) 去ini配置檔案中查詢使用者資訊
5、IniRealm根據輸入的token(UsernamePasswordToken)從 shiro-first.ini查詢使用者資訊,根據賬號查詢使用者資訊(賬號和密碼)
如果查詢到使用者資訊,就給ModularRealmAuthenticator返回使用者資訊(賬號和密碼)
如果查詢不到,就給ModularRealmAuthenticator返回null
6、ModularRealmAuthenticator接收IniRealm返回Authentication認證資訊
如果返回的認證資訊是null,ModularRealmAuthenticator丟擲異常(org.apache.shiro.authc.UnknownAccountException)
如果返回的認證資訊不是null(說明inirealm找到了使用者),對IniRealm返回使用者密碼 (在ini檔案中存在)和 token中的密碼 進行對比,如果不一致丟擲異常(org.apache.shiro.authc.IncorrectCredentialsException)

shiro授權的流程和認證十分相似,在以後我們再來介紹。

小結:shiro框架其實使用起來很簡單,無非就是自定義Real實現我們自己的認證和授權邏輯。