springboot-shiro:搭建測試環境
阿新 • • 發佈:2022-03-08
1 建立一個springboot專案
建立過程中新增web模組
2 匯入thymeleaf依賴
pom.xml
<!--thymeleaf模板引擎--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
3 編寫主頁
src/main/resources/templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>首頁</h1>
<p th:text="${msg}"></p>
</body>
</html>
4 建立一個controller包,在該包下編寫一個MyController
src/main/java/com/lv/controller/MyController.java
package com.lv.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { @RequestMapping({"/","/index"}) public String toIndex(Model model){ model.addAttribute("msg","hello Shiro"); return "index"; } }
5 啟動專案,訪問首頁
首頁訪問成功後,就可以進行接下來的操作
6 匯入shiro和spring整合的依賴
<!--shiro整合spring的包-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.8.0</version>
</dependency>
7 建立一個config包,在該包下編寫一個自定義的realm的類
在這個realm類中可以編寫一些查詢的方法,或者認證與授權的邏輯
src/main/java/com/lv/config/UserRealm.java
package com.lv.config;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
//自定義的UserRealm extends AuthorizingRealm
public class UserRealm extends AuthorizingRealm {
//授權
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("執行了=>授權doGetAuthorizationInfo");
return null;
}
//認證
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("執行了=>認證doGetAuthorizationInfo");
return null;
}
}
8 在config包下編寫一個shiro配置類
需要在這個配置類中配置三個核心API:
- Subject 使用者
- SecurityManger 管理所有使用者
- Realm 連線資料
src/main/java/com/lv/config/ShiroConfig.java
package com.lv.config;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//宣告為配置類
@Configuration
public class ShiroConfig {
//ShiroFilterFactoryBean : 第三步
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
//設定安全管理器
bean.setSecurityManager(defaultWebSecurityManager);
return bean;
}
//DefaultWebSecurityManager : 第二步
@Bean(name = "securityManager")
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//關聯userRealm
securityManager.setRealm(userRealm);
return securityManager;
}
//建立realm物件,需要自定義類 : 第一步
@Bean
public UserRealm userRealm(){
return new UserRealm();
}
}
9 建立兩個頁面
在templates下新建一個user目錄 並在該目錄下編寫add.html和update.html
src/main/resources/templates/user/add.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>add</h1>
</body>
</html>
src/main/resources/templates/user/update.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>update</h1>
</body>
</html>
10 在controller中新增跳轉頁面的方法
src/main/java/com/lv/controller/MyController.java
@RequestMapping("/user/add")
public String add(){
return "user/add";
}
@RequestMapping("/user/update")
public String update(){
return "user/update";
}
11 在首頁,增加跳轉連結
src/main/resources/templates/index.html
<body>
<h1>首頁</h1>
<p th:text="${msg}"></p>
<hr>
<a th:href="@{/user/add}">add</a> | <a th:href="@{/user/update}">update</a>
</body>
12 重啟專案,測試
訪問首頁
點選add連結
點選update連結
頁面跳轉成功