电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

springboot整合Shiro

瀏覽:5日期:2023-02-26 16:37:25
目錄什么是ShiroShiro的三大核心概念Shiro功能介紹Springboot整合Shiro導入依賴javaConfigRealmControllerShiro整合thymeleaf導入依賴HTML頁面命名空間使用舉例總結什么是Shiro

Apache Shiro是一個功能強大且易于使用的Java安全框架,它執行身份驗證、授權、加密和會話管理。借助Shiro易于理解的API,您可以快速輕松地保護任何應用程序—從最小的移動應用程序到最大的web和企業應用程序。

Shiro的三大核心概念

springboot整合Shiro

Subject:

主體,代表了當前“用戶”,這個用戶不一定是一個具體的人,與當前應用交互的任何東西都是Subject,如爬蟲、機器人等;即一個抽象概念;所有Subject都綁定到SecurityManager,與Subject的所有交互都會委托給SecurityManager;可以把Subject認為是一個門面;SecurityManager才是實際的執行者。

SecurityManager:

安全管理器;即所有與安全有關的操作都會與SecurityManager交互;且它管理著所有Subject;可以看出它是shiro的核心, SecurityManager相當于spring mvc中的dispatcherServlet前端控制器。

Realm:

域,shiro從Realm獲取安全數據(如用戶、角色、權限),就是說SecurityManager要驗證用戶身份,那么它需要從Realm獲取相應的用戶進行比較以確定用戶身份是否合法;也需要從Realm得到用戶相應的角色/權限進行驗證用戶是否能進行操作;可以把Realm看成DataSource,即安全數據源。

Shiro功能介紹

springboot整合Shiro

Authentication:

身份認證/登錄,驗證用戶是不是擁有相應的身份;

Authorization:

授權,即權限驗證,驗證某個已認證的用戶是否擁有某個權限;即判斷用 戶是否能進行什么操作,如:驗證某個用戶是否擁有某個角色。或者細粒度的驗證某個用戶對某個資源是否具有某個權限

Session Manager:

會話管理,即用戶登錄后就是一次會話,在沒有退出之前,它的所有信息都在會話中;會話可以是普通 JavaSE 環境,也可以是 Web 環境的

Cryptography:

加密,保護數據的安全性,如密碼加密存儲到數據庫,而不是明文存儲; Web Support:Web 支持,可以非常容易的集成到Web 環境;

Caching:

緩存,比如用戶登錄后,其用戶信息、擁有的角色/權限不必每次去查,這樣可以提高效率;

Concurrency:

Shiro 支持多線程應用的并發驗證,即如在一個線程中開啟另一個線程,能把權限自動傳播過去;

Testing:

提供測試支持;

Run As:

允許一個用戶假裝為另一個用戶(如果他們允許)的身份進行訪問;

Remember Me:

記住我,這個是非常常見的功能,即一次登錄后,下次再來的話不用登錄了

Springboot整合Shiro導入依賴

<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring --><dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.7.1</version></dependency>

配置javaConfig

三大核心對象ShiroFilterFactoryBean、DefaultWebSecurityManager、Realm

常用攔截器分類說明

springboot整合Shiro

javaConfig

