shiro能做什么?
登錄驗證:
不是登錄用戶不能訪問敏感資源,只有登錄了才可以訪問敏感資源
權限驗證:
不同的用戶賬號登錄成功之后,用戶所使用的功能不同
類似的安全框架:
1.spring security 功能完善,學習成本偏高
2.shiro 學習成本低,簡單的安全框架,基本功能存在(登錄認證,權限認證)
3.spring mvc interceptor(攔截器) 只能做登錄認證,不能做權限認證
一、shiro的jar包依賴
在pom.xml文件中配置:
<!-- Apache Shiro 權限依賴 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.2.3</version> </dependency> <!-- spring aop 依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency>
二、shiro的配置文件
配置文件關系注入圖如下:
web.xml文件配置:
創建spring_shiro.xml文件並配置信息交給將shiro配件的bean交給spring容器管理:
三、shiro的原理圖
shiro的登錄認證:
用戶登錄成功就可以訪問敏感資源
之后的所有訪問都通過shiro直接訪問指定的資源
用戶沒有登錄成功,跳轉到指定的登錄頁面
//環繞通知,可以控制目標方法的執行 public Object around(ProceedJoinPoint pjp){ try{ if(登錄過){ returnValue=pjp.proceed(); }else{ //由shiro控制跳轉到指定的頁面,由spring_shiro.xml提供跳轉的目的地 } } Object returnValue=null; }
注意:沒有shiro,項目的功能照樣跑起來,添加shiro實際就是橫切,把shiro橫切到項目中, 實際就是代理模式!!!
shiro的權限認證:
一定是在登錄認證完成后,才能做shiro的權限認證,根據用戶的權限顯示不同
四、shiro的功能模塊圖
shiro的功能列表:
主要的功能:
Authentication:登錄認證
authorrization:權限認證
session management:用shiro管理會話對象
cryptography:加密處理
輔助功能:
web support:shiro可以用在web項目中
caching:緩存 用shiro做緩存管理
concurrency:支持高並發
Testing:用shiro測試
runas :shiro可以應用java項目
remember me:記住我
shiro的使用步驟:
application code:用戶的代碼,代表的一個shiro的啟動入口,用shiro的api來啟動shiro可以理解成把用戶的數據用shiro的api傳遞給shiro,由shiro來處理用戶數據
subject:原義是主題,每一個subject代表一個用戶,抽象的用戶,就是用shiro對用戶的數據
進行封裝,把數據封裝給token(令牌),最終可以狹義理解成是數據的封裝
securitymanager:安全管理中心是shiro的核心,所有的數據都要經過shiro的安全管理中心來管理
realm:英文原義是域或范圍,可以理解成原始數據的源頭,源頭域,
五、策略模型圖
六、實際應用
shiro項目的搭建步驟:
1.創建項目
2.導入jar
a.手動導入
b.maven導入
3.把shiro的對象,交給spring容器來管理
web.xml
添加一個shiro的過濾器,要用這個過濾器過濾所有url
spring_shiro.xml
配置shiro的安全管理中心的對象,還有shiro的過濾器對象,
還有登錄和權限認證的接口的實現對象
4.創建java類,要給相應的接口給實現類
創建java類AuthRealm extends AuthorizingRealm
AuthorizingRealm實現自
implements org.apache.shiro.authz.Authorizer, org.apache.shiro.util.Initializable,
org.apache.shiro.authz.permission.PermissionResolverAware, org.apache.shiro.authz.permission.RolePermissionResolverAware
5.跑項目