一、Shiro的簡單介紹
Shiro是Apache下的一個開源項目,我們稱之謂Apache Shiro,它是一個易用與Java項目的安全框架,提供了認證、授權、加密、會話管理,與Spring Security一樣都是做一個權限的安全框架,但是與Spring Security
相比,Shiro使用了更簡單易懂易於使用的授權方式。
Shiro的三大核心組件:
1、Subject------------------當前用戶
2、SecurityManage--------管理所有的Subject
3、Reamls------------------權限信息的驗證
我們需要實現Reamls的Authentication與Authorization,其中Authentication用於驗證用戶身份,Authorization用於授權訪問控制。
Shiro核心是通過Filter來實現,就好像SpringMVC用DispatchServlet來做控制一樣。既然使用Filter,那么我們也可以猜到,Shiro是通過URL規則來進行過濾和權限校驗,所以我們需要定義一系列的URL規則和訪問權限。
另外通過Shiro提供的會話管理可以獲取Session中的信息,Shiro也提供緩存功能,使用CacheManage來管理。
二、SpringBoot集成Shiro核心分析
集成Shiro我們需要知道Shiro框架大概的一些管理對象。
1、ShiroFilterFactory:Shiro過濾工廠類,具體的實現類是ShiroFilterFactoryBean,該實現類依賴與SecurityManage安全管理器。
2、SecurityManage:Shiro的安全管理,主要是安全認證管理、緩存管理、cookie管理。所以在實際的開發中我們主要和SecurityManage打交道,ShiroFilterFactory主要配置好了Filter就可以了。
3、Reamls:身份信息、權限信息的驗證。
4、緩存管理、記住密碼等功能,這些大部分只要進行簡單的實現,然后注入到SecurityManage讓Shiro的安全管理器進行管理就好了。
三、搭建無Shiro的Springboot項目
我們先編寫一個無Shiro的簡單的框架,在這個框架中我們可以訪問到index,login,userInfo,userInfoAdd。這個步驟對於有Spring Boot基礎的就應該很簡單了,在這里簡單的介紹下:
1、新建一個maven java project,取名為spring-boot-shiro
2、在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>
3、編寫網頁文件:index.html,login.html,userInfo.html,userInfoAdd.html
這些文件存在在src/main/resouces/templates, 這幾個文件中都是簡單的代碼,只有登錄界面中有賬號和密碼:
inde.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="checkbox" name="rememberMe" />記住我</P> <p><input type="submit" value="登錄"/></p> </form> </body> </html>
其他頁面都簡單的一個標簽而已,可自行編寫。<h3>用戶查詢界面</h3>、<h3>用戶添加界面</h3>
4、Controller控制類
@Controller public class HomeController { @RequestMapping({"/","/index"}) public String index(){ return "index"; } @RequestMapping(value="/login",method=RequestMethod.GET) public String login(){ return "login"; } }
5、編寫啟動類
@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
這時候我們在運行我們的程序就應該可以訪問index,login頁面了。
到此這個小節就結束了,現在我們的程序還有問題,就是index頁面在沒有登錄的時候,就可以進行訪問了,我們希望是如果直接訪問index頁面,如果沒有登錄的話,直接跳轉到login進行登錄。
那么下一小節我們將會介紹如何在當前代碼中集成shiro。
