Shiro 簡介
Apache Shiro 是一個(gè)開源的輕量級的 Java 安全框架,它提供身份驗(yàn)證、授權(quán)、密碼管理以及會(huì)話管理等功能。相對于 Spring Security ,Shiro 框架更加直觀、易用,同時(shí)也能提供健壯的安全性。
在傳統(tǒng)的 SSM 框架中,手動(dòng)整合 Shiro 的配置步驟還是比較多的,針對 Spring Boot ,Shiro 官方提供了 shiro-spring-boot-web-starter 用來簡化 Shiro 在 Spring Boot 中的配置。
整合 Shiro
1. 創(chuàng)建項(xiàng)目
首先創(chuàng)建一個(gè)普通的 Spring Boot Web 項(xiàng)目,添加 Shiro 依賴以及頁面模板依賴
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-web-starter</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.github.theborakompanioni</groupId>
<artifactId>thymeleaf-extras-shiro</artifactId>
<version>2.0.0</version>
</dependency>
這里不需要添加 spring-boot-starter-web 依賴,shiro-spring-boot-web-starter 中已經(jīng)依賴了 spring-boot-starter-web 。同時(shí),此處使用 Thymeleaf 模板,為了在 Thymeleaf 使用 shiro 標(biāo)簽,加入了 thymeleaf-extras-shiro 依賴。
2. Shiro基本配置
在 application.properties 中配置 Shiro 的基本信息
# 開啟 Shiro 配置,默認(rèn)為 true
shiro.enabled=true
# 開啟 Shiro Web 配置,默認(rèn)為 true
shiro.web.enabled=true
# 配置登錄地址,默認(rèn)為 /login.jsp
shiro.loginUrl=/login
# 配置登錄成功的地址,默認(rèn)為 /
shiro.successUrl=/index
# 未獲授權(quán)默認(rèn)跳轉(zhuǎn)地址
shiro.unauthorizedUrl=/unauthorized
# 是否允許通過 URL 參數(shù)實(shí)現(xiàn)會(huì)話跟蹤,如果網(wǎng)站支持 Cookie,可以關(guān)閉此選項(xiàng),默認(rèn)為 true
shiro.sessionManager.sessionIdUrlRewritingEnabled=true
# 是否允許通過 Cookie 實(shí)現(xiàn)會(huì)話跟蹤,默認(rèn)為 true
shiro.sessionManager.sessionIdCookieEnabled=true
然后在 Java 代碼中配置 Shiro ,提供兩個(gè)最基本的 Bean 即可
@Configuration
public class ShiroConfig {
@Bean
public Realm realm() {
TextConfigurationRealm realm = new TextConfigurationRealm();
realm.setUserDefinitions("sang=123,user\n admin=123,admin");
realm.setRoleDefinitions("admin=read,write\n user=read");
return realm;
}
@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chainDefinition =
new DefaultShiroFilterChainDefinition();
chainDefinition.addPathDefinition("/login", "anon");
chainDefinition.addPathDefinition("/doLogin", "anon");
chainDefinition.addPathDefinition("/logout", "logout");
chainDefinition.addPathDefinition("/**", "authc");
return chainDefinition;
}
@Bean
public ShiroDialect shiroDialect() {
return new ShiroDialect();
}
}
代碼解釋:
這里提供兩個(gè)關(guān)鍵的 Bean ,一個(gè)是 Realm,另一個(gè)是 ShiroFilterChainDefinition 。至于 ShiroDialect 則是為了支持在 Thymeleaf 中使用 Shiro 標(biāo)簽,如果不在 Thymeleaf 中使用 Shiro 標(biāo)簽,那么可以不提供 ShiroDialect
Realm 可以是自定義的 Realm,也可以是 Shiro 提供的 Realm,簡單起見,此處沒有配置數(shù)據(jù)庫連接,直接配置了兩個(gè)用戶:sang/123 和 admin/123 ,分別對應(yīng)角色 user 和 admin。
ShiroFilterChainDefinition Bean 中配置了基本的過濾規(guī)則 ,“/login” 和 “/doLogin”,可以匿名訪問,“/logout”是一個(gè)注銷登錄請求,其余請求則都需要認(rèn)證后才能訪問
然后配置登錄接口以及頁面訪問接口
@Controller
public class UserController {
@PostMapping("/doLogin")
public String doLogin(String username, String password, Model model) {
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
Subject subject = SecurityUtils.getSubject();
try {
subject.login(token);
} catch (AuthenticationException e) {
model.addAttribute("error", "用戶名或密碼輸入錯(cuò)誤!");
return "login";
}
return "redirect:/index";
}
@RequiresRoles("admin")
@GetMapping("/admin")
public String admin() {
return "admin";
}
@RequiresRoles(value = {"admin", "user"}, logical = Logical.OR)
@GetMapping("/user")
public String user() {
return "user";
}
}
代碼解釋:
在 doLogin 方法中,首先構(gòu)建一個(gè) UsernamePasswordToken 實(shí)例,然后獲取一個(gè) Subject 對象并調(diào)用該對象中的 login 方法執(zhí)行登錄操作,在登錄操作執(zhí)行過程中,當(dāng)有異常拋出時(shí),說明登錄失敗,攜帶錯(cuò)誤信息返回登錄視圖;當(dāng)?shù)卿洺晒r(shí),則重定向到“/index”
接下來暴露兩個(gè)接口“/admin”和“/user”,對于“/admin”接口,需要具有 admin 角色才可以訪問;對于“/user”接口,具備 admin 角色 和 user角色其中任意一個(gè)即可訪問
對于其他不需要角色就能訪問的接口,直接在 WebMvc 中配置即可
@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/index").setViewName("index");
registry.addViewController("/unauthorized").setViewName("unauthorized");
}
}
接下來創(chuàng)建全局異常處理器進(jìn)行全局異常處理,此處主要是處理授權(quán)異常
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(AuthorizationException.class)
public ModelAndView error(AuthorizationException e) {
ModelAndView mv = new ModelAndView("unauthorized");
mv.addObject("error", e.getMessage());
return mv;
}
}
當(dāng)用戶訪問未授權(quán)的資源時(shí),跳轉(zhuǎn)到 unauthorized 視圖中,并攜帶出錯(cuò)誤信息。
配置完成后,最后在 resources/templates 目錄下創(chuàng)建 5 個(gè) HTML 頁面進(jìn)行測試。
(1)index.html
<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h4>Hello, <shiro:principal/></h4>
<h4><a href="/logout" rel="external nofollow" >注銷登錄</a></h4>
<h4><a shiro:hasRole="admin" href="/admin" rel="external nofollow" >管理員頁面</a></h4>
<h4><a shiro:hasAnyRoles="admin,user" href="/user" rel="external nofollow" >普通用戶頁面</a></h4>
</body>
</html>
(2)login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<form action="/doLogin" method="post">
<input type="text" name="username"><br>
<input type="password" name="password"><br>
<div th:text="${error}"></div>
<input type="submit" value="登錄">
</form>
</div>
</body>
</html>
(3)user.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>普通用戶頁面</h2>
</body>
</html>
(4)admin.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>管理員頁面</h2>
</body>
</html>
(5)unauthorized.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<h4>未獲授權(quán),非法訪問</h4>
<h4 th:text="${error}"></h4>
</div>
</body>
</html>
3. 測試
啟動(dòng)項(xiàng)目,訪問登錄頁面,使用 sang/123 登錄

注意:由于 sang 用戶不具備 admin 角色,因此登錄成功后的頁面沒有前往管理員頁面的超鏈接。
然后使用 admin/123 登錄。

如果用戶使用 sang 登錄,然后去訪問:http://localhost:8080/admin,會(huì)跳轉(zhuǎn)到未授權(quán)頁面

以上是SpringBoot安全管理之Shiro框架怎么使用的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!