Shiro入门到精通之HelloWorld
简介
-
Apache Shiro 是 Java 的一个安全(权限)框架。
-
Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境,也可以用在 JavaEE 环境。
- Shiro 可以完成:认证、授权、加密、会话管理、与Web 集成、缓存 等。
基本功能简介

-
Authentication:身份认证/登录,验证用户是不是拥有相应的身份;
-
Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限;即判断用 户是否能进行什么操作,如:验证某个用户是否拥有某个角色。或者细粒度的验证某个用户 对某个资源是否具有某个权限;
-
Session Manager:会话管理,即用户登录后就是一次会话,在没有退出之前,它的所有 信息都在会话中;会话可以是普通 JavaSE 环境,也可以是 Web 环境的;
-
Cryptography:加密,保护数据的安全性,如密码加密存储到数据库,而不是明文存储;
-
Web Support:Web 支持,可以非常容易的集成到Web 环境;
-
Caching:缓存,比如用户登录后,其用户信息、拥有的角色/权限不必每次去查,这样可 以提高效率;
-
Concurrency:Shiro 支持多线程应用的并发验证,即如在一个线程中开启另一个线程,能把权限自动传播过去;
-
Testing:提供测试支持;
-
Run As:允许一个用户假装为另一个用户(如果他们允许)的身份进行访问;
-
Remember Me:记住我,这个是非常常见的功能,即一次登录后,下次再来的话不用登录了
Shiro----helloworld
1 public static void main(String[] args) { 2 //工厂获取ini文件 3 Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); 4 //创建一个securityManager实例 5 SecurityManager securityManager = factory.getInstance(); 6 SecurityUtils.setSecurityManager(securityManager); 7 // 获取当前的 Subject. 调用 SecurityUtils.getSubject(); 8 Subject currentUser = SecurityUtils.getSubject(); 9 // 测试使用 Session 10 // 获取 Session: Subject#getSession() 11 Session session = currentUser.getSession(); 12 session.setAttribute("someKey", "aValue"); 13 String value = (String) session.getAttribute("someKey"); 14 if (value.equals("aValue")) { 15 log.info("---> Retrieved the correct value! [" + value + "]"); 16 } 17 // 测试当前的用户是否已经被认证. 即是否已经登录. 18 // 调动 Subject 的 isAuthenticated() 19 if (!currentUser.isAuthenticated()) { 20 // 把用户名和密码封装为 UsernamePasswordToken 对象 21 UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa"); 22 // rememberme 23 token.setRememberMe(true); 24 try { 25 // 执行登录. 26 currentUser.login(token); 27 } 28 // 若没有指定的账户, 则 shiro 将会抛出 UnknownAccountException 异常. 29 catch (UnknownAccountException uae) { 30 log.info("----> There is no user with username of " + token.getPrincipal()); 31 return; 32 } 33 // 若账户存在, 但密码不匹配, 则 shiro 会抛出 IncorrectCredentialsException 异常。 34 catch (IncorrectCredentialsException ice) { 35 log.info("----> Password for account " + token.getPrincipal() + " was incorrect!"); 36 return; 37 } 38 // 用户被锁定的异常 LockedAccountException 39 catch (LockedAccountException lae) { 40 log.info("The account for username " + token.getPrincipal() + " is locked. " 41 + "Please contact your administrator to unlock it."); 42 } 43 // 所有认证时异常的父类. 44 catch (AuthenticationException ae) { 45 } 46 } 47 log.info("----> User [" + currentUser.getPrincipal() + "] logged in successfully."); 48 49 // test a role: 50 // 测试是否有某一个角色. 调用 Subject 的 hasRole 方法. 51 if (currentUser.hasRole("schwartz")) { 52 log.info("----> May the Schwartz be with you!"); 53 } else { 54 log.info("----> Hello, mere mortal."); 55 return; 56 } 57 // 测试用户是否具备某一个行为. 调用 Subject 的 isPermitted() 方法。 58 if (currentUser.isPermitted("lightsaber:weild")) { 59 log.info("----> You may use a lightsaber ring. Use it wisely."); 60 } else { 61 log.info("Sorry, lightsaber rings are for schwartz masters only."); 62 } 63 // 测试用户是否具备某一个行为. 64 if (currentUser.isPermitted("user:delete:zhangsan")) { 65 log.info("----> You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'. " 66 + "Here are the keys - have fun!"); 67 } else { 68 log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!"); 69 } 70 // 执行登出. 调用 Subject 的 Logout() 方法. 71 System.out.println("---->" + currentUser.isAuthenticated()); 72 currentUser.logout(); 73 System.out.println("---->" + currentUser.isAuthenticated()); 74 75 System.exit(0); 76 } 77 }
说明:此代码是下载shiro的源代码里面的quickstart里面包含了ini文件
ini文件配置了用户的角色,权限等
用到的jar: log4j-1.2.15.jar shiro-all-1.3.2.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar

浙公网安备 33010602011771号