溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

權(quán)限控制框架Shiro簡(jiǎn)單介紹及配置實(shí)例

發(fā)布時(shí)間:2020-07-16 05:00:42 來(lái)源:網(wǎng)絡(luò) 閱讀:1151 作者:zsdnr 欄目:網(wǎng)絡(luò)安全

Shiro是什么

Apache Shiro是一個(gè)非常易用的Java安全框架它能提供驗(yàn)證、授權(quán)、加密和Session控制。Shiro非常輕量級(jí)而且API也非常易于理解可以使用Shiro完成從APP到企業(yè)級(jí)應(yīng)用的所有權(quán)限控制。

宏觀視圖

從宏觀來(lái)看Shiro架構(gòu)中有3個(gè)重要概念Subjct、SecurityManager和Realms。

權(quán)限控制框架Shiro簡(jiǎn)單介紹及配置實(shí)例 

Subject

Subject實(shí)際上是正在執(zhí)行的用戶的抽象“用戶”這里可以指自然人第三方服務(wù)代理賬戶或者其他。

Subject被綁定在SecurityManager上當(dāng)我們與Subject交互時(shí)實(shí)際上是與SecurityManager交互。

SecurityManager

SecurityManager是Shiro權(quán)限架構(gòu)的核心內(nèi)部維護(hù)了一系列安全組件。然而我們一旦將其配置完成真正給用戶強(qiáng)相關(guān)的就是Subject接口了。

當(dāng)我們操作subject時(shí)實(shí)際上就是在操作SecurityManager。

Realms

Reamls是Shiro與我們應(yīng)用的安全數(shù)據(jù)溝通的橋梁在Realm中真正實(shí)現(xiàn)用戶登錄與授權(quán)的邏輯。

從這個(gè)角度上來(lái)講Realms其實(shí)是一個(gè)安全領(lǐng)域的DAO發(fā)將相關(guān)數(shù)據(jù)封裝并提供給Shiro當(dāng)使用Shiro時(shí)我們必須制定至少一個(gè)Realms。

SecurityManager可以配置多個(gè)Realms但是至少一個(gè)。

Shiro已經(jīng)提供了默認(rèn)的DAO實(shí)現(xiàn)如LDAP和JDBC另外我們也能實(shí)現(xiàn)自己的DAO例如使用Redis。

細(xì)節(jié)視圖

權(quán)限控制框架Shiro簡(jiǎn)單介紹及配置實(shí)例 

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

與當(dāng)前軟件交互的安全視角內(nèi)的用戶抽象用戶、第三方服務(wù)

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

Shiro安全框架的核心像保護(hù)傘一樣管理著和協(xié)調(diào)其內(nèi)部組件確保其協(xié)調(diào)運(yùn)行。它也維護(hù)著每個(gè)用戶的Shiro角色因此它知道用戶的所有安全操作。

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

負(fù)責(zé)執(zhí)行和驗(yàn)證用戶登錄行為的組件當(dāng)一個(gè)用戶試圖登錄該邏輯是由Authenticator執(zhí)行的。Authenticator知道如何去協(xié)調(diào)一個(gè)或者更多的realms這些realms保存著用戶信息。而且realms中的數(shù)據(jù)被取出用來(lái)驗(yàn)證用戶。

Authentication Strategy(org.apache.shiro.)

如果配置了多個(gè)realmsAuthentication Strategy將負(fù)責(zé)協(xié)調(diào)各Realms的判斷邏輯。

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

用戶控制用戶訪問(wèn)主要是決定用戶能否訪問(wèn)某些資源。類似于AuthenticatorAuthorizer也知道如何協(xié)調(diào)多個(gè)數(shù)據(jù)源并據(jù)此判斷這些用戶能否執(zhí)行某個(gè)給定的Action。

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

SessionManager知道怎樣創(chuàng)建和管理用戶Session生命周期從而為用戶提供一個(gè)健壯的Session體驗(yàn)。這種機(jī)制是Shiro的獨(dú)創(chuàng)即使不是Web工程Shiro也能提供內(nèi)置的Session機(jī)制。

SessionDao負(fù)責(zé)存取Session。

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

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

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

用來(lái)保存Shiro使用的鑒權(quán)數(shù)據(jù)可以使用任何現(xiàn)有的cache產(chǎn)品。

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

加密工具包根據(jù)需要使用

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

真正與安全相關(guān)數(shù)據(jù)例如賬戶我們可以創(chuàng)建任意多的Realm。

配置實(shí)例

權(quán)限控制框架Shiro簡(jiǎn)單介紹及配置實(shí)例

<?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,暫時(shí)沒(méi)用 -->

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

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

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

        <!-- 為通過(guò)驗(yàn)證的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權(quán)限過(guò)濾過(guò)濾器定義 -->

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

        <constructor-arg>

            <value>

                <!-- anon表示不校驗(yàn) -->

                /bower_components/** = anon

                

                /info/home/Vh2/**=anon

                /=anon                <!-- 剩余請(qǐng)求均經(jīng)過(guò)authc -->

                /** = authc            

            </value>

        </constructor-arg>

    </bean>

    <!-- Form認(rèn)證過(guò)濾器 -->

    <bean id="formAuthenticationFilter"

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

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

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

        <!-- 單realm應(yīng)用。如果有多個(gè)realm使用‘realms’屬性代替 -->

        <!-- 管理認(rèn)證和授權(quán) -->

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

        <!-- 僅shiro適用 -->

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

        <!-- 分布式或單機(jī)session緩存 -->

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

        <!-- 自動(dòng)登錄使用,暫時(shí)沒(méi)有使用 -->

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

    </bean>

 

    <bean id="systemAuthorizingRealm"

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

 

    <!-- 自定義會(huì)話管理配置 -->

    <bean id="sessionManager"

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

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

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

        <!-- 會(huì)話超時(shí)時(shí)間配置在system.properties 單位毫秒 -->

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

        <!-- 定時(shí)清理失效會(huì)話, 清理用戶直接關(guān)閉瀏覽器造成的孤立會(huì)話 -->

        <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>

 

    <!-- 指定本系統(tǒng)SESSIONID, 默認(rèn)為: JSESSIONID 問(wèn)題: 與SERVLET容器名沖突, 如JETTY, TOMCAT 等默認(rèn)JSESSIONID,

        當(dāng)跳出SHIRO SERVLET時(shí)如ERROR-PAGE容器會(huì)為JSESSIONID重新分配值導(dǎo)致登錄會(huì)話丟失! -->

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

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

    </bean>

    <!-- 自定義Session存儲(chǔ)容器,集群環(huán)境使用 <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存儲(chǔ)容器開(kāi)發(fā)環(huán)境使用 -->

    <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加密的密鑰 建議每個(gè)項(xiàng)目都不一樣 默認(rèn)AES算法 密鑰長(zhǎng)度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>

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

        /> -->

    <!-- 定義授權(quán)緩存管理器 -->

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

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

    </bean>

 

    <!-- 保證實(shí)現(xiàn)了Shiro內(nèi)部lifecycle函數(shù)的bean執(zhí)行 -->

    <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式方法級(jí)權(quán)限檢查 -->

 

    <!-- AOP式方法級(jí)權(quán)限檢查 這兩個(gè)類主要用于注解 -->

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

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

    </bean>

 </beans>


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI