1、Shiro簡介
1.1、什么是shiro
- Apache Shiro 是 Java 的一個安全(權限)框架,
-
Shiro 可以非常容易的開發出足夠好的應用,其不僅可以用在 JavaSE 環境,也可以用在 JavaEE 環境,
-
Shiro 可以完成:認證、授權、加密、會話管理、與Web 集成、快取等,
-
下載地址
- 官網:http://shiro.apache.org/
- github:https://github.com/apache/shiro
1.2、shiro有哪些功能?

如圖所示,shiro的大部分功能在此圖上,接下來我幫助大家解讀這些功能
1、Authentication:身份認證,用于驗證判斷用戶是否擁有權限 (對應身份)
2、Authorization:身份授權,對于指定身份擁有什么權限進行給予(如老師給了同學管理班級的能力)
3、Session Management:會話管理,用戶在進行登錄操作時便開啟了一次會話;
4、Cryptography:資料加密,用于保護資料的安全性,將密碼加密存盤更安全,常用加密方式有md5、des等
5、Web Suppot:支持web,方便集成到Web環境中
6、Caching:快取,比如用戶登錄后,其用戶資訊、擁有的角色/權限不必每次去查,這樣可以提高效率
7、Concurrency:支持多執行緒應用并發驗證,如一個執行緒中又開啟了一個執行緒,能將權限自動傳遞過去
8、Testing:提供測驗支持
9、Run As:允許用戶偽裝成另一個用戶的身份進行方法(需取得允許)
10、RememberMe:記住我,相當于選中后下次便不需要輸入賬戶密碼了
以上就是shiro的大體功能了!
現在我們來決議shiro的架構
1.3、Shiro架構(外部)
我們首先從外部來觀查Shiro是如何執行的

啟動應用程式后首先獲取當前的物件然后根據安全管理器ShiroSecurityManager從Realm中獲得的安全資料(如用戶、權限)來進行驗證
- Subject:程式直接互動的物件便是Subject,可以理解Subject就是shiro對外API核心,Subject代表當前用戶(這個用戶可以不是一個人,他是程式直接互動的物件,它可以是網路爬蟲,也可以是機器人!),與Subject的所有互動都會由SecurityManager來處理,SecurityManager才是實際執行者
- SecurityManager:安全管理器,所有與安全有關的操作都會與其互動,它管理著所有的Subject,它是Shiro的核心,負責與Shiro的其他組件互動的職責
- Realm:Shiro從Realm中獲取安全資料(如用戶資訊、權限等),如果SecurityManager要驗證用戶身份,那么他需要從Realm中獲取相應的用戶來進行比較來確認用戶身份是否合法,也需要從Realm中得到用戶相應的角色/權限進行驗證用戶是否能執行該操作
1.4、Shiro架構(內部)

- Subject:任何可以與應用互動的用戶
- Security Manager:所有的互動都通過其進行控制,它控制所有Subject,且負責認證、授權、會話以及快取的管理
- Cryptography:資料加密模塊,Shiro提供了一些常見的加密組件用于加密操作
其余具體可參考1.2
2、與SpirngBoot集成
2.1、 Springboot環境搭建
1、新建一個專案或模塊,勾選依賴

2、創建完專案修改pom檔案添加依賴
<dependencies>
<!--匯入springWeb-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--匯入thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--spring單元測驗-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
專案結構如下、

3、創建controller來測驗環境是否正常
3.1、創建一個控制器
/**
* @author: 霖磬
* @remark:
* @createTime: 2021-03-04 20:21
* @version:
**/
@Controller
public class MyController {
@RequestMapping({"/","/index"})
public String toIndex(Model model){
model.addAttribute("msg","hello,shiro!");
return "index";
}
@RequestMapping("/user/add")
public String add() {
return "user/add";
}
@RequestMapping("/user/update")
public String update() {
return "user/update";
}
}
3.2、創建一個index.html頁面
<!DOCTYPE html>
<!--匯入命名空間 thymeleaf 不匯入也沒關系 只是會爆紅但可以正常使用-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首頁</title>
</head>
<body>
<h1>首頁</h1>
<p th:text="${msg}"></p>
<hr>
<a th:href="@{/user/add}">add</a> |
<a th:href="@{/user/update}">update</a>
</body>
</html>
3.3、新建一個user包 包下創建add.html和update.html用于權限控制
3.3.1、創建add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>add</h1>
</body>
</html>
3.3.2、創建update.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Update</h1>
</body>
</html>
4、運行測驗

