新聞中心
springboot中集成shiro框架
關于shior框架的介紹,需要引入相關jar如下:
org.apache.shiro shiro-spring 1.4.0 org.crazycake shiro-redis 3.2.2 org.springframework.boot spring-boot-starter-data-redis
未整合Spring/SpringBoot以前,是需要在Web.xml
中定義org.apache.shiro.web.servlet.ShiroFilter過濾器的
Shiro的初始化工作在web.xml中設置監(jiān)聽器完成
org.apache.shiro.web.env.EnvironmentLoaderListener ShiroFilter org.apache.shiro.web.servlet.ShiroFilter Shiro 的 EnvironmentLoaderListener 就是一個典型的 ServletContextListener,它也是整個 Shiro Web 應用的入口 。 ShiroFilter /*
EventListener 是一個標志接口,里面沒有任何的方法,Servlet 容器中所有的 Listener 都要繼承這個接口(這是 Servlet 規(guī)范)。
ServletContextListener 是一個 ServletContext 的監(jiān)聽器,用于監(jiān)聽容器的啟動與關閉事件,包括如下兩個方法:
void contextInitialized(ServletContextEvent sce); // 當容器啟動時調(diào)用
void contextDestroyed(ServletContextEvent sce); // 當容器關閉時調(diào)用
可以從 ServletContextEvent 中直接獲取 ServletContext 對象。
EnvironmentLoaderListener 不僅實現(xiàn)了 ServletContextListener 接口,也擴展了 EnvironmentLoader 類,應該是需要在 Servlet 容器中調(diào)用 EnvironmentLoader 對象的生命周期方法
從 Shiro 1.2 開始引入了 Environment/WebEnvironment 的概念,即由它們的實現(xiàn)提供相應的 SecurityManager 及其相應的依賴。ShiroFilter 會自動找到 Environment 然后獲取相應的依賴。
通過 EnvironmentLoaderListener 來創(chuàng)建相應的 WebEnvironment,并自動綁定到 ServletContext,默認使用 IniWebEnvironment 實現(xiàn)。
EnvironmentLoader的功能:
當容器啟動時,讀取 web.xml 文件,從中獲取 WebEnvironment 接口的實現(xiàn)類(默認是 IniWebEnvironment),初始化該實例,并將其加載到 ServletContext 中。
當容器關閉時,銷毀 WebEnvironment 實例,并從 ServletContext 將其移除。
IniWebEnvironment的功能:
查找并加載 shiro.ini 配置文件,首先從自身成員變量里查找,然后從 web.xml 中查找,然后從 /WEB-INF 下查找,然后從 classpath 下查找,若均未找到,則直接報錯。
當找到了 ini 配置文件后就開始解析,此時構(gòu)造了一個 Bean 容器(相當于一個輕量級的 IOC 容器),最終的目標是為了創(chuàng)建 WebSecurityManager 對象與 FilterChainResolver 對象,創(chuàng)建過程使用了 Abstract Factory 模式
EnvironmentLoaderListener無非就是在容器啟動時創(chuàng)建 WebEnvironment 對象,并由該對象來讀取 Shiro 配置文件,創(chuàng)建WebSecurityManager(安全管理器)與 FilterChainResolver(過濾鏈解析器) 對象,在ShiroFilter中起到了重要作用。
ShiroFilter 是整個 Shiro 的入口點,用于攔截需要安全控制的請求進行處理。
因為它攔截了所有的請求,后面的 Authentication(認證)和Authorization(授權(quán))都由ShiroFilter說了算
和Spring/SpringBoot整合以后,我們只需要注入ShiroFilter即可,ShiroFilter由ShiroFilterFactoryBean負責創(chuàng)建。所以注入ShiroFilterFactoryBean,由 ShiroFilterFactoryBean創(chuàng)建 ShiroFilter即可
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文名稱:?springboot中集成shiro框架的方法-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://ef60e0e.cn/article/cchggc.html