Shiro简单实现HelloWorld

布满荆棘的人生 2024-04-17 17:59 126阅读 0赞

点击进入Shiro 下载官网

1、所需jar包

shiro-all-1.4.1.jar、log4j-1.2.17.jar、slf4j-api-1.7.28.jar、slf4j-log4j12-1.7.28.jar

2、导入以下两个文件(文件位于shiro-root-source-release\shiro-root\samples\quickstart\src\main\resources文件夹下)

20190829163753165.png

3、Quickstart.java代码

  1. package com.hern.shiro;
  2. import org.apache.shiro.SecurityUtils;
  3. import org.apache.shiro.authc.*;
  4. import org.apache.shiro.config.IniSecurityManagerFactory;
  5. import org.apache.shiro.mgt.SecurityManager;
  6. import org.apache.shiro.session.Session;
  7. import org.apache.shiro.subject.Subject;
  8. import org.apache.shiro.util.Factory;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. /**
  12. * Simple Quickstart application showing how to use Shiro's API.
  13. *
  14. * @since 0.9 RC2
  15. */
  16. public class Quickstart {
  17. private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
  18. public static void main(String[] args) {
  19. //创建Shiro SecurityManager的最简单方法
  20. //领域、用户、角色和权限是使用简单的ini配置。
  21. //我们将使用一个可以接收.ini文件
  22. //返回SecurityManager实例:
  23. //使用类路径根目录下的shiro.ini文件
  24. //(file:和url:分别从文件和url加载前缀):
  25. Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
  26. SecurityManager securityManager = factory.getInstance();
  27. // for this simple example quickstart, make the SecurityManager
  28. // accessible as a JVM singleton. Most applications wouldn't do this
  29. // and instead rely on their container configuration or web.xml for
  30. // webapps. That is outside the scope of this simple quickstart, so
  31. // we'll just do the bare minimum so you can continue to get a feel
  32. // for things.
  33. SecurityUtils.setSecurityManager(securityManager);
  34. //获取当前正在执行的用户:
  35. //获取当前的Subject,调用SecurityUtils.getSubject();
  36. Subject currentUser = SecurityUtils.getSubject();
  37. //在会话中做一些事情(不需要Web或EJB容器!!!!)
  38. //测试使用Session。首先获取Session,通过Subject的getSession()
  39. Session session = currentUser.getSession();
  40. session.setAttribute("someKey", "aValue");
  41. String value = (String) session.getAttribute("someKey");
  42. if (value.equals("aValue")) {
  43. log.info("检索到正确的值! [" + value + "]");
  44. }
  45. //让我们登录当前用户,以便检查角色和权限:
  46. //测试当前的用户是否已经被认证,即是否已经登录
  47. //调用Subject的isAuthenticated()
  48. if (!currentUser.isAuthenticated()) {
  49. //把用户名和密码封装为UsernamePasswordToken对象
  50. UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
  51. //RememberME
  52. token.setRememberMe(true);
  53. try {
  54. //执行登录
  55. currentUser.login(token);
  56. } catch (UnknownAccountException uae) {//若没有指定的账户,则Shiro将会抛出UnknownAccountException异常
  57. log.info("没有用户名为 " + token.getPrincipal() + " 的用户");
  58. return;
  59. } catch (IncorrectCredentialsException ice) {//若账户存在,但密码不匹配,则Shiro将会抛出IncorrectCredentialsException异常
  60. log.info("账户的密码 " + token.getPrincipal() + " 是不正确的!");
  61. return;
  62. } catch (LockedAccountException lae) {//用户锁定的异常
  63. log.info("账户的用户名" + token.getPrincipal() + " 已锁定。 " +
  64. "请与管理员联系以解锁!");
  65. }
  66. // ... catch more exceptions here (maybe custom ones specific to your application?
  67. catch (AuthenticationException ae) {//所有认证时异常的父类
  68. //unexpected condition? error?
  69. }
  70. }
  71. //say who they are:
  72. //print their identifying principal (in this case, a username):
  73. log.info("用户 [" + currentUser.getPrincipal() + "] 登录成功!");
  74. //测试一个角色,调用Subject的hasRole()
  75. if (currentUser.hasRole("schwartz")) {
  76. log.info("May the Schwartz be with you!");
  77. } else {
  78. log.info("你好,你仅仅是普通用户角色");
  79. }
  80. //测试是否具有权限(不是实例级别),调用Subject的isPermitted方法
  81. if (currentUser.isPermitted("lightsaber:wield")) {
  82. log.info("You may use a lightsaber ring. Use it wisely.");
  83. } else {
  84. log.info("Sorry, lightsaber rings are for schwartz masters only.");
  85. }
  86. //测试是否具有权限,更加具体,调用Subject的isPermitted方法
  87. if (currentUser.isPermitted("winnebago:drive:eagle5")) {
  88. log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " +
  89. "Here are the keys - have fun!");
  90. } else {
  91. log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
  92. }
  93. //all done - log out!
  94. //执行登出
  95. currentUser.logout();
  96. System.exit(0);
  97. }
  98. }

发表评论

表情:
评论列表 (有 0 条评论,126人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Apache Shiro-HelloWorld

    Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码学和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的