現在可以通過這兩個超鏈接進行頁面跳轉,那我們要怎樣才能使其擁有限制呢?
2.2、Shiro實作登錄攔截
1、匯入shiro整合Spring的包
<!--shiro整合spring的包-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.5.3</version>
</dependency>
2、創建一個ShiroConfig類
/**
* @author: 霖磬
* @remark:
* @createTime: 2021-03-04 21:30
* @version:
**/
@Configuration//申明這是個配置類
public class ShiroConfig {
//創建ShiroFilterFactoryBean :3
@Bean//通過Qualifier注解去指定我們安全管理器
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager safetyManager){
//ShiroFilterFactoryBean物件 它是shiro和spring整合的關鍵
ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
//通過bean去設定安全管理器
bean.setSecurityManager(safetyManager);
return bean;
}
//DefaultWebSecurityManager 該類對應SecurityManager(安全管理器) :2
@Bean//通過Qualifier注解去指定我們realm物件
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//關聯UserRealm物件 因為我們需要從realm物件中獲取安全資料
securityManager.setRealm(userRealm());
return securityManager;
}
//創建realm物件 :1
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
}
3、創建UserRealm類
/**
* @author: 霖磬
* @remark: 自定義的Realm
* @createTime: 2021-03-04 21:33
* @version:
**/
public class UserRealm extends AuthorizingRealm {
@Override//授權
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("執行了授權");
return null;
}
@Override//認證
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("執行了認證");
return null;
}
}
4、實作登錄攔截,在ShiroConfig類中getShiroFilterFactoryBean方法添加代碼
//創建ShiroFilterFactoryBean :3
@Bean//通過Qualifier注解去指定我們安全管理器
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager safetyManager){
//ShiroFilterFactoryBean物件 它是shiro和spring整合的關鍵
ShiroFilterFactoryBean bean=new ShiroFilterFactoryBean();
//通過bean去設定安全管理器
bean.setSecurityManager(safetyManager);
/* shiro中內置的過濾器
- anon: 無需認證就可以訪問
- authc: 必須認證了才能訪問
- user: 必須擁有記住我功能才能用
- perms: 擁有對某個資源的權限才能訪問
- role: 擁有某個角色權限
*/
Map<String, String> filterMap = new LinkedHashMap<>();
//訪問/user/add 是必須認證
filterMap.put("/user/add","authc");
filterMap.put("/user/update","authc");
//將過濾器添加到bean中.如果權限不足會自動訪問login.jsp頁面
bean.setFilterChainDefinitionMap(filterMap);
//我們需要設定權限不足時會打開的頁面
bean.setLoginUrl("/toLogin");
return bean;
}
5、在控制層MyController中添加兩個方法
@RequestMapping("/toLogin")
public String toLogin() {
return "login";
}
@RequestMapping("/login")
public String login(String username, String password, Model model) {
//獲取當前用戶
Subject subject = SecurityUtils.getSubject();
// 封裝用戶的登錄資料
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
try {
subject.login(token);//執行登錄的方法,如果沒有例外就說明ok了
return "index";
} catch (UnknownAccountException e) {//用戶名錯誤 回傳登錄界面
model.addAttribute("msg","用戶名錯誤");
return "login";
} catch (IncorrectCredentialsException e) {//密碼錯誤 回傳登錄界面
model.addAttribute("msg","密碼錯誤");
return "login";
}
}
6、在UserRealm類中認證添加虛擬的賬號密碼來進行驗證
@Override//認證
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("執行了認證");
// 用戶名、密碼, 資料中取 此資料虛擬(可以從資料庫中取)
String name = "root";
String password = "123456";
//強轉令牌 獲取到從前端封裝過來的賬號和密碼
UsernamePasswordToken userToken= (UsernamePasswordToken) token;
//如果用戶名不正確則拋出UnknownAccountException例外 告訴用戶改用戶名不存在
if (!userToken.getUsername().equals(name)) {
return null;//拋出例外 UnknownAccountException
}
//密碼驗證,因為密碼事關程式的安全,我們需要交給shiro中SimpleAuthenticationInfo類來做
return new SimpleAuthenticationInfo("",password,"");
}
7、創建login.html頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登錄頁面</title>
</head>
<body>
<h1>登錄</h1>
<hr>
<p th:text="${msg}" style="color: red;"></p>
<form th:action="@{/login}">
<p>用戶名:<input type="text" name="username"></p>
<p>密碼:<input type="text" name="password"></p>
<p>密碼:<input type="submit"></p>
</form>
</body>
</html>
8、簡單測驗

