shiro權限管理框架簡介(一)

一:什麼是shiro?

       shiroapache的一個開源框架,是一個權限管理的框架,實現 用戶認證、用戶授權。

       spring中有spring security (原名Acegi),是一個權限框架,它和spring依賴過於緊密,沒有shiro使用簡單。

       shiro不依賴於springshiro不僅可以實現 web應用的權限管理,還可以實現c/s系統,分佈式系統權限管理,shiro屬於輕量框架,越來越多企業項目開始使用shiro

使用shiro實現系統 的權限管理,有效提高開發效率,從而降低開發成本。

二:shiro架構

    

subject:主體,可以是用戶也可以是程序,主體要訪問系統,系統需要對主體進行認證、授權。

 

securityManager:安全管理器,主體進行認證和授權都 是通過securityManager進行。

 

authenticator:認證器,主體進行認證最終通過authenticator進行的。

 

authorizer:授權器,主體進行授權最終通過authorizer進行的。

 

sessionManagerweb應用中一般是用web容器對session進行管理,shiro也提供一套session管理的方式。

SessionDao:  通過SessionDao管理session數據,針對個性化的session數據存儲需要使用sessionDao

 

cache Manager:緩存管理器,主要對session和授權數據進行緩存,比如將授權數據通過cacheManager進行緩存管理,和ehcache整合對緩存數據進行管理。

 

realm:域,領域,相當於數據源,通過realm存取認證、授權相關數據。

 

注意:在realm中存儲授權和認證的邏輯。

 

cryptography密碼管理,提供了一套加密/解密的組件,方便開發。比如提供常用的散列、加/解密等功能。

比如 md5散列算法。

三:shiro相關的jar包,這是用maven進行說明

與其它java開源框架類似,將shirojar包加入項目就可以使用shiro提供的功能了。shiro-core是核心包必須選用,還提供了與web整合的shiro-web、與spring整合的shiro-spring、與任務調度quartz整合的shiro-quartz等,下邊是shirojar包的maven座標。

 

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-core</artifactId>

<version>1.2.3</version>

</dependency>

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-web</artifactId>

<version>1.2.3</version>

</dependency>

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-spring</artifactId>

<version>1.2.3</version>

</dependency>

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-ehcache</artifactId>

<version>1.2.3</version>

</dependency>

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-quartz</artifactId>

<version>1.2.3</version>

</dependency>

 

也可以通過引入shiro-all包括shiro所有的包:

<dependency>

<groupId>org.apache.shiro</groupId>

<artifactId>shiro-all</artifactId>

<version>1.2.3</version>

</dependency>