Apache Shrio安全框架實現(xiàn)原理及實例詳解
一、Shiro整體概述
1.簡介
Apache Shiro是Java的一個安全框架,功能強大,使用簡單,Shiro為開發(fā)人員提供了一個直觀而全面的認證(登錄),授權(判斷是否含有權限),加密(密碼加密)及會話管理(Shiro內置Session)的解決方案.
2.Shiro組件

3.Shiro架構
3.1 外部架構(以應用程序角度)

3.2 內部架構

4. Shiro的過濾器
|
過濾器簡稱 |
對應的java類 |
|
anon |
org.apache.shiro.web.filter.authc.AnonymousFilter |
|
authc |
org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
|
authcBasic |
org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
|
perms |
org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
|
port |
org.apache.shiro.web.filter.authz.PortFilter |
|
rest |
org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
|
roles |
org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
|
ssl |
org.apache.shiro.web.filter.authz.SslFilter |
|
user |
org.apache.shiro.web.filter.authc.UserFilter |
|
logout |
org.apache.shiro.web.filter.authc.LogoutFilter |
挑幾個重要的說明一下:
anon:匿名過濾器,不登錄也可以訪問的資源使用,比如首頁,一些靜態(tài)資源等
authc:認證過濾器,登錄成功后才能訪問的資源使用
perms:授權過濾器,必須具備某種權限才能訪問
roles:角色過濾器,必須具備某種角色才能訪問
注意:這么多過濾器,使用起來肯定不方便,Shiro框架也考慮到了這一點,所以有一個過濾器,一個頂十個,即DelegatingFilterProxy.
5. Shiro與Spring整合
5.1 pom.xml
<!--shiro和spring整合-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
5.2 web.xml
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5.3applicationContext-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--安全管理器,需要注入realm域,如果有緩存配置,還需要注入緩存管理器-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--引用自定義的realm -->
<property name="realm" ref="authRealm"/>
<!--引入緩存管理器-->
<property name="cacheManager" ref="cacheManager"/>
</bean>
<!-- 自定義Realm域的編寫 -->
<bean id="authRealm" class="com.itheima.web.shiro.AuthRealm">
<!-- 注入自定義的密碼比較器 -->
<property name="credentialsMatcher" ref="customerCredentialsMatcher"></property>
</bean>
<!-- 自定義的密碼比較器 -->
<bean id="customerCredentialsMatcher" class="com.itheima.web.shiro.CustomCredentialsMatcher"></bean>
<!--緩存配置-->
<!--內置(windows)緩存配置:MemoryConstrainedCacheManager-->
<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>
<!-- filter-name這個名字的值來自于web.xml中filter的名字 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!--登錄頁面 -->
<property name="loginUrl" value="/login.jsp"></property>
<!-- 登錄失敗后 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp"></property>
<property name="filterChainDefinitions">
<!-- /**代表下面的多級目錄也過濾 -->
<value>
/system/module/list.do = perms["模塊管理"]<!--路徑和模塊名稱一定要和數(shù)據(jù)庫表中存儲的數(shù)據(jù)一致-->
/index.jsp* = anon<!--anon 不登錄也可以訪問的資源-->
/login.jsp* = anon
/login* = anon
/logout* = anon
/css/** = anon
/img/** = anon
/plugins/** = anon
/make/** = anon
/** = authc
</value>
</property>
</bean>
<!-- 保證實現(xiàn)了Shiro內部lifecycle函數(shù)的bean執(zhí)行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!-- 生成代理,通過代理進行控制 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true"/>
</bean>
<!-- 安全管理器 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!--支持Shiro注解配置-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
5.4 如果想看具體的實現(xiàn)代碼(含sql腳本),可以點擊頁面右上角Fork me on github,到我的github倉庫中拉取
倉庫地址: https://github.com/AdilCao/Shiro.git
代碼部分只需要關注三個類:
1.LoginController(登錄,在這里獲取Subject主體,調用subject.login()方法后直接調用認證方法)
2.AuthRealm(認證和授權在這個類中定義,認證成功后調用密碼比較器進行比較;授權即查找登錄用戶所具有的權限模塊集合)
3.CustomCredentialsMatcher(密碼比較器,將瀏覽器輸入明文密碼加密后,與數(shù)據(jù)庫中的安全密碼進行比較)
注意:整個過程中如果登錄不成功,就會拋出異常
相關文章
詳解如何用spring Restdocs創(chuàng)建API文檔
這篇文章將帶你了解如何用spring官方推薦的restdoc去生成api文檔。具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Spring的Bean生命周期之BeanDefinition詳解
這篇文章主要介紹了Spring的Bean生命周期之BeanDefinition詳解,在spring bean創(chuàng)建過程 依賴 BeanDefinition 中的信息處理bean的生產,BeanDefinition 是 Spring Framework 中定義 Bean 的配置元信息接口,需要的朋友可以參考下2023-12-12
SpringBoot中MapStruct實現(xiàn)優(yōu)雅的數(shù)據(jù)復制
本文主要介紹了SpringBoot中MapStruct實現(xiàn)優(yōu)雅的數(shù)據(jù)復制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-08-08
Spring Security實現(xiàn)退出登錄和退出處理器
本文主要介紹了Spring Security實現(xiàn)退出登錄和退出處理器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
java根據(jù)擴展名獲取系統(tǒng)圖標和文件圖標示例
這篇文章主要介紹了java根據(jù)擴展名獲取系統(tǒng)圖標和文件圖標示例,需要的朋友可以參考下2014-03-03

