Spring Boot Shiro許可權管理1
原文地址:https://412887952-qq-com.iteye.com/blog/2299732
(1). Shiro簡單介紹
(2). 整合Shiro核心分析
(3). 無Shiro的Spring Boot
(4). 整合Shiro 進行使用者授權
(5). Shiro快取
(6). Shiro記住密碼
(7). Shiro驗證碼
-------------------------------------------------------------------
(1). Shiro簡單介紹
Shiro是Apache下的一個開源專案,我們稱之為Apache Shiro。它是一個很易用與Java專案的的安全框架,提供了認證、授權、加密、會話管理,與 Spring Security 一樣都是做一個許可權的安全框架,但是與Spring Security 相比,在於 Shiro 使用了比較簡單易懂易於使用的授權方式。
Apache Shiro 的三大核心元件
<!--[endif]-->
- Subject 當前使用者操作
- SecurityManager 用於管理所有的Subject
- Realms 用於進行許可權資訊的驗證,也是我們需要自己實現的。
我們需要實現Realms的Authentication 和 Authorization。其中 Authentication 是用來驗證使用者身份,Authorization 是授權訪問控制,用於對使用者進行的操作授權,證明該使用者是否允許進行當前操作,如訪問某個連結,某個資原始檔等。
Apache Shiro 核心通過 Filter 來實現,就好像SpringMvc 通過DispachServlet 來主控制一樣。
既然是使用 Filter 一般也就能猜到,是通過URL規則來進行過濾和許可權校驗,所以我們需要定義一系列關於URL的規則和訪問許可權。
另外我們可以通過Shiro 提供的會話管理來獲取Session中的資訊。Shiro 也提供了快取支援,使用CacheManager 來管理。
完整架構圖:
Shiro是很強大的一個安全框架,這裡只是拋裝引玉下,還有很多的需要大家自己去學習Shiro。
(2). 整合Shiro核心分析
整合Shiro的話,我們需要知道Shiro框架大概的一些管理物件。
第一:ShiroFilterFactory,Shiro過濾器工廠類,具體的實現類是:ShiroFilterFactoryBean,此實現類是依賴於SecurityManager安全管理器。
第二:SecurityManager,Shiro的安全管理,主要是身份認證的管理,快取管理,cookie管理,所以在實際開發中我們主要是和SecurityManager進行打交道的,ShiroFilterFactory主要配置好了Filter就可以了。當然SecurityManager並進行身份認證快取的實現,我們需要進行對應的編碼然後進行注入到安全管理器中。
第三:Realm,用於身份資訊許可權資訊的驗證。
第四:其它的就是快取管理,記住登入之類的,這些大部分都是需要自己進行簡單的實現,然後注入到SecurityManager讓Shiro的安全管理器進行管理就好了。
(3). 無Shiro的Spring Boot
我們先編寫一個無Shiro的簡單的框架,在這個框架中我們可以訪問到index,login,userInfo,userInfoAdd。
這個步驟對於有Spring Boot基礎的就應該很簡單了,在這裡簡單的介紹下:
(a) 新建一個maven java project,取名為spring-boot-shiro1
(b) 在pom.xml中引入基本依賴,在這裡還沒有引入shiro等的依賴:
<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.kfit</groupId>
<artifactId>spring-boot-shiro1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-shiro1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<!--
spring boot 父節點依賴,
引入這個之後相關的引入就不需要新增version配置,
spring boot會自動選擇最合適的版本進行新增。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<!-- spring boot web支援:mvc,aop... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thmleaf模板依賴. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
在這裡只引入了Spirng Boot的web依賴以及對thymleaf模板引擎的依賴。
(c) 編寫網頁檔案:
index.html,login.html,userInfo.html,userInfoAdd.html
這個檔案存在在src/main/resouces/templates, 這幾個檔案中都是簡單的程式碼,只有登入介面中有賬號和密碼:
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h3>index</h3>
</body>
</html>
login.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
錯誤資訊:<h4 th:text="${msg}"></h4>
<form action="" method="post">
<p>賬號:<input type="text" name="username" value="admin"/></p>
<p>密碼:<input type="text" name="password" value="123456"/></p>
<p><input type="submit" value="登入"/></p>
</form>
</body>
</html>
其它的頁面都是簡單的一個標籤而已:
<h3>使用者查詢介面</h3>
<h3>使用者新增介面</h3>
請自行編碼。
(d)編寫啟動類
編寫啟動類com.kfit.App.java:
package com.kfit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 啟動類.
* @author Angel(QQ:412887952)
* @version v.0.1
*/
@SpringBootApplication
publicclass App {
/**
* 引數裡VM引數設定為:
-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify
* @param args
*/
publicstaticvoid main(String[] args) {
SpringApplication.run(App.class, args);
}
}
這樣類似的程式碼我們已經介紹很多了,沒有什麼可以過多的介紹了,
這時候我們右鍵run as 執行App.java類訪問index,login頁面,會報Error Page,因為我們還沒編寫Controller處理類呢。
(e)編寫HomeController類
在com.kfit.root.controller新建HomeController類:
package com.kfit.root.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
publicclass HomeController {
@RequestMapping({"/","/index"})
public String index(){
return"/index";
}
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(){
return"login";
}
}
在這裡我們並沒有把UserInfo對應的處理也在頁面進行編碼了,因為之後我們建立了UserInfo之後,打算新建一個UserInfoController進行處理,所以這裡就沒有相應的userInfo的跳轉處理。
這時候我們在執行我們的程式就應該可以訪問index,login頁面了。
現在我們的程式還有問題,就是index頁面在沒有登入的時候,就可以進行訪問了,我們希望是如果直接訪問index頁面,如果沒有登入的話,直接跳轉到login進行登入。