SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼
導(dǎo)入依賴(pom.xml)
<!--整合Shiro安全框架--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.0</version> </dependency> <!--集成jwt實(shí)現(xiàn)token認(rèn)證--> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.2.0</version> </dependency>
創(chuàng)建 ShiroConfig 配置類
@Configuration public class ShiroConfig { /** * ShiroFilterFactoryBean */ @Bean public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) { ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean(); //設(shè)置安全管理器 factoryBean.setSecurityManager(defaultWebSecurityManager); // 添加shiro的內(nèi)置過(guò)濾器 /* * anon:無(wú)需認(rèn)證就可以訪問(wèn) * authc:必須認(rèn)證才能訪問(wèn) * user:必須擁有 記住我 功能才能用 * perms:擁有對(duì)某個(gè)資源的權(quán)限能訪問(wèn) * role:擁有某個(gè)角色權(quán)限能訪問(wèn) */ Map<String, String> filterMap = new LinkedHashMap<>(); // 放行不需要權(quán)限認(rèn)證的接口 //放行登錄接口 filterMap.put("/login/**", "anon"); //放行用戶接口 filterMap.put("/", "anon"); // 網(wǎng)站首頁(yè) //認(rèn)證管理員接口 filterMap.put("/administrators/**", "authc"); factoryBean.setFilterChainDefinitionMap(filterMap); // 設(shè)置無(wú)權(quán)限時(shí)跳轉(zhuǎn)的 url // 設(shè)置登錄的請(qǐng)求 factoryBean.setLoginUrl("/login/toLogin"); return factoryBean; } /** * 注入 DefaultWebSecurityManager */ @Bean(name = "securityManager") public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("customRealm") CustomRealm customRealm) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); //關(guān)聯(lián)CustomRealm securityManager.setRealm(customRealm); return securityManager; } /** * 注入 securityManager */ @Bean public CustomRealm customRealm() { return new CustomRealm(); } }
創(chuàng)建密碼登錄時(shí)驗(yàn)證授權(quán) CustomRealm 類
@Component public class CustomRealm extends AuthorizingRealm { @Autowired AdministratorsService administratorsService; /* * 設(shè)置加密方式 */ { HashedCredentialsMatcher mather = new HashedCredentialsMatcher(); // 加密方式 mather.setHashAlgorithmName("md5"); // 密碼進(jìn)行一次運(yùn)算 mather.setHashIterations(512); this.setCredentialsMatcher(mather); } /** * 授權(quán) */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { System.out.println("————授權(quán)————doGetAuthorizationInfo————"); return null; } /** * 認(rèn)證 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("————認(rèn)證————doGetAuthenticationInfo————"); UsernamePasswordToken userToken = (UsernamePasswordToken) token; // 連接數(shù)據(jù)庫(kù) 查詢用戶數(shù)據(jù) QueryWrapper<Administrators> wrapper = new QueryWrapper<>(); wrapper.eq("username", userToken.getUsername()); Administrators administrators = administratorsService.getOne(wrapper); if (administrators == null) { return null; // 拋出異常 UnknownAccountException } // 密碼認(rèn)證,shiro做 return new SimpleAuthenticationInfo("", administrators.getPassword(), ""); } }
控制層用戶密碼登錄
//用戶名登錄 @ApiOperation(value = "管理員登錄", notes = "用戶名登錄--不進(jìn)行攔截") @PostMapping("/doLogin") public String doLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session,Model model) { // 獲取當(dāng)前的用戶 Subject subject = SecurityUtils.getSubject(); // 封裝用戶的登錄數(shù)據(jù) UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { subject.login(token); //保存session會(huì)話 管理員名字 session.setAttribute("adname", username); return "admin"; } catch (UnknownAccountException e) { model.addAttribute("usererror", "用戶名錯(cuò)誤!請(qǐng)重新輸入。"); return "login"; } catch (IncorrectCredentialsException ice) { model.addAttribute("pwerror", "密碼錯(cuò)誤!請(qǐng)重新輸入。"); return "login"; } }
到此這篇關(guān)于SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)SpringBoot 整合 Shiro 密碼登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 整合 Shiro 密碼登錄與郵件驗(yàn)證碼登錄功能(多 Realm 認(rèn)證)
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
- 基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析
- springboot整合shiro登錄失敗次數(shù)限制功能的實(shí)現(xiàn)代碼
- SpringBoot整合Shiro實(shí)現(xiàn)登錄認(rèn)證的方法
- SpringBoot+Shiro學(xué)習(xí)之密碼加密和登錄失敗次數(shù)限制示例
- springboot整合shiro多驗(yàn)證登錄功能的實(shí)現(xiàn)(賬號(hào)密碼登錄和使用手機(jī)驗(yàn)證碼登錄)
相關(guān)文章
Spring使用三級(jí)緩存解決循環(huán)依賴的問(wèn)題
本文給大家分享Spring使用三級(jí)緩存解決循環(huán)依賴的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06簡(jiǎn)單了解Java的默認(rèn)和靜態(tài)方法
這篇文章主要介紹了簡(jiǎn)單了解Java的默認(rèn)和靜態(tài)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Spring Boot 中使用 JSON Schema 校驗(yàn)復(fù)雜JSO
在數(shù)據(jù)交換領(lǐng)域,JSON Schema 以其強(qiáng)大的標(biāo)準(zhǔn)化能力,為定義和規(guī)范 JSON 數(shù)據(jù)的結(jié)構(gòu)與規(guī)則提供了有力支持,下面給大家介紹Spring Boot 中使用 JSON Schema 校驗(yàn)復(fù)雜JSON數(shù)據(jù)的過(guò)程,感興趣的朋友跟隨小編一起看看吧2024-08-08MyBatis批量更新(update foreach)報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了MyBatis批量更新(update foreach)報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Mybatis日期格式自動(dòng)轉(zhuǎn)換需要用到的兩個(gè)注解說(shuō)明
這篇文章主要介紹了Mybatis日期格式自動(dòng)轉(zhuǎn)換需要用到的兩個(gè)注解說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08淺談SpringSecurity注解與AOP切面執(zhí)行順序
這篇文章主要介紹了淺談SpringSecurity注解與AOP切面執(zhí)行順序,引入Spring Security后,在Controller的方法中會(huì)出現(xiàn)Spring Security的方法注解與AOP同時(shí)存在的問(wèn)題,這是就會(huì)設(shè)計(jì)順序問(wèn)題,需要的朋友可以參考下2023-10-10Spring?Boot?整合?Thymeleaf?實(shí)例分享
這篇文章主要分享了Spring?Boot整合Thymeleaf,Thymeleaf是新一代的Java模板引擎,類似于Velocity、FreeMarker等傳統(tǒng)引擎,關(guān)于其更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-05-05