溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

?springboot中集成shiro框架的方法

發(fā)布時間:2020-05-29 16:06:01 來源:億速云 閱讀:247 作者:鴿子 欄目:編程語言

springboot中集成shiro框架

關(guān)于shior框架的介紹,需要引入相關(guān)jar如下:

    <!--shiro核心jar from www.1b23.com-->
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring</artifactId>
        <version>1.4.0</version>
    </dependency>
    <!--實(shí)現(xiàn)session共享。緩存等-->
    <dependency>
        <groupId>org.crazycake</groupId>
        <artifactId>shiro-redis</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

未整合Spring/SpringBoot以前,是需要在Web.xml中定義org.apache.shiro.web.servlet.ShiroFilter過濾器的
Shiro的初始化工作在web.xml中設(shè)置監(jiān)聽器完成

<listener>   
 <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
 </listener>
 <filter>
     <filter-name>ShiroFilter</filter-name>
     <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
 </filter>
 <filter-mapping>
     <filter-name>ShiroFilter</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>
 Shiro 的 EnvironmentLoaderListener 就是一個典型的 ServletContextListener,它也是整個 Shiro Web 應(yīng)用的入口 。

EventListener 是一個標(biāo)志接口,里面沒有任何的方法,Servlet 容器中所有的 Listener 都要繼承這個接口(這是 Servlet 規(guī)范)。


ServletContextListener 是一個 ServletContext 的監(jiān)聽器,用于監(jiān)聽容器的啟動與關(guān)閉事件,包括如下兩個方法:
void contextInitialized(ServletContextEvent sce); // 當(dāng)容器啟動時調(diào)用
void contextDestroyed(ServletContextEvent sce); // 當(dāng)容器關(guān)閉時調(diào)用

可以從 ServletContextEvent 中直接獲取 ServletContext 對象。

EnvironmentLoaderListener 不僅實(shí)現(xiàn)了 ServletContextListener 接口,也擴(kuò)展了 EnvironmentLoader 類,應(yīng)該是需要在 Servlet 容器中調(diào)用 EnvironmentLoader 對象的生命周期方法
從 Shiro 1.2 開始引入了 Environment/WebEnvironment 的概念,即由它們的實(shí)現(xiàn)提供相應(yīng)的 SecurityManager 及其相應(yīng)的依賴。ShiroFilter 會自動找到 Environment 然后獲取相應(yīng)的依賴。
通過 EnvironmentLoaderListener 來創(chuàng)建相應(yīng)的 WebEnvironment,并自動綁定到 ServletContext,默認(rèn)使用 IniWebEnvironment 實(shí)現(xiàn)。

EnvironmentLoader的功能:

當(dāng)容器啟動時,讀取 web.xml 文件,從中獲取 WebEnvironment 接口的實(shí)現(xiàn)類(默認(rèn)是 IniWebEnvironment),初始化該實(shí)例,并將其加載到 ServletContext 中。
當(dāng)容器關(guān)閉時,銷毀 WebEnvironment 實(shí)例,并從 ServletContext 將其移除。
IniWebEnvironment的功能:

查找并加載 shiro.ini 配置文件,首先從自身成員變量里查找,然后從 web.xml 中查找,然后從 /WEB-INF 下查找,然后從 classpath 下查找,若均未找到,則直接報錯。
當(dāng)找到了 ini 配置文件后就開始解析,此時構(gòu)造了一個 Bean 容器(相當(dāng)于一個輕量級的 IOC 容器),最終的目標(biāo)是為了創(chuàng)建 WebSecurityManager 對象與 FilterChainResolver 對象,創(chuàng)建過程使用了 Abstract Factory 模式
EnvironmentLoaderListener無非就是在容器啟動時創(chuàng)建 WebEnvironment 對象,并由該對象來讀取 Shiro 配置文件,創(chuàng)建WebSecurityManager(安全管理器)與 FilterChainResolver(過濾鏈解析器) 對象,在ShiroFilter中起到了重要作用。

ShiroFilter 是整個 Shiro 的入口點(diǎn),用于攔截需要安全控制的請求進(jìn)行處理。
因?yàn)樗鼣r截了所有的請求,后面的 Authentication(認(rèn)證)和Authorization(授權(quán))都由ShiroFilter說了算

和Spring/SpringBoot整合以后,我們只需要注入ShiroFilter即可,ShiroFilter由ShiroFilterFactoryBean負(fù)責(zé)創(chuàng)建。所以注入ShiroFilterFactoryBean,由 ShiroFilterFactoryBean創(chuàng)建 ShiroFilter即可

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI