最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年04月04日 14:26:15   作者:狼_少_年  
這篇文章主要介紹了Apache Shrio安全框架實現(xiàn)原理及實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、Shiro整體概述

1.簡介

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

2.Shiro組件

3.Shiro架構(gòu)

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

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

4. Shiro的過濾器

過濾器簡稱

對應的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

挑幾個重要的說明一下:

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

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

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

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

注意:這么多過濾器,使用起來肯定不方便,Shiro框架也考慮到了這一點,所以有一個過濾器,一個頂十個,即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這個名字的值來自于web.xml中filter的名字 -->
  <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    <property name="securityManager" ref="securityManager"/>
    <!--登錄頁面 -->
    <property name="loginUrl" value="/login.jsp"></property>
    <!-- 登錄失敗后 -->
    <property name="unauthorizedUrl" value="/unauthorized.jsp"></property>

    <property name="filterChainDefinitions">
      <!-- /**代表下面的多級目錄也過濾 -->
      <value>
        /system/module/list.do = perms["模塊管理"]<!--路徑和模塊名稱一定要和數(shù)據(jù)庫表中存儲的數(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>

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

  <!-- 生成代理,通過代理進行控制 -->
  <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 如果想看具體的實現(xiàn)代碼(含sql腳本),可以點擊頁面右上角Fork me on github,到我的github倉庫中拉取

倉庫地址:  https://github.com/AdilCao/Shiro.git

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

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

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

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

注意:整個過程中如果登錄不成功,就會拋出異常

相關(guān)文章

  • 簡單剖析Java中動態(tài)線程池的擴容以及縮容操作

    簡單剖析Java中動態(tài)線程池的擴容以及縮容操作

    這篇文章主要為大家詳細介紹了Java中動態(tài)線程池的擴容以及縮容操作的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • SpringBoot配置Redis實現(xiàn)保存獲取和刪除數(shù)據(jù)

    SpringBoot配置Redis實現(xiàn)保存獲取和刪除數(shù)據(jù)

    本文主要介紹了SpringBoot配置Redis實現(xiàn)保存獲取和刪除數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • SpringBoot事件發(fā)布和監(jiān)聽詳解

    SpringBoot事件發(fā)布和監(jiān)聽詳解

    今天去官網(wǎng)查看spring boot資料時,在特性中看見了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-11-11
  • springboot2啟動時執(zhí)行,初始化(或定時任務(wù))servletContext問題

    springboot2啟動時執(zhí)行,初始化(或定時任務(wù))servletContext問題

    這篇文章主要介紹了springboot2啟動時執(zhí)行,初始化(或定時任務(wù))servletContext問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • MyBatisPlus 封裝分頁方法示例

    MyBatisPlus 封裝分頁方法示例

    本文主要介紹了基于MybatisPlus的分頁插件封裝,包括分頁結(jié)果對象、查詢對象的封裝,以及對象轉(zhuǎn)換處理,具有一定的參考價值,感興趣的可以了解一下
    2024-12-12
  • 基于Java解決華為機試之字符串加解密?

    基于Java解決華為機試之字符串加解密?

    這篇文章主要介紹了基于Java解決華為機試之字符串加解密,問題描述展開主題即詳細代碼的分享完成文章內(nèi)容,具有一的的參考價值,需要的小伙伴可以參考一下。希望對你有所幫助
    2022-02-02
  • Springboot深入講解nocos的整合與使用

    Springboot深入講解nocos的整合與使用

    Nacos?是阿里巴巴推出來的一個新開源項目,這是一個更易于構(gòu)建云原生應用的動態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺,在項目開發(fā)過程中,我們經(jīng)常使用nacos作為配置中心和注冊中心。本文章我們就從代碼層面研究下springboot是如何整合nacos使用的
    2022-07-07
  • JAVA使用SimpleDateFormat類表示時間代碼實例

    JAVA使用SimpleDateFormat類表示時間代碼實例

    這篇文章主要介紹了JAVA使用SimpleDateFormat類表示時間代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Java設(shè)計模式之策略模式案例詳解

    Java設(shè)計模式之策略模式案例詳解

    策略模式(Strategy?Pattern)定義了一組同類型的算法,在不同的類中封裝起來,每種算法可以根據(jù)當前場景相互替換,從而使算法的變化獨立于使用它們的客戶端即算法的調(diào)用者
    2022-07-07
  • Java線程間協(xié)作wait、notify和notifyAll詳解

    Java線程間協(xié)作wait、notify和notifyAll詳解

    這篇文章主要介紹了Java線程間協(xié)作wait、notify和notifyAll詳解,在 Java 中可以用 wait、notify 和 notifyAll 來實現(xiàn)線程間的通信,盡管關(guān)于wait和notify的概念很基礎(chǔ),它們也都是Object類的函數(shù),但用它們來寫代碼卻并不簡單,,需要的朋友可以參考下
    2023-10-10

最新評論

中江县| 南开区| 蒲城县| 临武县| 文山县| 安平县| 丹棱县| 安阳市| 昭苏县| 南江县| 抚顺市| 平果县| 忻州市| 石台县| 达州市| 东莞市| 平利县| 东港市| 临安市| 永丰县| 莱州市| 禹城市| 宜黄县| 德州市| 军事| 偏关县| 彭阳县| 姚安县| 大洼县| 浙江省| 图们市| 土默特右旗| 廊坊市| 云南省| 广丰县| 崇左市| 武汉市| 黑山县| 民权县| 中江县| 沙坪坝区|