Shiro入門5:Shiro認證的HelloWorld

這是一個基於原生實現的一個Shiro認證的HelloWorld的Test,不與其他Spring等框架整合,但是需要用到Junit,或者可以將下面的測試代碼用static void main來運行也是可以的。

這篇文章爲了讓大家更加快速熟悉Shiro的使用流程,在最後我會給大家發一下我自己的總結。



主要需要構建一個ini配置文件來進行Shiro的使用,配置文件放在src/main/resources中

//==============================================================
// Shiro認證部分
//==============================================================
	|---認證流程
		|---構造Security Manager環境
		|---Subject.login()提交認證
		|---SecurityManager.login()執行認證
		|---Authenticator執行認證
		|---Realm根據身份獲取驗證信息
	|
	|---工程結構
		|---shiro.ini:通過此配置文件創建SecurityManager工廠
		|---對ini進行配置
			#對用戶信息進行配置
			[users]

			#用戶帳號和密碼
			zhangsan=11111
			lisi=22222
	|


此文老貓原創,轉載請加本文連接:http://blog.csdn.net/nthack5730/article/details/50965025
更多有關老貓的文章:http://blog.csdn.net/nthack5730


	|---入門程序:
	|
		public class AuthenticationTest {
			//用戶登陸和退出
			@Test
			public void testLoginAndLogout(){
				//創建securityManager工廠,通過Ini配置文件創建securityManager工廠
				Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini");
				
				//創建SecurityManager
				SecurityManager sm = factory.getInstance();
				
				//將securityManager設置到當前的環境中
				SecurityUtils.setSecurityManager(sm);
				
				//從SecurityUtils裏面創建一個subject
				Subject subject = SecurityUtils.getSubject();
				
				//在認證提交前,需要準備token(令牌)
				UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "11111");
				
				try {
					//執行認證提交
					subject.login(token);
				} catch (AuthenticationException e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				
				//是否認證通過
				boolean isAuthenticated = subject.isAuthenticated();
				
				System.out.println("是否認證通過:" + isAuthenticated);
				
				
				//退出操作
				subject.logout();
				
				//是否認證通過
				isAuthenticated = subject.isAuthenticated();
				
				System.out.println("是否認證通過:" + isAuthenticated);
				
				
			}//method
		}//class

此文老貓原創,轉載請加本文連接:http://blog.csdn.net/nthack5730/article/details/50965025

更多有關老貓的文章:http://blog.csdn.net/nthack5730




方便大家複製:【整體流程總結精華在最後】

shiro-first.ini

#對用戶信息進行配置
[users]
#用戶帳號和密碼
zhangsan=11111
lisi=22222


AuthenticationTest.java

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
public class AuthenticationTest {
	//用戶登陸和退出
	@Test
	public void testLoginAndLogout(){
		//創建securityManager工廠,通過Ini配置文件創建securityManager工廠
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-first.ini");
		
		//創建SecurityManager
		SecurityManager sm = factory.getInstance();
		
		//將securityManager設置到當前的環境中
		SecurityUtils.setSecurityManager(sm);
		
		//從SecurityUtils裏面創建一個subject
		Subject subject = SecurityUtils.getSubject();
		
		//在認證提交前,需要準備token(令牌)
		UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "11111");
		
		try {
			//執行認證提交
			subject.login(token);
		} catch (AuthenticationException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//是否認證通過
		boolean isAuthenticated = subject.isAuthenticated();
		
		System.out.println("是否認證通過:" + isAuthenticated);
		
		
		//退出操作
		subject.logout();
		
		//是否認證通過
		isAuthenticated = subject.isAuthenticated();
		
		System.out.println("是否認證通過:" + isAuthenticated);
		
		
	}//method
}//class

此文老貓原創,轉載請加本文連接:http://blog.csdn.net/nthack5730/article/details/50965025

更多有關老貓的文章:http://blog.csdn.net/nthack5730




流程總結:

	|---執行流程:
		|---通過ini配置文件創建SecurityManager
		|---調用subject.login方法提交認證,提交的就是token 
			|---內部通過SecurityManager進行認證
			|
			|---SecurityManager通過調用Authenticator的實現類最終由ModularRealmAuthenticator進行認證
			|
			|---ModularRealmAuthenticator調用IniRealm【給Realm傳入token】去ini配置文件中查詢用戶信息
			|
			|---IniRealm根據輸入的token(UsernamePasswordToken)從Shiro-first.ini查詢用戶信息,根據帳號查詢用戶信息(帳號和密碼)
				|---如果IniRealm查詢到賬號:就給ModularRealmAuthenticator返回用戶信息【包括帳號和密碼】
				|---如果IniRealm帳號找不到,就給ModularRealmAuthenticator返回null
			|
			|---ModularRealmAuthenticator接受IniRealm返回的Authentication認證信息
				|---如果接受信息是null,說明帳號不存在,
					ModularRealmAuthenticator拋出異常【最終幹活的是Realm】
				|
				|---如果接受認證信息不是null【說明IniRealm找到了用戶】,
					ModularRealmAuthenticator對IniRealm返回用戶密碼(在ini文件中存在)和token中的密碼進行比對
	|				|---如果不一致,拋出異常
	|---小結
		|---ModularRealmAuthenticator作用進行認證,需要調用realm查詢用戶信息(在數據庫中存在用戶信息)
		|---ModularRealmAuthenticator進行密碼對比(認證過程)。
		|
		|---realm:需要根據token中的用戶信息去查詢數據庫,如果查到用戶返回認證信息,如果查不到就返回null

此文老貓原創,轉載請加本文連接:http://blog.csdn.net/nthack5730/article/details/50965025

更多有關老貓的文章:http://blog.csdn.net/nthack5730



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章