springboot中如何去整合shrio實例分享
背景:
上文學習了shrio 基本概念后,本章將進一步的落地實踐學習,在springboot中如何去整合shrio,整個過程步驟有個清晰的了解。
利用Shiro進行登錄認證主要步驟:
1. 添加依賴:首先,在pom.xml文件中添加Spring Boot和Shiro的相關依賴。
<!-- Spring Boot --> <dependency> ????<groupId>org.springframework.boot</groupId> ????<artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Shiro --> <dependency> ????<groupId>org.apache.shiro</groupId> ????<artifactId>shiro-spring-boot-starter</artifactId> ????<version>1.7.1</version> </dependency> |
2. 創(chuàng)建Shiro配置類:創(chuàng)建一個ShiroConfig類,用于配置Shiro的相關信息和組件。(對于配置的解釋和作用見第三章雜談)
@Configuration
public?class?ShiroConfig {
????// 配置安全管理器
????@Bean
????public?DefaultWebSecurityManager securityManager() {
????????DefaultWebSecurityManager securityManager =?new?DefaultWebSecurityManager();
????????securityManager.setRealm(myRealm());
????????return?securityManager;
????}
????// 配置自定義Realm
????@Bean
????public?MyRealm myRealm() {
????????return?new?MyRealm();
????}
????// 配置Shiro過濾器
????@Bean
????public?ShiroFilterFactoryBean shiroFilterFactoryBean() {
????????ShiroFilterFactoryBean shiroFilterFactoryBean =?new?ShiroFilterFactoryBean();
????????shiroFilterFactoryBean.setSecurityManager(securityManager());
????????shiroFilterFactoryBean.setLoginUrl("/login");?// 設置登錄頁面
????????shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");?// 設置未授權頁面
????????Map<String, String> filterChainDefinitionMap =?new?LinkedHashMap<>();
????????// 允許匿名訪問的路徑
????????filterChainDefinitionMap.put("/login",?"anon");
????????filterChainDefinitionMap.put("/static/**",?"anon");
????????// 需要認證才能訪問的路徑
????????filterChainDefinitionMap.put("/**",?"authc");
????????shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
????????return?shiroFilterFactoryBean;
????}
} 3. 創(chuàng)建自定義Realm:創(chuàng)建一個MyRealm類,繼承AuthorizingRealm并實現(xiàn)相關方法,用于處理認證和授權邏輯
public?class?MyRealm?extends?AuthorizingRealm {
????// 處理認證邏輯
????@Override
????protected?AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)?throws?AuthenticationException {
????????// 從token中獲取用戶名
????????String username = (String) authenticationToken.getPrincipal();
????????// 模擬從數(shù)據(jù)庫或其他存儲中獲取用戶信息
????????// 例如,從數(shù)據(jù)庫中查詢用戶信息并返回
????????String dbPassword =?"123456";?// 假設從數(shù)據(jù)庫中查詢的密碼是123456
????????// 返回認證信息,包括用戶名和密碼
????????return?new?SimpleAuthenticationInfo(username, dbPassword, getName());
????}
????// 處理授權邏輯
????@Override
????protected?AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
????????// 從PrincipalCollection中獲取用戶名
????????String username = (String) principalCollection.getPrimaryPrincipal();
????????// 模擬從數(shù)據(jù)庫或其他存儲中獲取用戶角色和權限信息
????????// 例如,從數(shù)據(jù)庫中查詢用戶對應的角色和權限并返回
????????Set<String> roles =?new?HashSet<>();
????????roles.add("admin");?// 假設用戶擁有admin角色
????????Set<String> permissions =?new?HashSet<>();
????????permissions.add("user:read");?// 假設用戶擁有user:read權限
????????// 創(chuàng)建授權信息
????????SimpleAuthorizationInfo authorizationInfo =?new?SimpleAuthorizationInfo();
????????authorizationInfo.setRoles(roles);
????????authorizationInfo.setStringPermissions(permissions);
????????return?authorizationInfo;
????}
}4. 創(chuàng)建登錄接口和登錄頁面:創(chuàng)建一個登錄接口處理用戶的登錄請求
@Controller
public?class?LoginController {
????@GetMapping("/login")
????public?String login() {
????????return?"login";
????}
????@PostMapping("/login")
????public?String doLogin(String username, String password) {
????????// 執(zhí)行登錄邏輯
????????Subject currentUser = SecurityUtils.getSubject();
????????UsernamePasswordToken token = new?UsernamePasswordToken(username, password);
????????try?{
????????????currentUser.login(token); // 執(zhí)行登錄
????????????return?"redirect:/home"; // 登錄成功后跳轉(zhuǎn)到首頁
????????} catch?(AuthenticationException e) {
????????????return?"redirect:/login-error"; // 登錄失敗后跳轉(zhuǎn)到錯誤頁面
????????}
????}
}整體的執(zhí)行流程:
用戶在瀏覽器中訪問登錄頁面,輸入用戶名和密碼,并點擊登錄按鈕。
Controller層的
LoginController類中的doLogin方法被調(diào)用,該方法接收用戶名和密碼作為參數(shù)。創(chuàng)建一個
Subject對象,該對象代表當前正在與應用程序交互的用戶。創(chuàng)建一個
UsernamePasswordToken對象,將用戶名和密碼設置為該對象的屬性。調(diào)用
Subject對象的login方法,將UsernamePasswordToken對象作為參數(shù)傳遞進去。Subject對象將UsernamePasswordToken對象傳遞給Shiro進行認證。Shiro框架會調(diào)用
MyRealm類中的doGetAuthenticationInfo方法,該方法用于處理認證邏輯。在
doGetAuthenticationInfo方法中,從UsernamePasswordToken對象中獲取用戶名。可以根據(jù)需要,從數(shù)據(jù)庫或其他存儲中獲取與用戶名對應的用戶信息,例如密碼等。
將獲取到的用戶信息與
UsernamePasswordToken對象中的密碼進行比較,判斷用戶是否通過認證。如果認證成功,創(chuàng)建一個
SimpleAuthenticationInfo對象,將用戶名、數(shù)據(jù)庫中的密碼和Realm名稱作為參數(shù)傳遞給它。SimpleAuthenticationInfo對象會被返回給Shiro框架,表示認證成功。Shiro框架會將認證成功的信息保存在
Subject對象中。如果認證失敗,將拋出
AuthenticationException異常。在
doLogin方法中,通過捕獲AuthenticationException異常,可以處理登錄失敗的情況,例如重定向到登錄失敗頁面。如果登錄成功,可以根據(jù)需要執(zhí)行一些操作,例如重定向到首頁或其他需要登錄后才能訪問的頁面。
總結(jié)起來,整個執(zhí)行流程如下:
- 用戶輸入用戶名和密碼,并提交登錄表單。
- Controller層的
LoginController類中的doLogin方法接收到登錄請求。 - 創(chuàng)建
Subject對象,代表當前用戶。 - 創(chuàng)建
UsernamePasswordToken對象,將用戶名和密碼設置為其屬性。 - 調(diào)用
Subject對象的login方法,將UsernamePasswordToken對象作為參數(shù)傳入。 - Shiro框架調(diào)用
MyRealm類中的doGetAuthenticationInfo方法,處理認證邏輯。 - 在
doGetAuthenticationInfo方法中,獲取用戶名和密碼,并與數(shù)據(jù)庫中的信息進行比較。 - 如果認證成功,返回一個
SimpleAuthenticationInfo對象,表示認證通過。 - 如果認證失敗,拋出
AuthenticationException異常。 - 在
doLogin方法中,根據(jù)認證結(jié)果執(zhí)行相應的操作,例如重定向到登錄成功頁面或登錄失敗頁面。
到此這篇關于springboot中如何去整合shrio實例分享的文章就介紹到這了,更多相關springboot整合shrio內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
kafka運維consumer-groups.sh消費者組管理
這篇文章主要為大家介紹了kafka運維consumer-groups.sh消費者組管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
MyBatis?Generator生成數(shù)據(jù)庫模型實現(xiàn)示例
這篇文章主要為大家介紹了MyBatis?Generator生成數(shù)據(jù)庫模型實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Security6.4.2?自定義異常中統(tǒng)一響應遇到的問題
本文主要介紹了Security6.4.2?自定義異常中統(tǒng)一響應遇到的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-03-03
Java中關于優(yōu)先隊列PriorityQueue的使用及相關方法
這篇文章主要介紹了Java中關于優(yōu)先隊列PriorityQueue的使用及相關方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
Maven pom的distributionManagement配置方式
文章主要介紹了Maven的distributionManagement配置方式,以及它的作用、配置方法和重要性,distributionManagement用于指定構(gòu)件的發(fā)布位置,包括下載URL、狀態(tài)等,文章還詳細解釋了如何配置repository和snapshotRepository,以及它們的用途和區(qū)別2025-01-01

