前后分離的shiro權(quán)限

前陣子在前后端分離項(xiàng)目中集成shiro項(xiàng)目,折騰了一下子,參考了網(wǎng)上一些博客,發(fā)現(xiàn)大多都還是之前傳統(tǒng)的模式,并不適用于前后端分離結(jié)構(gòu)。今天抽空整理了下demo,方便以后使用以及后來(lái)人參考。

婺源ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!

一、springboot中集成shiro框架

關(guān)于shior框架的介紹可以參考這篇,需要引入相關(guān)jar如下:

????<!--shiro核心jar?from?官網(wǎng)?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過(guò)濾器的
Shiro的初始化工作在web.xml中設(shè)置監(jiān)聽(tīng)器完成

<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?就是一個(gè)典型的?ServletContextListener,它也是整個(gè)?Shiro?Web?應(yīng)用的入口?。

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


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

可以從 ServletContextEvent 中直接獲取 ServletContext 對(duì)象。

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

EnvironmentLoader的功能:

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

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

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

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

二、前后端分離中遇到的坑
  1. 服務(wù)端需開(kāi)啟跨域支持

  2. 只返回Json,不要重定向

  3. OPTIONS Request 不進(jìn)行鑒權(quán)操作

分享標(biāo)題:前后分離的shiro權(quán)限
網(wǎng)頁(yè)地址:http://bm7419.com/article26/jcedjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站維護(hù)、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站建設(shè)

廣告

聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)