前言
之前在開發中聽到有關權限管理的字眼,由於是第一次聽到,所以上百度搜了一下,發現出現頻率比較高的就是Shiro框架。碰巧,在實際開發中也遇到了使用Shrio框架的場景。所以想着自己也有必要去學習一下這個Shiro框架。這裏就先梳理一下什麼是Shiro框架。
什麼是安全框架
安全框架顧名思義一定是和安全有關的,它應該是在軟甲系統開發中對於軟件整體和系統能夠提高其安全性。我們可以想一想,我們的軟件和系統在哪些情況下需要較高的安全性。比如我們在註冊賬號的時候我們的密碼如果沒有加密,別有用心的人通過不同的手段獲得了我們的密碼就可以登錄我們的賬號,那我們的個人隱私不就被看光光了。。再比如,有些系統對於不同的用戶會展示不同的信息。一些較爲高層的信息,不對普通用戶展示,這就需要設計權限管理。所以一個軟件或者系統需要正常穩定地運行是離不開安全框架的。安全框架爲軟件提供了許多方便的接口,用於進行安全管理。就java而言用的比較多的就是Shiro框架和Spring全家桶的Spring Security。
Shiro安全框架
Shiro安全框架是一個強大靈活的安全框架,相比於Spring Security,它更加的輕量級,對於開發者更加容易上手和操作。
我們訪問Shiro的官方網站可以看到
Shiro框架主要由四部分組成,從左到右從上到下分別是身份驗證,授權,會話管理,密碼。所支持的特性分別是網絡支持,緩存,併發性,測試,運行和記住賬號。
身份驗證(Authentication):指讓系統知道現在登錄的人是誰
授權(Authorization):對於登錄的用戶賦予權限,用戶獲得權限後可以進行與權限相對應的操作
會話管理(Session Management):特指用戶的會話管理
密碼(Cryptography):用戶登錄時對密碼進行加密的安全手段
網絡支持(Web Support):框架可以應用於web程序,提高安全性
緩存(Caching):能夠使用戶在確保安全的前提下進行高效快速的操作
併發性(Concurrency):框架支持多線程
測試(Testing):框架可以向JUnit那樣幫助開發者進行單元測試和集成測試
運行(Run As):在某些管理場景中會用到(這裏不是很清楚官網所寫的意思)
記住我(Remember Me):在很多登錄頁可見,把賬號信息存在會話中,便於下次登錄
到此,我們大概瞭解了Shiro的組成和主要功能。我們再來看一些再安全框架中比較常見的名詞,這對我們理解框架會有幫助
通常安全框架會與數據庫緊密相連。數據庫ji建表一般從主體(用戶),角色,權限三個來考慮。這樣的顆粒度比較細。
這個是我大概設計的一個數據庫分爲用戶表,角色表,權限表,用戶角色關聯表,角色權限關聯表
有數據庫基礎的應該可以看懂,我不贅述了。
角色,在安全框架中這是一個隱式的安全策略。怎麼說呢,我們先回到之前的權限上去,權限,指的是程序可以做什麼事,它是安全策略中最低一層的元素。我們設想如果將用戶和權限連接綁定,再借助框架是不是就可以做到賦予用戶權限,同時用戶在系統中也按照各自的權限進行瀏覽,編輯和刪除操作。確實是這樣。但是如果這是一個非常龐大的用戶集呢?
爲每個用戶要去綁定多條權限,一來比較繁瑣,權限修改不方便,二來數據量大,有些冗餘。也許編程設計的思想是相通的,我覺得這是一種類似於解耦合的操作,加入了角色的概念。前面說角色是一種隱式的策略,因爲角色可以看做是多個權限的集合,提高管理者對安全的可控性。比如在之前的設想中,用戶量增大到一定程度,我們發現有些用戶所具有的的權限是一樣,我們把這些用戶進行分組,對於一組的用戶,都具有相同的權限,也就是有了一個角色。把用戶、權限用角色綁定起來,對外界來說權限不可見,因此可以說是隱式的。這種模式下,也方便對於大規模用戶進行權限的管理,也方便對於新用戶進行授權。
好了,現在我們開始,實戰一下。這次我先準備的是Shiro和SSM框架的整合,首先通過Maven引入依賴包,我的話是引了這些包主要是核心包,web包,緩存的,和spring整合的
<!-- Shiro的jar包 Start -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.2.1</version>
</dependency>
然後我的話是在主配置XML文件裏引了Shrio配置,把shiro單獨放一個文件,這樣比較清楚。
<!--導入shiro配置-->
<import resource="spring-shiro.xml"/>
這裏先web.xml中加個shiro的過濾器,所有的請求都會被攔截,通過shiro進行安全管理
<!--Shiro過濾器-->
<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>
然後shiro的配置文件內容還是比較多的,英語好的同學可以去shiro官網看和spring的整合教程,我看了一下還是比較容易理解的
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--shrio過濾器-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!--未授權-->
<property name="unauthorizedUrl" value="/index.jsp"/>
<property name="filters">
<util:map>
<entry key="authc" value-ref="formAuthenticationFilter"/>
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
/user/login = anon
/** = authc
</value>
</property>
</bean>
<!--表單驗證過濾器-->
<bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
<property name="usernameParam" value="userName"/>
<property name="passwordParam" value="passWord"/>
<property name="loginUrl" value="/"/>
<property name="rememberMeParam" value="rememberMe"/>
</bean>
<!--安全管理器-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="realm" ref="myRealm"/>
<property name="cacheManager" ref="cacheManger"/>
<property name="rememberMeManager" ref="RememberMeManager"/>
<!-- By default the servlet container sessions will be used. Uncomment this line
to use shiro's native sessions (see the JavaDoc for more): -->
<!-- <property name="sessionMode" value="native"/> -->
</bean>
<!-- Shiro生命週期處理器-->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<!--Realm-->
<bean id="myRealm" class="com.webShopBack.shrio.myRealm">
<!--配置緩存管理器-->
<property name="cacheManager" ref="cacheManger"/>
<!--配置加密器-->
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"/>
<property name="hashIterations" value="2"/>
</bean>
</property>
</bean>
<!--用戶授權信息Cache-->
<bean id="cacheManger" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"/>
<!--啓用shiro註解-->
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!-- Secure Spring remoting: Ensure any Spring Remoting method invocations -->
<!-- can be associated with a Subject for security checks. -->
<!--Server-side Configuration-->
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<property name="securityManager" ref="securityManager"/>
</bean>
<!--Client-side Configuration-->
<bean id="secureRemoteInvocationFactory" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationFactory"/>
<!--rememberMe管理器-->
<bean class="org.apache.shiro.web.mgt.CookieRememberMeManager" id="RememberMeManager">
<property name="cookie" ref="cookie"/>
<property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/>
</bean>
<!--SessionCookie模板-->
<bean class="org.apache.shiro.web.servlet.SimpleCookie" id="cookie">
<constructor-arg value="rememberMe"/>
<!--<property name="httpOnly" value="true"/>-->
<property name="maxAge" value="36000"/>
</bean>
</beans>
這裏配置了shiro,我們結合官網來看
首先是shiroFilter,這也就是之前在web.xml中出現過的過濾器,主要用於過濾我們的請求,哪些請求需要處理,哪些不需要;而需要處理的又該怎麼處理。這裏主要是由這個過濾器鏈filterChainDefinitions實現的。它定義了不同路由所對應的過濾器,在shiro中有個自的名稱和含義。
anon:不需要執行任何的安全檢查,一般就是一些遊客登錄可以訪問的頁面和登錄頁
authc:要求用戶進行身份驗證,如果驗證不通過會強制重定向到登錄頁面進行登錄驗證
authcbasic:同樣要求用戶進行身份驗證,不同的是這個是使用HTTP basic協議進行認證(感興趣的同學可以去查一下相關資料)
logout:退出登錄
這裏我定義是除了登錄路由外別的都需要認證
<!--shrio過濾器-->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!--未授權-->
<property name="unauthorizedUrl" value="/index.jsp"/>
<property name="filters">
<util:map>
<entry key="authc" value-ref="formAuthenticationFilter"/>
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
/user/login = anon
/** = authc
</value>
</property>
</bean>
然後比較重要的這個Realm這裏我們會使用自定義的Realm,在Realm中主要是實現登錄的驗證和權限的授予。然後定義了加密器的加密規則。這裏選擇的是MD5加密,次數爲2。
<!--Realm-->
<bean id="myRealm" class="com.webShopBack.shrio.myRealm">
<!--配置緩存管理器-->
<property name="cacheManager" ref="cacheManger"/>
<!--配置加密器-->
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="MD5"/>
<property name="hashIterations" value="2"/>
</bean>
</property>
</bean>
另外比較重要的是remember me 和Session 管理,緩存管理
<!--rememberMe管理器-->
<bean class="org.apache.shiro.web.mgt.CookieRememberMeManager" id="RememberMeManager">
<property name="cookie" ref="cookie"/>
<property name="cipherKey" value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}"/>
</bean>
<!--SessionCookie模板-->
<bean class="org.apache.shiro.web.servlet.SimpleCookie" id="cookie">
<constructor-arg value="rememberMe"/>
<!--<property name="httpOnly" value="true"/>-->
<property name="maxAge" value="36000"/>
</bean>
<!--用戶授權信息Cache-->
<bean id="cacheManger" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"/>
這裏主要是用戶在認證之後信息的一些存儲的管理,設置一些信息存儲的過期時間
最後我們在具體使用Shiro 的時候會用到Shiro的註解因此再加一個配置
<!--啓用shiro註解-->
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
<!-- the lifecycleBeanProcessor has run: -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
到此Shiro和ssm框架的配置就基本完成了。
後面我們再討論如何使用Shiro框架進行開發。