ApacheShrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解

一、Shiro整體概述

創(chuàng)新互聯(lián)公司專注于根河企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。根河網(wǎng)站建設(shè)公司,為根河等地區(qū)提供建站服務(wù)。全流程按需制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

1.簡(jiǎn)介

Apache Shiro是Java的一個(gè)安全框架,功能強(qiáng)大,使用簡(jiǎn)單,Shiro為開發(fā)人員提供了一個(gè)直觀而全面的認(rèn)證(登錄),授權(quán)(判斷是否含有權(quán)限),加密(密碼加密)及會(huì)話管理(Shiro內(nèi)置Session)的解決方案.

2.Shiro組件

Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解

3.Shiro架構(gòu)

3.1 外部架構(gòu)(以應(yīng)用程序角度)

Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解

3.2 內(nèi)部架構(gòu)

Apache Shrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解

4. Shiro的過濾器

過濾器簡(jiǎn)稱

對(duì)應(yīng)的java類

anon

org.apache.shiro.web.filter.authc.AnonymousFilter

authc

org.apache.shiro.web.filter.authc.FormAuthenticationFilter

authcBasic

org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

perms

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter

port

org.apache.shiro.web.filter.authz.PortFilter

rest

org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

roles

org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

ssl

org.apache.shiro.web.filter.authz.SslFilter

user

org.apache.shiro.web.filter.authc.UserFilter

logout

org.apache.shiro.web.filter.authc.LogoutFilter

挑幾個(gè)重要的說明一下:

anon:匿名過濾器,不登錄也可以訪問的資源使用,比如首頁(yè),一些靜態(tài)資源等

authc:認(rèn)證過濾器,登錄成功后才能訪問的資源使用

perms:授權(quán)過濾器,必須具備某種權(quán)限才能訪問

roles:角色過濾器,必須具備某種角色才能訪問

注意:這么多過濾器,使用起來肯定不方便,Shiro框架也考慮到了這一點(diǎn),所以有一個(gè)過濾器,一個(gè)頂十個(gè),即DelegatingFilterProxy.

5. Shiro與Spring整合

5.1 pom.xml

<!--shiro和spring整合-->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.3.2</version>
    </dependency>

5.2 web.xml

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

5.3applicationContext-shiro.xml

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--安全管理器,需要注入realm域,如果有緩存配置,還需要注入緩存管理器-->
  <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!--引用自定義的realm -->
    <property name="realm" ref="authRealm"/>
    <!--引入緩存管理器-->
    <property name="cacheManager" ref="cacheManager"/>
  </bean>

  <!-- 自定義Realm域的編寫 -->
  <bean id="authRealm" class="com.itheima.web.shiro.AuthRealm">
    <!-- 注入自定義的密碼比較器 -->
    <property name="credentialsMatcher" ref="customerCredentialsMatcher"></property>
  </bean>

  <!-- 自定義的密碼比較器 -->
  <bean id="customerCredentialsMatcher" class="com.itheima.web.shiro.CustomCredentialsMatcher"></bean>

  <!--緩存配置-->
  <!--內(nèi)置(windows)緩存配置:MemoryConstrainedCacheManager-->
  <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"></bean>

  <!-- filter-name這個(gè)名字的值來自于web.xml中filter的名字 -->
  <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <!--登錄頁(yè)面 -->
    <property name="loginUrl" value="/login.jsp"></property>
    <!-- 登錄失敗后 -->
    <property name="unauthorizedUrl" value="/unauthorized.jsp"></property>

    <property name="filterChainDefinitions">
      <!-- /**代表下面的多級(jí)目錄也過濾 -->
      <value>
        /system/module/list.do = perms["模塊管理"]<!--路徑和模塊名稱一定要和數(shù)據(jù)庫(kù)表中存儲(chǔ)的數(shù)據(jù)一致-->
        /index.jsp* = anon<!--anon 不登錄也可以訪問的資源-->
        /login.jsp* = anon
        /login* = anon
        /logout* = anon
        /css/** = anon
        /img/** = anon
        /plugins/** = anon
        /make/** = anon
        /** = authc
      </value>
    </property>
  </bean>

  <!-- 保證實(shí)現(xiàn)了Shiro內(nèi)部lifecycle函數(shù)的bean執(zhí)行 -->
  <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

  <!-- 生成代理,通過代理進(jìn)行控制 -->
  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
     depends-on="lifecycleBeanPostProcessor">
    <property name="proxyTargetClass" value="true"/>
  </bean>

  <!-- 安全管理器 -->
  <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
  </bean>

  <!--支持Shiro注解配置-->
  <aop:aspectj-autoproxy proxy-target-class="true"/>

</beans>

5.4 如果想看具體的實(shí)現(xiàn)代碼(含sql腳本),可以點(diǎn)擊頁(yè)面右上角Fork me on github,到我的github倉(cāng)庫(kù)中拉取

倉(cāng)庫(kù)地址:  https://github.com/AdilCao/Shiro.git

代碼部分只需要關(guān)注三個(gè)類:

1.LoginController(登錄,在這里獲取Subject主體,調(diào)用subject.login()方法后直接調(diào)用認(rèn)證方法)

2.AuthRealm(認(rèn)證和授權(quán)在這個(gè)類中定義,認(rèn)證成功后調(diào)用密碼比較器進(jìn)行比較;授權(quán)即查找登錄用戶所具有的權(quán)限模塊集合)

3.CustomCredentialsMatcher(密碼比較器,將瀏覽器輸入明文密碼加密后,與數(shù)據(jù)庫(kù)中的安全密碼進(jìn)行比較)

注意:整個(gè)過程中如果登錄不成功,就會(huì)拋出異常

名稱欄目:ApacheShrio安全框架實(shí)現(xiàn)原理及實(shí)例詳解
標(biāo)題URL:http://bm7419.com/article46/igechg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)微信小程序、網(wǎng)站改版網(wǎng)頁(yè)設(shè)計(jì)公司網(wǎng)站營(yíng)銷、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)