Spring Boot整合jpa,Shiro進(jìn)行權(quán)限管理

轉(zhuǎn):https://blog.csdn.net/liuchuanhong1/article/details/76179601

本來(lái)想寫(xiě)一篇spring boot整合Shiro實(shí)現(xiàn)權(quán)限驗(yàn)證的文章,發(fā)現(xiàn)這篇寫(xiě)的非常不錯(cuò),就直接借鑒了!

(1). Shiro簡(jiǎn)單介紹

Shiro是Apache下的一個(gè)開(kāi)源項(xiàng)目,我們稱(chēng)之為Apache Shiro。它是一個(gè)很易用與Java項(xiàng)目的的安全框架,提供了認(rèn)證、授權(quán)、加密、會(huì)話管理,與spring Security 一樣都是做一個(gè)權(quán)限的安全框架,但是與Spring Security 相比,在于 Shiro 使用了比較簡(jiǎn)單易懂易于使用的授權(quán)方式。
Apache Shiro 的三大核心組件

image
  • Subject 當(dāng)前用戶(hù)操作
  • SecurityManager 用于管理所有的Subject
  • Realms 用于進(jìn)行權(quán)限信息的驗(yàn)證,也是我們需要自己實(shí)現(xiàn)的。
    我們需要實(shí)現(xiàn)Realms的Authentication 和 Authorization。其中 Authentication 是用來(lái)驗(yàn)證用戶(hù)身份,Authorization 是授權(quán)訪問(wèn)控制,用于對(duì)用戶(hù)進(jìn)行的操作授權(quán),證明該用戶(hù)是否允許進(jìn)行當(dāng)前操作,如訪問(wèn)某個(gè)鏈接,某個(gè)資源文件等。

Apache Shiro 核心通過(guò) Filter 來(lái)實(shí)現(xiàn),就好像SpringMvc 通過(guò)DispachServlet 來(lái)主控制一樣。
既然是使用 Filter 一般也就能猜到,是通過(guò)URL規(guī)則來(lái)進(jìn)行過(guò)濾和權(quán)限校驗(yàn),所以我們需要定義一系列關(guān)于URL的規(guī)則和訪問(wèn)權(quán)限。
另外我們可以通過(guò)Shiro 提供的會(huì)話管理來(lái)獲取Session中的信息。Shiro 也提供了緩存支持,使用 CacheManager 來(lái)管理。

官方網(wǎng)站:http://shiro.apache.org/

完整架構(gòu)圖:

image

Shiro是很強(qiáng)大的一個(gè)安全框架,這里只是拋裝引玉下,還有很多的需要大家自己去學(xué)習(xí)Shiro。

(2). 集成Shiro核心分析
集成Shiro的話,我們需要知道Shiro框架大概的一些管理對(duì)象。
第一:ShiroFilterFactory,Shiro過(guò)濾器工廠類(lèi),具體的實(shí)現(xiàn)類(lèi)是:ShiroFilterFactoryBean,此實(shí)現(xiàn)類(lèi)是依賴(lài)于SecurityManager安全管理器。
第二:SecurityManager,Shiro的安全管理,主要是身份認(rèn)證的管理,緩存管理,cookie管理,所以在實(shí)際開(kāi)發(fā)中我們主要是和SecurityManager進(jìn)行打交道的,ShiroFilterFactory主要配置好了Filter就可以了。當(dāng)然SecurityManager并進(jìn)行身份認(rèn)證緩存的實(shí)現(xiàn),我們需要進(jìn)行對(duì)應(yīng)的編碼然后進(jìn)行注入到安全管理器中。
第三:Realm,用于身份信息權(quán)限信息的驗(yàn)證。
第四:其它的就是緩存管理,記住登錄之類(lèi)的,這些大部分都是需要自己進(jìn)行簡(jiǎn)單的實(shí)現(xiàn),然后注入到SecurityManager讓Shiro的安全管理器進(jìn)行管理就好了。

(3). 無(wú)Shiro的Spring Boot
我們先編寫(xiě)一個(gè)無(wú)Shiro的簡(jiǎn)單的框架,在這個(gè)框架中我們可以訪問(wèn)到index,login,userInfo,userInfoAdd。
這個(gè)步驟對(duì)于有Spring Boot基礎(chǔ)的就應(yīng)該很簡(jiǎn)單了,在這里簡(jiǎn)單的介紹下:
(a) 新建一個(gè)maven Java project,取名為spring-boot-shiro1
(b) 在pom.xml中引入基本依賴(lài),在這里還沒(méi)有引入shiro等的依賴(lài):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
    <groupId>com.example</groupId>  
    <artifactId>spring-boot-shiro1</artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
    <packaging>jar</packaging>  
  
  
    <properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
    </properties>  
  
  
    <!-- Inherit defaults from Spring Boot -->  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.4.0.RELEASE</version>  
    </parent>  
  
  
    <dependencies>  
        <!-- spring boot web支持:mvc,aop... -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <!-- thmleaf模板依賴(lài). -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-thymeleaf</artifactId>  
        </dependency>  
         
       <!-- 熱部署 -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-devtools</artifactId>  
            <optional>true</optional>  
        </dependency>  
  
    </dependencies>  
</project>

(c) 編寫(xiě)網(wǎng)頁(yè)文件:
index.html,login.html,userInfo.html,userInfoAdd.html
這個(gè)文件存在在src/main/resouces/templates, 這幾個(gè)文件中都是簡(jiǎn)單的代碼,只有登錄界面中有賬號(hào)和密碼:

index.html

1.  <!DOCTYPE html>  
2.  <html>  
3.  <head>  
4.  <meta charset="UTF-8" />  
5.  <title>Insert title here</title>  
6.  </head>  
7.  <body>  
8.  <[h3](https://www.baidu.com/s?wd=h3&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd)>index</h3>  
9.  </body>  
10.  </html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容