天天看點

權限控制架構Shiro簡單介紹及配置執行個體

簡單介紹了權限管理架構apache shiro,包括其宏觀和微觀架構,并提供了生産配置執行個體。

Shiro是什麼

http://shiro.apache.org/

Apache Shiro是一個非常易用的Java安全架構,它能提供驗證、授權、加密和Session控制。Shiro非常輕量級,而且API也非常易于了解,可以使用Shiro完成從APP到企業級應用的所有權限控制。

宏觀視圖

從宏觀來看,Shiro架構中有3個重要概念,Subjct、SecurityManager和Realms。

權限控制架構Shiro簡單介紹及配置執行個體

ŸSubject

Subject實際上是正在執行的使用者的抽象,“使用者”這裡可以指自然人,第三方服務,代理賬戶或者其他。

Subject被綁定在SecurityManager上,當我們與Subject互動時,實際上是與SecurityManager互動。

SecurityManager

SecurityManager是Shiro權限架構的核心,内部維護了一系列安全元件。然而,我們一旦将其配置完成,真正給使用者強相關的就是Subject接口了。

當我們操作subject時,實際上就是在操作SecurityManager。

Realms

Reamls是Shiro與我們應用的安全資料溝通的橋梁,在Realm中真正實作使用者登入與授權的邏輯。

從這個角度上來講,Realms其實是一個安全領域的DAO,發将相關資料封裝并提供給Shiro,當使用Shiro時,我們必須制定至少一個Realms。

SecurityManager可以配置多個Realms,但是至少一個。

Shiro已經提供了預設的DAO實作,如LDAP和JDBC,另外我們也能實作自己的DAO(例如使用Redis)。

細節視圖

權限控制架構Shiro簡單介紹及配置執行個體

ŸSubject(org.apache.shiro.subject.Subject)

與目前軟體互動的安全視角内的使用者抽象(使用者、第三方服務)

SecurityManager(org.apache.shiro.mgt.SecurityManager)

Shiro安全架構的核心,像保護傘一樣管理着和協調其内部元件,確定其協調運作。它也維護着每個使用者的Shiro角色,是以它知道使用者的所有安全操作。

Authenticator(org.apache.shiro.authc.Authenticator)

負責執行和驗證使用者登入行為的元件,當一個使用者試圖登入,該邏輯是由Authenticator執行的。Authenticator知道如何去協調一個或者更多的realms,這些realms儲存着使用者資訊。而且realms中的資料被取出用來驗證使用者。

Authentication Strategy(org.apache.shiro.)

如果配置了多個realms,Authentication Strategy将負責協調各Realms的判斷邏輯。

ŸAuthorizer(org.apache.shiro.authz.Authorizer)

使用者控制使用者通路,主要是決定使用者能否通路某些資源。類似于Authenticator,Authorizer也知道如何協調多個資料源并據此判斷這些使用者能否執行某個給定的Action。

SessionManager(org.apache.shiro.session.mgt.SessionManager)

SessionManager知道怎樣建立和管理使用者Session生命周期,進而為使用者提供一個健壯的Session體驗。這種機制是Shiro的獨創,即使不是Web工程,Shiro也能提供内置的Session機制。

SessionDao負責存取Session。

  • SessionDao(org.apache.shiro.session.mgt.eis.SessionDao)

SessionDao替SessionManager完成Session的CRUD操作,它允許任何Session儲存方式(Redis/Memcache/DB..)

CacheManager(org.apache.shiro.cache.CacheManager)

用來儲存Shiro使用的鑒權資料,可以使用任何現有的cache産品。

Cryptography(org.apache.shiro.crypto.*)

加密工具包,根據需要使用

Realms(org.apache.shiro.realm.Realm)

真正與安全相關資料(例如賬戶),我們可以建立任意多的Realm。

配置執行個體

