SpringBoot Shiro授權(quán)實現(xiàn)過程解析
這篇文章主要介紹了SpringBoot Shiro授權(quán)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
使用Shiro過濾器實現(xiàn)授權(quán)
設(shè)置好授權(quán)攔截跳轉(zhuǎn)的請求地址
/** * 創(chuàng)建ShiroFilterFactoryBean */ @Bean public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean(); //設(shè)置安全管理器 shiroFilterFactoryBean.setSecurityManager(securityManager); //添加Shiro內(nèi)置過濾器 /** * Shiro內(nèi)置過濾器,可以實現(xiàn)權(quán)限相關(guān)的攔截器 * 常用的過濾器: * anon:無需認(rèn)證(登錄)可以訪問 * authc:必須認(rèn)證才可以訪問 * user:如果使用rememberMe的功能可以直接訪問 * perms:該資源必須得到資源權(quán)限才可以訪問 * role:該資源必須得到角色權(quán)限才可以訪問 */ Map<String, String> filterMap= new LinkedHashMap<String,String>(); //設(shè)置攔截后跳轉(zhuǎn)的請求路徑 shiroFilterFactoryBean.setLoginUrl("/user/badRequest"); //設(shè)置未授權(quán)提示的頁面 shiroFilterFactoryBean.setUnauthorizedUrl("/user/badRequest"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap); return shiroFilterFactoryBean; }
再使用 filterMap 去放對應(yīng)的攔截即可,例如 filterMap.put("/**", "anon")
上面是認(rèn)證的,如果需要授權(quán)的,相應(yīng)的多給一個任意的授權(quán)字符串如 filterMap.put("/user/add", "perms[user:add]")
授權(quán)邏輯編寫的位置
/** * 執(zhí)行授權(quán)邏輯 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { System.out.println("開始執(zhí)行Shiro的授權(quán)方法..."); //給資源進(jìn)行授權(quán) SimpleAuthorizationInfo info= new SimpleAuthorizationInfo(); //添加資源的授權(quán)字符串 info.addStringPermission("user:add"); return null; }
授權(quán)字符串和之前的字符串對應(yīng)
補(bǔ)充
/** * 執(zhí)行認(rèn)證邏輯 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException { System.out.println("開始執(zhí)行Shiro的認(rèn)證方法..."); //編寫Shiro判斷邏輯,判斷用戶名和密碼 //判斷用戶名是否存在 UsernamePasswordToken token=(UsernamePasswordToken)arg0; User user = userMapper.findByUsername(token.getUsername()); if(user==null) return null; //判斷密碼是否正確,參數(shù)一為認(rèn)證的實體 return new SimpleAuthenticationInfo(user,user.getPassword(),""); }
在執(zhí)行認(rèn)證時的SimpleAuthenticationInfo第一個傳參可以用來獲取當(dāng)前用戶
/** * 執(zhí)行授權(quán)邏輯 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { System.out.println("開始執(zhí)行Shiro的授權(quán)方法..."); //給資源進(jìn)行授權(quán) SimpleAuthorizationInfo info= new SimpleAuthorizationInfo(); //添加資源的授權(quán)字符串 info.addStringPermission("user:register"); //獲取當(dāng)前的登錄用戶 User user=(User)SecurityUtils.getSubject().getPrincipal(); return null; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于springboot redirect重定向路徑問題總結(jié)
這篇文章主要介紹了springboot redirect重定向路徑問題總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java實現(xiàn)學(xué)生管理系統(tǒng)詳解
這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-10-10解決MyBatis中模糊搜索使用like匹配帶%字符時失效問題
Mybatis是我們?nèi)粘m椖恐薪?jīng)常使用的框架,在項目中我們一般會使用like查詢作為模糊匹配字符進(jìn)行搜索匹配,下面的Mapper.xml是我們使用like在項目中進(jìn)行模糊匹配的常用方式,感興趣的朋友跟隨小編一起看看吧2021-09-09學(xué)習(xí)SpringBoot容器功能及注解原理
這篇文章主要介紹了學(xué)習(xí)SpringBoot容器功能及注解原理,文中通過詳細(xì)的代碼示例對SpringBoot容器功能及注解原理進(jìn)行了解析,有需要的朋友可以借鑒參考下2021-09-09