javascript
Springbooot集成Shiro简单使用
文章目錄
- 1.Shiro架構(gòu)
- 2.Shiro環(huán)境搭建
- 3.Shiro用戶登錄攔截與認(rèn)證
1.Shiro架構(gòu)
Shiro三個(gè)主要的概念:
Shiro外部架構(gòu)
- Subject:主體,當(dāng)前參與應(yīng)用安全部分的主體。可以是用戶,可以是第三方服務(wù),可以是cron 任務(wù),或者任何東西。主要指一個(gè)正在與當(dāng)前軟件交互的東西。所有Subject都需要SecurityManager,當(dāng)與Subject進(jìn)行交互,這些交互行為實(shí)際上被轉(zhuǎn)換為與SecurityManager的交互
- SecurityManager:安全管理員,Shiro架構(gòu)的核心,它就像Shiro內(nèi)部所有原件的保護(hù)傘。然而一旦配置了SecurityManager,SecurityManager就用到的比較少,開發(fā)者大部分時(shí)間都花在Subject上面。當(dāng)你與Subject進(jìn)行交互的時(shí)候,實(shí)際上是SecurityManager在背后幫你舉起Subject來(lái)做一些安全操作。
- Realms:Realms作為Shiro和應(yīng)用的連接橋,當(dāng)需要與安全數(shù)據(jù)交互的時(shí)候,像用戶賬戶,或者訪問(wèn)控制,Shiro就從一個(gè)或多個(gè)Realms中查找。Shiro提供了一些可以直接使用的Realms,如果默認(rèn)的Realms不能滿足你的需求,你也可以定制自己的Realms
Shiro整體架構(gòu)
Authenticator:認(rèn)證器,管理登陸與登出。
Authorizer:授權(quán)器,賦予主體權(quán)限。
Session Manager:session管理器,session管理機(jī)制。不借助任何web容器使用session
Session Dao:session操作,主要增刪改查。
Cache Manager:緩存管理器
Pluggable Realms(1 or more):shiro與數(shù)據(jù)庫(kù)的連接,認(rèn)證授權(quán)校驗(yàn)
Cryptography:數(shù)據(jù)加密
Shiro核心組件
-
Subject 即主體,外部引用于 Subject 進(jìn)行交互,Subject 記錄了當(dāng)前操作用戶,將用戶的概念理解為當(dāng)前操作的主體,可能是一個(gè)通過(guò)瀏覽器請(qǐng)求的用戶,也可能是一個(gè)運(yùn)行的程序。Subject 在 Shrio 中是一個(gè)接口,接口中定義了很多認(rèn)證授權(quán)相關(guān)的方法,外部程序通過(guò) Sbject 進(jìn)行認(rèn)證授權(quán),而 Sbject 是通過(guò) SecurityManager 安全管理器進(jìn)行認(rèn)證授權(quán)的。
-
SecurityManager 即安全管理器,對(duì)全部的 Sbject 進(jìn)行安全管理,它是 Shrio 的核心,通過(guò) SecurityManager 可以完成全部 Sbject 的認(rèn)證、授權(quán)等。實(shí)質(zhì)上 SecurityManager 是通過(guò) Authenticator 進(jìn)行認(rèn)證,通過(guò) Authorizer 進(jìn)行授權(quán),通過(guò) SessionManager 進(jìn)行會(huì)話管理等。SecurityManager是一個(gè)接口,繼承了 Authenticator ,Authorizer ,SessionManager 這三個(gè)接口。
-
Authenticator即認(rèn)證器,對(duì)用戶身份進(jìn)行認(rèn)證,Authenticator 是一個(gè)接口,Shrio 提供了 ModularRealmAuthenticator實(shí)現(xiàn)類,通過(guò)ModularRealmAuthenticator 基本上可以滿足大多數(shù)需求,也可以自定義認(rèn)證器。
Authorizer即授權(quán)器,用戶通過(guò)認(rèn)證器認(rèn)證通過(guò),在訪問(wèn)功能時(shí)需要通過(guò)授權(quán)器判斷用戶是否有此功能的操作權(quán)限。 -
Realm即領(lǐng)域,相當(dāng)于 datasource 數(shù)據(jù)源,SecurityManager 進(jìn)行安全認(rèn)證需要通過(guò) Realm 獲取用戶權(quán)限數(shù)據(jù),比如:如果用戶身份數(shù)據(jù)在數(shù)據(jù)庫(kù),那么 Realm 就需要從數(shù)據(jù)庫(kù)獲取用戶身份信息。不要把 Realm 理解成只是從數(shù)據(jù)源取數(shù)據(jù),在 Realm 中還有認(rèn)證授權(quán)校驗(yàn)的相關(guān)代碼。
-
SessionManager 即會(huì)話管理,Shrio 框架定義了一套會(huì)話管理,它不依賴 web 容器的 Session ,所以Shrio 可以使用在非 Web 應(yīng)用上,也可以將分布式應(yīng)用的會(huì)話集中在一點(diǎn)管理,此特性可使它實(shí)現(xiàn)單點(diǎn)登錄。
-
SessionDAO 即會(huì)話DAO,是對(duì) Session 會(huì)話操作的一套接口,比如要將 Session 存儲(chǔ)到數(shù)據(jù)庫(kù),可以通過(guò) jdbc 將會(huì)話存儲(chǔ)到數(shù)據(jù)庫(kù)。
-
CacheManager 即緩存管理,將用戶權(quán)限數(shù)據(jù)存儲(chǔ)在緩存,這樣可以提高性能。
-
Cryptografy即密碼管理,Shrio 提供了一套解密/加密的組件,方便開發(fā)。比如提供常用的散列、加/解密功能。
2.Shiro環(huán)境搭建
主要分三步
1.導(dǎo)入maven
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.6.0</version></dependency>2.編寫Realm
public class UserRealm extends AuthorizingRealm {//授權(quán)@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {return null;}//認(rèn)證@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {return null;} }3.編寫ShiroConfig
@Configuration public class ShiroConfig {//ShiroFilterFactoryBean 3@Beanpublic ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();//設(shè)置安全管理器bean.setSecurityManager(defaultWebSecurityManager);return bean;}//DefaultWebSecurityManager 2@Beanpublic DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(userRealm);return securityManager;}//創(chuàng)建realm對(duì)象 1@Bean(name = "userRealm")public UserRealm userRealm(){return new UserRealm();} }3.Shiro用戶登錄攔截與認(rèn)證
shiro的內(nèi)置過(guò)濾器各個(gè)權(quán)限
- anno:無(wú)需認(rèn)證就可以訪問(wèn)
- authc:必須認(rèn)證了才能訪問(wèn)
- user:必須擁有記住我功能才能訪問(wèn)
- perms:必須擁有對(duì)某個(gè)資源的權(quán)限才能訪問(wèn)
- role:擁有某個(gè)角色權(quán)限才能訪問(wèn)
在getShiroFilterFactoryBean中添加內(nèi)置過(guò)濾器實(shí)現(xiàn)登錄攔截
bean.setLoginUrl("/user/toLogin"); 可以設(shè)置攔截時(shí)的重定向
在UserRealm 下的 doGetAuthenticationInfo方法中 添加用戶執(zhí)行認(rèn)證
@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {System.out.println("執(zhí)行了認(rèn)證");String username = "root";String password = "123456";UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;if (!userToken.getUsername().equals(username)){return null; //拋出異常,UnknownAccountException}//密碼認(rèn)證,shiro自己做return new SimpleAuthenticationInfo("",password,"");}在controller中獲取用戶,封裝用戶登錄的數(shù)據(jù)和登錄認(rèn)證
@PostMapping("/login")public String login(String username, String password, Model model){//獲取當(dāng)前的用戶Subject subject = SecurityUtils.getSubject();//封裝用戶的登錄數(shù)據(jù)UsernamePasswordToken token = new UsernamePasswordToken(username,password);//執(zhí)行登錄方法,如果說(shuō)沒(méi)有異常就說(shuō)明登錄成功try {//登陸成功返回首頁(yè)subject.login(token);return "index";}catch (UnknownAccountException e){//用戶名錯(cuò)誤返回登錄頁(yè)model.addAttribute("msg","用戶名錯(cuò)誤");return "login";}catch (IncorrectCredentialsException e){//密碼錯(cuò)誤返回登錄頁(yè)model.addAttribute("msg","密碼錯(cuò)誤");return "login";}}總結(jié)
以上是生活随笔為你收集整理的Springbooot集成Shiro简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring Security实现登录权
- 下一篇: beanfactorypostproce