Shiro架构和Hello World的示例分析
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,今天就跟大家聊聊有关Shiro架构和Hello World的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Shiro 一个Apache
千家信息网最后更新 2025年12月03日Shiro架构和Hello World的示例分析
今天就跟大家聊聊有关Shiro架构和Hello World的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
Shiro 一个Apache 权限处理框架,现在更流行于security,能够指定用户的具体操作哪一个按钮,搭配接口,通过注解实现。
官网:
http://shiro.apache.org/download.html
主要涉及
权限管理,权限认证,session管理,加密
支持web,缓存,并发,测试,记住我
Shiro架构
Subject 当前用户
Security 管理subject
Realm 相当于RealmDao
Hello World
找到官网下载zip包。解压后

找到samples/quickstart/QuickStart.java
并将resource下面的配置复制
QuickStart.java
注释已经写在代码内
public class Quickstart {private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);public static void main(String[] args) {// The easiest way to create a Shiro SecurityManager with configured// realms, users, roles and permissions is to use the simple INI config.// We'll do that by using a factory that can ingest a .ini file and// return a SecurityManager instance:// Use the shiro.ini file at the root of the classpath// (file: and url: prefixes load from files and urls respectively):Factoryfactory = new IniSecurityManagerFactory("classpath:shiro.ini"); SecurityManager securityManager = factory.getInstance();// for this simple example quickstart, make the SecurityManager// accessible as a JVM singleton. Most applications wouldn't do this// and instead rely on their container configuration or web.xml for// webapps. That is outside the scope of this simple quickstart, so// we'll just do the bare minimum so you can continue to get a feel// for things.SecurityUtils.setSecurityManager(securityManager);// Now that a simple Shiro environment is set up, let's see what you can do:// get the currently executing user://获取当前的subject SecurityUtils.getSubject()Subject currentUser = SecurityUtils.getSubject();// Do some stuff with a Session (no need for a web or EJB container!!!)//获取sessionSession session = currentUser.getSession();//放入属性session.setAttribute("someKey", "aValue");//验证是否取到String value = (String) session.getAttribute("someKey");if (value.equals("aValue")) {log.info("Retrieved the correct value! -*******[" + value + "]");}// let's login the current user so we can check against roles and permissions://测试是否被认证if (!currentUser.isAuthenticated()) {//用户名密码封装UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");//记住我token.setRememberMe(true);try {//执行登录currentUser.login(token);//如果没有指定的用户} catch (UnknownAccountException uae) {log.info("There is no user with username of " + token.getPrincipal());//密码错误} catch (IncorrectCredentialsException ice) {log.info("Password for account " + token.getPrincipal() + " was incorrect!");//用户被锁定} catch (LockedAccountException lae) {log.info("The account for username " + token.getPrincipal() + " is locked. " +"Please contact your administrator to unlock it.");}// ... catch more exceptions here (maybe custom ones specific to your application?//总的认证异常处理catch (AuthenticationException ae) {//unexpected condition? error?}}//say who they are://print their identifying principal (in this case, a username):log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");//test a role://测试是否有某一个角色if (currentUser.hasRole("schwartz")) {log.info("May the Schwartz be with you!");} else {log.info("Hello, mere mortal.");}//test a typed permission (not instance-level)//测试用户是否有一个行为 weild//isPermitted The 'schwartz' role can do anything (*) with any lightsaber:if (currentUser.isPermitted("lightsaber:weild")) {log.info("You may use a lightsaber ring. Use it wisely.");} else {log.info("Sorry, lightsaber rings are for schwartz masters only.");}//a (very powerful) Instance Level permission://goodguy = winnebago:drive:eagle5 更具体的行为if (currentUser.isPermitted("winnebago:drive:eagle5")) {log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +"Here are the keys - have fun!");} else {log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");}//all done - log out!currentUser.logout();System.exit(0);}}
实际应用
@RequiresPermissions("risk:thirdInterface:view")@GetMapping()public String thirdInterface(ModelMap map, ThirdInterface thirdInterface) {ListthirdInterfaceList = thirdInterfaceService.selectThirdInterfaceList(thirdInterface); map.put("list", thirdInterfaceList);return prefix + "/thirdInterface";}
配合前端
var editFlag = [[${@permission.hasPermi('system:user:edit')}]];var removeFlag = [[${@permission.hasPermi('system:user:remove')}]];formatter: function(value, row, index) {var actions = [];actions.push('编辑 ');actions.push('删除 ');actions.push('重置');return actions.join('');}
在角色的分配操作时指定是否拥有某个按钮的权限操作。
看完上述内容,你们对Shiro架构和Hello World的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
用户
权限
测试
架构
内容
管理
认证
示例
分析
密码
按钮
行为
角色
处理
支持
代码
前端
实际
属性
接口
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
宝山区辅助软件开发创新服务
苹果手机隐藏数据库
服务器当台式机用
上海家用软件开发预算
sql数据库查看连接用户
数据库报错10004
深圳水木网络技术公司
163邮箱怎样恢复数据库
镇江网络安全防护
服务器管理员密码错误
创建矢量化数据库
信息安全管理员与网络安全管理员
机顶盒时间服务器
浦东新区数据软件开发有哪些
软件开发培训后的出路
群服务器
查询怎么更新数据库表
服务器做节点跟矿机挖矿
wmi数据库损坏
数据库新增一条数据语句
现成的数据库代码
c语言程序连接数据库设计实训
信息化及网络安全知识
怎么单独查数据库的某一列
互联网医美平台 科技赋能
网络技术运维方案
电子小报网络安全怎么制作
小红书数据库营销案例分析
网络安全和硬件工程师哪个好
温州java软件开发流程