@Configurationpublic class ShiroConfig { //ShiroFilterFactoryBean @Bean public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Autowired DefaultWebSecurityManager securityManager) {ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();filterFactoryBean.setSecurityManager(securityManager);Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();//攔截filterChainDefinitionMap.put('/user/add', 'authc');filterChainDefinitionMap.put('/user/update', 'authc');filterChainDefinitionMap.put('/user/*', 'authc');filterChainDefinitionMap.put('/logout', 'logout');//退出/*filterChainDefinitionMap.put('/*','authc');*///授權filterChainDefinitionMap.put('/user/add','perms[user:add]');filterChainDefinitionMap.put('/user/update','perms[user:update]');//設置登錄的請求filterFactoryBean.setLoginUrl('/toLogin');//設置未授權頁面filterFactoryBean.setUnauthorizedUrl('/unauth');filterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);return filterFactoryBean; } //DefaultWebSecurityManager @Bean public DefaultWebSecurityManager getDefaultWebSecurityManager(@Autowired UserRealm userRealm) {DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();securityManager.setRealm(userRealm);return securityManager; } //Realm @Bean public UserRealm userRealm() {return new UserRealm(); }}Realm

創建UserRealm繼承AuthorizingRealm實現doGetAuthorizationInfo()、doGetAuthenticationInfo()方法

從數據庫中拿到用戶信息,這里需要整合MyBatis、Druid相關依賴,具體的springboot整合MyBatis的代碼這里就贅述了,如果自己聯系,可以不從數據庫中獲取數據,可以自己直接設定默認的username和password

springboot整合Shiro

perm是該用戶的權限可以通過authorizationInfo.addStringPermissions();方法授權

public class UserRealm extends AuthorizingRealm { @Autowired UserService userService; @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();//authorizationInfo.addStringPermission('user:add');Subject currentUser = SecurityUtils.getSubject();/** * 通過session取值 *//*Session session = currentUser.getSession();String username = (String) session.getAttribute('username');System.out.println(username);User user = userService.getByUsername(username);authorizationInfo.addStringPermission(user.getPerm());System.out.println(user.getPerm());*//** * 通過principal取值 */String username = (String) currentUser.getPrincipal();System.out.println(username);User user = userService.getByUsername(username);System.out.println(user.getPerm());String[] perms = user.getPerm().split(',');ArrayList<String> permList = new ArrayList();for (String perm : perms) { permList.add(perm);}authorizationInfo.addStringPermissions(permList);System.out.println('執行了======>授權');return authorizationInfo; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {System.out.println('執行了======>認證');UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;User user = userService.getByUsername(token.getUsername());if (user == null) { return null;}//密碼可以加密//密碼認證,shiro加密return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(),''); }}Controller

@Controllerpublic class MyController { @RequestMapping('/toLogin') public String toLogin() {return 'login'; } @RequestMapping({'/','/index'}) public String toIndex(Model model) {model.addAttribute('msg','Hello,Shiro');return 'index'; } @RequestMapping('/user/add') public String addUser() {return 'user/add'; } @RequestMapping('/user/update') public String updateUser() {return 'user/update'; } @PostMapping('/login') public String login(String username, String password, Model model) {UsernamePasswordToken token = new UsernamePasswordToken(username, password);Subject currentUser = SecurityUtils.getSubject(); try {currentUser.login(token);Session session = currentUser.getSession();session.setAttribute('username', username);return 'index'; } catch (UnknownAccountException uae) {model.addAttribute('msg', token.getPrincipal() + '用戶名不匹配');return 'login'; } catch (IncorrectCredentialsException ice) {model.addAttribute('msg', token.getPrincipal() + '密碼錯誤');return 'login'; } } @ResponseBody @RequestMapping('/unauth') public String unAuth() {return '未經授權'; } @RequestMapping('/logout') public String logout() {return '/login'; }}

前端頁面這里就不獻丑了,大家自由發揮

Shiro整合thymeleaf導入依賴

<!--thymeleaf shiro整合包--><!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro --><dependency> <groupId>com.github.theborakompanioni</groupId> <artifactId>thymeleaf-extras-shiro</artifactId> <version>2.0.0</version></dependency>HTML頁面命名空間

<html lang='en' xmlns:th='http://www.thymeleaf.org' xmlns:shiro='http://www.pollix.at/thymeleaf/shiro'>使用舉例

<div shiro:notAuthenticated=''> <!--沒有認證不顯示--> <p><button><a th:href='http://www.hdgsjgj.cn/bcjs/@{/toLogin}'>登錄</a></button></p></div><div shiro:authenticated=''><!--認證了顯示--> <p><button><a th:href='http://www.hdgsjgj.cn/bcjs/@{/logout}'>退出</a></button></p></div><hr/><div shiro:hasPermission='user:update'><!--有user:update 權限顯示--> <a th:href='http://www.hdgsjgj.cn/bcjs/@{/user/add}'>add</a></div><div shiro:hasPermission='user:add'><!--有user:add權限顯示--> <a th:href='http://www.hdgsjgj.cn/bcjs/@{/user/update}'>update</a></div>總結

本篇文章就到這里了,希望能對你有所幫助,也希望您能夠多多關注好吧啦網的更多內容!

標簽: Spring
相關文章:
主站蜘蛛池模板: 济南办公室装修-厂房装修-商铺装修-工装公司-山东鲁工装饰设计 | 分类168信息网 - 分类信息网 免费发布与查询 | 电竞学校_电子竞技培训学校学院-梦竞未来电竞学校官网 | 琉璃瓦-琉璃瓦厂家-安徽盛阳新型建材科技有限公司 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | 破碎机_上海破碎机_破碎机设备_破碎机厂家-上海山卓重工机械有限公司 | 环氧树脂地坪_防静电地坪漆_环氧地坪漆涂料厂家-地壹涂料地坪漆 环球电气之家-中国专业电气电子产品行业服务网站! | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 真空冷冻干燥机_国产冻干机_冷冻干燥机_北京四环冻干 | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | 深圳市东信高科自动化设备有限公司 | 光栅尺_Magnescale探规_磁栅尺_笔式位移传感器_苏州德美达 | 硬质合金模具_硬质合金非标定制_硬面加工「生产厂家」-西迪技术股份有限公司 | 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | 安平县鑫川金属丝网制品有限公司,声屏障,高速声屏障,百叶孔声屏障,大弧形声屏障,凹凸穿孔声屏障,铁路声屏障,顶部弧形声屏障,玻璃钢吸音板 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | X光检测仪_食品金属异物检测机_X射线检测设备_微现检测 | 专业的压球机生产线及解决方案厂家-河南腾达机械厂 | 济南ISO9000认证咨询代理公司,ISO9001认证,CMA实验室认证,ISO/TS16949认证,服务体系认证,资产管理体系认证,SC食品生产许可证- 济南创远企业管理咨询有限公司 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 山东信蓝建设有限公司官网| 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 整车VOC采样环境舱-甲醛VOC预处理舱-多舱法VOC检测环境仓-上海科绿特科技仪器有限公司 | 防渗土工膜|污水处理防渗膜|垃圾填埋场防渗膜-泰安佳路通工程材料有限公司 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 济南玻璃安装_济南玻璃门_济南感应门_济南玻璃隔断_济南玻璃门维修_济南镜片安装_济南肯德基门_济南高隔间-济南凯轩鹏宇玻璃有限公司 | 泰来华顿液氮罐,美国MVE液氮罐,自增压液氮罐,定制液氮生物容器,进口杜瓦瓶-上海京灿精密机械有限公司 | 土壤检测仪器_行星式球磨仪_土壤团粒分析仪厂家_山东莱恩德智能科技有限公司 | 动环监控_机房环境监控_DCIM_机房漏水检测-斯特纽 | 细石混凝土泵_厂家_价格-烟台九达机械有限公司 | 渣油泵,KCB齿轮泵,不锈钢齿轮泵,重油泵,煤焦油泵,泊头市泰邦泵阀制造有限公司 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 流变仪-热分析联用仪-热膨胀仪厂家-耐驰科学仪器商贸 | 【化妆品备案】进口化妆品备案流程-深圳美尚美化妆品有限公司 | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) |