shiro(1)-简介
简介
apache shiro 是一个功能强大和易于使用的Java安全框架,为开发人员提供一个直观而全面的的解决方案的认证,授权,加密,会话管理。
在实际应用中,它实现了应用程序的安全管理的各个方面。
shiro的功能
apache shiro能做什么?
支持认证跨一个或多个数据源(LDAP,JDBC,kerberos身份等)
执行授权,基于角色的细粒度的权限控制。
增强的缓存的支持。
支持web或者非web环境,可以在任何单点登录(SSO)或集群分布式会话中使用。
主要功能是:认证,授权,会话管理和加密。
下载并且使用
1,确保系统内安装JDK1.5+和maven2.2+。
2,到shiro主页下载shiro.
3,解压缩
unzip shiro-root-1.1.0-source-release.zip
4,进入到quickstart目录
cd shiro-root-1.1.0/samples/quickstart
5,运行quickstart
mvn compile exec:java
执行完成如下图:
Quickstart.java
// get the currently executing user: Subject currentUser = SecurityUtils.getSubject(); |
使用SecurityUtils.getSubject(),我们可以得到当前正在执行的主题。
得到主题之后,你可以得到他对应的会话信息
// Do some stuff with a Session (no need for a web or EJB container!!!) Session session = currentUser.getSession(); session.setAttribute( "someKey" , "aValue" ); String value = (String) session.getAttribute( "someKey" ); if (value.equals( "aValue" )) { log.info( "Retrieved the correct value! [" + value + "]" ); } |
你可以得到http的session信息,也可以在非web环境中使用,得到相对应的会话信息。
如果在web应用程序中部署应用,默认情况下,应用将以HttpSession为基础。在企业级应用中,你在多个应用中可以使用相同的API,无论部署环境。而且使用任何客户端技术你都可以共享会话数据。
接下来判断登录信息
// 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) 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: 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(); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架