欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、Shiro整體概述

1.簡介

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

2.Shiro組件

3.Shiro架構(gòu)

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

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

4. Shiro的過濾器

過濾器簡稱

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

挑幾個重要的說明一下:

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

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

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

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

注意:這么多過濾器,使用起來肯定不方便,Shiro框架也考慮到了這一點(diǎn),所以有一個過濾器,一個頂十個,即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"/>

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

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

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

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

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

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

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

相關(guān)文章

  • SpringBoot項目中訪問HTML頁面的三種方法

    SpringBoot項目中訪問HTML頁面的三種方法

    這篇文章主要介紹了SpringBoot項目中訪問HTML頁面的三種方法,文中通過代碼示例和圖文結(jié)合的方式講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • 詳解如何用spring Restdocs創(chuàng)建API文檔

    詳解如何用spring Restdocs創(chuàng)建API文檔

    這篇文章將帶你了解如何用spring官方推薦的restdoc去生成api文檔。具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Spring的Bean生命周期之BeanDefinition詳解

    Spring的Bean生命周期之BeanDefinition詳解

    這篇文章主要介紹了Spring的Bean生命周期之BeanDefinition詳解,在spring bean創(chuàng)建過程 依賴 BeanDefinition 中的信息處理bean的生產(chǎn),BeanDefinition 是 Spring Framework 中定義 Bean 的配置元信息接口,需要的朋友可以參考下
    2023-12-12
  • Java多種方法實現(xiàn)合并多個list對象列表

    Java多種方法實現(xiàn)合并多個list對象列表

    Java編程中,合并多個列表對象可以通過Stream?API或傳統(tǒng)循環(huán)方式實現(xiàn),使用Stream?API合并時,利用flatMap方法將嵌套的List展平,再通過collect方法收集成一個新的列表,傳統(tǒng)循環(huán)則通過創(chuàng)建一個空的ArrayList,并通過遍歷每個列表將元素添加進(jìn)去
    2024-09-09
  • SpringBoot中MapStruct實現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制

    SpringBoot中MapStruct實現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制

    本文主要介紹了SpringBoot中MapStruct實現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • Java實現(xiàn)簡單的酒店管理系統(tǒng)

    Java實現(xiàn)簡單的酒店管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Spring Security實現(xiàn)退出登錄和退出處理器

    Spring Security實現(xiàn)退出登錄和退出處理器

    本文主要介紹了Spring Security實現(xiàn)退出登錄和退出處理器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Gradle快速安裝及入門

    Gradle快速安裝及入門

    今天小編就為大家分享一篇關(guān)于Gradle快速安裝及入門,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Java類獲取Spring中bean的5種方式

    Java類獲取Spring中bean的5種方式

    這篇文章主要為大家詳細(xì)介紹了Java類獲取Spring中bean的5種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • java根據(jù)擴(kuò)展名獲取系統(tǒng)圖標(biāo)和文件圖標(biāo)示例

    java根據(jù)擴(kuò)展名獲取系統(tǒng)圖標(biāo)和文件圖標(biāo)示例

    這篇文章主要介紹了java根據(jù)擴(kuò)展名獲取系統(tǒng)圖標(biāo)和文件圖標(biāo)示例,需要的朋友可以參考下
    2014-03-03

最新評論