<?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:context="http://www.springframework.org/schema/context"

    default-lazy-init="true" 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-4.0.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

 

    <description>Shiro Configuration</description>

    <!-- Shiro Filter -->

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

        <!-- 安全管理器 -->

        <property name="securityManager" ref="securityManager" />

        <!-- 登陸的Url,暫時沒用 -->

        <property name="loginUrl" value="" />

        <!-- 登入成功的Url,未用 -->

        <property name="successUrl" value="/web/index.html" />

        <!-- 為通過驗證的URL -->

        <property name="unauthorizedUrl" value="/index.html" />

        <property name="filters">

            <map>

                <entry key="authc" value-ref="formAuthenticationFilter" />

            </map>

        </property>

        <property name="filterChainDefinitions">

            <ref bean="shiroFilterChainDefinitions" />

        </property>

    </bean>

    <!-- Shiro權限過濾過濾器定義 -->

    <bean name="shiroFilterChainDefinitions" class="java.lang.String">

        <constructor-arg>

            <value>

                <!-- anon表示不校驗 -->

                /bower_components/** = anon

                

                /info/home/Vh1/**=anon

                /=anon

                <!-- 剩餘請求均經過authc -->

                /** = authc            

            </value>

        </constructor-arg>

    </bean>

    <!-- Form認證過濾器 -->

    <bean id="formAuthenticationFilter"

        class="com.xxxx.xxxx.system.security.FormAuthenticationFilter" />

    <!-- 定義Shiro安全管理配置 -->

    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

        <!-- 單realm應用。如果有多個realm,使用‘realms’屬性代替 -->

        <!-- 管理認證和授權 -->

        <property name="realm" ref="systemAuthorizingRealm" />

        <!-- 僅shiro适用 -->

        <property name="cacheManager" ref="shiroCacheManager" />

        <!-- 分布式或單機session緩存 -->

        <property name="sessionManager" ref="sessionManager" />

        <!-- 自動登入使用,暫時沒有使用 -->

        <property name="rememberMeManager" ref="rememberMeManager" />

    </bean>

 

    <bean id="systemAuthorizingRealm"

        class="com.emcc.xxxx.system.security.shiro.SystemAuthorizingRealm" />

 

    <!-- 自定義會話管理配置 -->

    <bean id="sessionManager"

        class="com.emcc.xxxx.system.security.shiro.session.SessionManager">

        <!-- Redis/本地儲存 -->

        <property name="sessionDAO" ref="sessionDAO" />

        <!-- 會話逾時時間,配置在system.properties 機關:毫秒 -->

        <property name="globalSessionTimeout" value="${session.sessionTimeout}" />

        <!-- 定時清理失效會話, 清理使用者直接關閉浏覽器造成的孤立會話 -->

        <property name="sessionValidationInterval" value="${session.sessionTimeoutClean}" />

        <property name="sessionValidationSchedulerEnabled" value="false" />

        <!-- 配置cookie中sessionid的key -->

        <property name="sessionIdCookie" ref="sessionIdCookie" />

        <property name="sessionIdCookieEnabled" value="true" />

    </bean>

 

    <!-- 指定本系統SESSIONID, 預設為: JSESSIONID 問題: 與SERVLET容器名沖突, 如JETTY, TOMCAT 等預設JSESSIONID,

        當跳出SHIRO SERVLET時如ERROR-PAGE容器會為JSESSIONID重新配置設定值導緻登入會話丢失! -->

    <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">

        <constructor-arg name="name" value="web.session.id" />

    </bean>

    <!-- 自定義Session存儲容器,叢集環境使用 <bean id="sessionDAO" class="com.emcc.xxxx.system.security.shiro.session.JedisSessionDAO">

        <property name="sessionIdGenerator" ref="idGen" /> <property name="sessionKeyPrefix"

        value="${redis.keyPrefix}_session_" /> </bean> -->

    <!-- 自定義Session存儲容器,開發環境使用 -->

    <bean id="sessionDAO"

        class="com.emcc.xxxx.system.security.shiro.session.CacheSessionDAO">

        <property name="sessionIdGenerator" ref="idGen" />

        <property name="activeSessionsCacheName" value="activeSessionsCache" />

        <property name="cacheManager" ref="shiroCacheManager" />

    </bean>

 

    <!-- rememberMe管理器 -->

    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">

        <!-- rememberMe cookie加密的密鑰 建議每個項目都不一樣 預設AES算法 密鑰長度(128 256 512 位) -->

        <property name="cipherKey"

            value="#{T(org.apache.shiro.codec.Base64).decode('4AvVhmFLUs0KTA3Kprsdag==')}" />

        <property name="cookie" ref="rememberMeCookie" />

    </bean>

    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">

        <constructor-arg value="rememberMe" />

        <property name="httpOnly" value="true" />

        <property name="maxAge" value="2592000" /><!-- 30天 -->

    </bean>

    <!-- 定義授權緩存管理器 <bean id="shiroCacheManager" class="com.emcc.xxxx.system.security.shiro.cache.SessionCacheManager"

        /> -->

    <!-- 定義授權緩存管理器 -->

    <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">

        <property name="cacheManager" ref="cacheManager" />

    </bean>

 

    <!-- 保證實作了Shiro内部lifecycle函數的bean執行 -->

    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

 

    <!-- securityManager -->

    <bean

        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">

        <property name="staticMethod"

            value="org.apache.shiro.SecurityUtils.setSecurityManager" />

        <property name="arguments" ref="securityManager" />

    </bean>

 

    <!-- AOP式方法級權限檢查 -->

 

    <!-- AOP式方法級權限檢查 這兩個類主要用于注解 -->

    <bean

        class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">

        <property name="securityManager" ref="securityManager" />

    </bean>

 

</beans>