Apache Shiro是一個非常易用的Java安全框架它能提供驗證、授權、加密和Session控制。Shiro非常輕量級而且API也非常易于理解可以使用Shiro完成從APP到企業級應用的所有權限控制。
創新互聯致力于互聯網網站建設與網站營銷,提供做網站、成都網站制作、網站開發、seo優化、網站排名、互聯網營銷、微信小程序開發、公眾號商城、等建站開發,創新互聯網站建設策劃專家,為不同類型的客戶提供良好的互聯網應用定制解決方案,幫助客戶在新的全球化互聯網環境中保持優勢。
從宏觀來看Shiro架構中有3個重要概念Subjct、SecurityManager和Realms。
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。
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.)
如果配置了多個realmsAuthentication Strategy將負責協調各Realms的判斷邏輯。
Authorizer(org.apache.shiro.authz.Authorizer)
用戶控制用戶訪問主要是決定用戶能否訪問某些資源。類似于AuthenticatorAuthorizer也知道如何協調多個數據源并據此判斷這些用戶能否執行某個給定的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/Vh2/**=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>
網站標題:權限控制框架Shiro簡單介紹及配置實例
網站地址:http://www.yijiale78.com/article42/ihohhc.html
成都網站建設公司_創新互聯,為您提供網站設計、移動網站建設、微信小程序、自適應網站、網站建設、靜態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