當我們輸入錯誤資訊后則會顯示用戶米錯誤如果密碼錯誤則會顯示密碼錯誤
如果兩者正確則可以進入到我們的首頁!
2.3、結合mybatis實作
2.3.1、在pom中匯入依賴
<!--匯入lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--匯入mysql的驅動包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--匯入druid資料源(德魯伊)-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--引入mybatis,這是MyBatis官方提供的適配spring Boot的,而不是spring Boot自己的-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
2.3.2、在resources中創建組態檔application.yml
注意:使用yml組態檔每次:號后需要多出一個空格 必須嚴格要求!!1
spring:
datasource:
username: root
password: admin
#?serverTimezone=UTC解決時區的報錯
url: jdbc:mysql://localhost:3306/lq?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默認是不注入這些屬性值的,需要自己系結
#druid 資料源專有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置監控統計攔截的filters,stat:監控統計、log4j:日志記錄、wall:防御sql注入
#如果允許時報錯 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#則匯入 log4j 依賴即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#配置mybatis
mybatis:
#配置別名設定
type-aliases-package: com.lin.pojo
#指定mapper的路徑
mapper-locations: classpath:mapper/*.xml
2.3.4、創建用戶User物件
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String psd;
private String perms;//權限
}
2.3.5、創建mapper并配置xml檔案以及service與實作類
2.3.5.1、創建UserMapper介面
@Repository//將該類交給Spring接管
@Mapper//告訴程式這是個mapper類
public interface UserMapper {
/**
* 通過用戶名查詢用戶
* @param name
* */
User queryUserByName(String name);
}
2.3.5.2、創建UserMapper.xml檔案(在resources創建mapper目錄)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace=系結一個對應的Dao/Mapper介面-->
<mapper namespace="com.lin.mapper.UserMapper">
<select id="queryUserList" resultType="User" parameterType="String">
select * from lq.user where name=#{name};
</select>
</mapper>
2.3.5.3、創建UserService介面
/**
* @author: 霖磬
* @remark:
* @createTime: 2021-03-04 23:43
* @version:
**/
public interface UserService {
/**
* 通過用戶名查詢用戶
* @param name
* */
User queryUserByName(String name);
}
2.3.5.4、創建UserService實作類
/**
* @author: 霖磬
* @remark:
* @createTime: 2021-03-04 23:44
* @version:
**/
@Service//交給spring托管
public class UserServiceImpl implements UserService {
@Autowired//自動注入UserMapper
UserMapper userMapper;
@Override
public User queryUserByName(String name) {
return userMapper.queryUserByName(name);
}
}
2.4、測驗環境

成功查出!
2.5、UserRealm`連接真實資料庫
@Override//認證
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
System.out.println("執行了認證");
//強轉令牌 獲取到從前端封裝過來的賬號和密碼
UsernamePasswordToken userToken= (UsernamePasswordToken) token;
//通過傳遞過來的用戶名從資料庫查詢出對應user
User user = userService.queryUserByName(userToken.getUsername());
if (user == null) {//沒有這個人
return null;
}
//密碼驗證,因為密碼事關程式的安全,我們需要交給shiro中SimpleAuthenticationInfo類來做 注意,寫在這里我們可以通過subject去取到對應的資料
return new SimpleAuthenticationInfo(user,user.getName(),"");
}
2.6、實作授權功能
2.6.1、在controller添加一個方法,來告訴用戶當前狀況
@RequestMapping("/noauto")
@ResponseBody//回傳一個字串
public String unauthorized() {
return "未經授權,無法訪問此頁面";
}
2.6.2、在ShiroConfig類getShiroFilterFactoryBean方法中添加
//設定無權限進入頁面時的自動跳轉
bean.setUnauthorizedUrl("/noauth");
2.6.3、在UserRealm進行授權處理
@Override//授權
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("執行了授權");
//創建給予授權方法
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//獲取當前物件
Subject subject=SecurityUtils.getSubject();
//拿到User物件
User user = (User) subject.getPrincipal();
//設定當前用戶權限
info.addStringPermission(user.getPerms());
//將權力回傳
return info;
}
ShiroConfig中的getShiroFilterFactoryBean方法添加認證代碼
//授權,正常情況下,沒有授權會跳轉到為授權頁面
filterMap.put("/user/add","perms[user:add]");
filterMap.put("/user/update","perms[user:update]");
2.6.4、測驗!
專案目錄結構

感謝各位看官姥爺,初來乍到,還請多多包容!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/266977.html
標籤:其他
