SpringBoot淺析安全管理之Shiro框架
Shiro 簡介
Apache Shiro 是一個開源的輕量級的 Java 安全框架,它提供身份驗證、授權、密碼管理以及會話管理等功能。相對于 Spring Security ,Shiro 框架更加直觀、易用,同時也能提供健壯的安全性。
在傳統(tǒng)的 SSM 框架中,手動整合 Shiro 的配置步驟還是比較多的,針對 Spring Boot ,Shiro 官方提供了 shiro-spring-boot-web-starter 用來簡化 Shiro 在 Spring Boot 中的配置。
整合 Shiro
1. 創(chuàng)建項目
首先創(chuàng)建一個普通的 Spring Boot Web 項目,添加 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 。同時,此處使用 Thymeleaf 模板,為了在 Thymeleaf 使用 shiro 標簽,加入了 thymeleaf-extras-shiro 依賴。
2. Shiro基本配置
在 application.properties 中配置 Shiro 的基本信息
# 開啟 Shiro 配置,默認為 true
shiro.enabled=true
# 開啟 Shiro Web 配置,默認為 true
shiro.web.enabled=true
# 配置登錄地址,默認為 /login.jsp
shiro.loginUrl=/login
# 配置登錄成功的地址,默認為 /
shiro.successUrl=/index
# 未獲授權默認跳轉(zhuǎn)地址
shiro.unauthorizedUrl=/unauthorized
# 是否允許通過 URL 參數(shù)實現(xiàn)會話跟蹤,如果網(wǎng)站支持 Cookie,可以關閉此選項,默認為 true
shiro.sessionManager.sessionIdUrlRewritingEnabled=true
# 是否允許通過 Cookie 實現(xiàn)會話跟蹤,默認為 true
shiro.sessionManager.sessionIdCookieEnabled=true
然后在 Java 代碼中配置 Shiro ,提供兩個最基本的 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(); } }
代碼解釋:
- 這里提供兩個關鍵的 Bean ,一個是 Realm,另一個是 ShiroFilterChainDefinition 。至于 ShiroDialect 則是為了支持在 Thymeleaf 中使用 Shiro 標簽,如果不在 Thymeleaf 中使用 Shiro 標簽,那么可以不提供 ShiroDialect
- Realm 可以是自定義的 Realm,也可以是 Shiro 提供的 Realm,簡單起見,此處沒有配置數(shù)據(jù)庫連接,直接配置了兩個用戶:sang/123 和 admin/123 ,分別對應角色 user 和 admin。
- ShiroFilterChainDefinition Bean 中配置了基本的過濾規(guī)則 ,“/login” 和 “/doLogin”,可以匿名訪問,“/logout”是一個注銷登錄請求,其余請求則都需要認證后才能訪問
然后配置登錄接口以及頁面訪問接口
@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", "用戶名或密碼輸入錯誤!"); 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 方法中,首先構建一個 UsernamePasswordToken 實例,然后獲取一個 Subject 對象并調(diào)用該對象中的 login 方法執(zhí)行登錄操作,在登錄操作執(zhí)行過程中,當有異常拋出時,說明登錄失敗,攜帶錯誤信息返回登錄視圖;當?shù)卿洺晒r,則重定向到“/index”
- 接下來暴露兩個接口“/admin”和“/user”,對于“/admin”接口,需要具有 admin 角色才可以訪問;對于“/user”接口,具備 admin 角色 和 user角色其中任意一個即可訪問
對于其他不需要角色就能訪問的接口,直接在 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)建全局異常處理器進行全局異常處理,此處主要是處理授權異常
@ControllerAdvice public class ExceptionController { @ExceptionHandler(AuthorizationException.class) public ModelAndView error(AuthorizationException e) { ModelAndView mv = new ModelAndView("unauthorized"); mv.addObject("error", e.getMessage()); return mv; } }
當用戶訪問未授權的資源時,跳轉(zhuǎn)到 unauthorized 視圖中,并攜帶出錯誤信息。
配置完成后,最后在 resources/templates 目錄下創(chuàng)建 5 個 HTML 頁面進行測試。
(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> <h3>Hello, <shiro:principal/></h3> <h3><a href="/logout" rel="external nofollow" >注銷登錄</a></h3> <h3><a shiro:hasRole="admin" href="/admin" rel="external nofollow" >管理員頁面</a></h3> <h3><a shiro:hasAnyRoles="admin,user" href="/user" rel="external nofollow" >普通用戶頁面</a></h3> </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> <h1>普通用戶頁面</h1> </body> </html>
(4)admin.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>管理員頁面</h1> </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> <h3>未獲授權,非法訪問</h3> <h3 th:text="${error}"></h3> </div> </body> </html>
3. 測試
啟動項目,訪問登錄頁面,使用 sang/123 登錄
注意:由于 sang 用戶不具備 admin 角色,因此登錄成功后的頁面沒有前往管理員頁面的超鏈接。
然后使用 admin/123 登錄。
如果用戶使用 sang 登錄,然后去訪問:http://localhost:8080/admin,會跳轉(zhuǎn)到未授權頁面
以上通過一個簡單的案例展示了如何在 Spring Boot 中整合 Shiro 以及如何在 Thymeleaf 中使用 Shiro 標簽,一旦整合成功,接下來 Shiro 的用法就和原來的一模一樣。此處主要將 Spring Boot 整合 Shiro,對于 Shiro 的其它用法,可以參考 Shiro 官方文檔。
到此這篇關于SpringBoot淺析安全管理之Shiro框架的文章就介紹到這了,更多相關SpringBoot Shiro框架內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring boot2.0實現(xiàn)優(yōu)雅停機的方法
這篇文章主要介紹了spring boot2.0實現(xiàn)優(yōu)雅停機的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05SpringBoot集成MyBatis的分頁插件PageHelper實例代碼
這篇文章主要介紹了SpringBoot集成MyBatis的分頁插件PageHelper的相關操作,需要的朋友可以參考下2017-08-08java的Array,List和byte[],String相互轉(zhuǎn)換的方法你了解嘛
這篇文章主要為大家詳細介紹了java的Array,List和byte[],String相互轉(zhuǎn)換的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02SpringBoot Actuator潛在的OOM問題的解決
本文主要介紹了SpringBoot Actuator潛在的OOM問題的解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11