Shiro 笔记 实现role permission验证

maven配置:添加shiro相关的包,以及jdbc相关的包,junit包

  1. <dependency>
  2. <groupId>junit </groupId>
  3. <artifactId>junit </artifactId>
  4. <version>4.12 </version>
  5. </dependency>

添加log4j.properties


配置ShiroUtil工具类

  1. public static Subject login(String configFile,String userName,String password){
  2. // 读取配置文件,初始化SecurityManager工厂
  3. Factory <SecurityManager> factory=new IniSecurityManagerFactory(configFile);
  4. // 获取securityManager实例
  5. SecurityManager securityManager=factory.getInstance();
  6. // 把securityManager实例绑定到SecurityUtils
  7. SecurityUtils.setSecurityManager(securityManager);
  8. // 得到当前执行的用户
  9. Subject currentUser=SecurityUtils.getSubject();
  10. // 创建token令牌,用户名/密码
  11. UsernamePasswordToken token=new UsernamePasswordToken(userName, password);
  12. try{
  13. // 身份认证
  14. currentUser.login(token);
  15. System.out.println("身份认证成功!");
  16. }catch(AuthenticationException e){
  17. e.printStackTrace();
  18. System.out.println("身份认证失败!");
  19. }
  20. return currentUser;
  21. }

shiro_role.ini

  1. [users]
  2. java1234=123456,role1,role2
  3. jack=123,role1

RoleTest测试类

  1. @Test
  2. public void testHasRole() {
  3. Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "java1234", "123456");
  4. // Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "jack", "123");
  5. System.out.println(currentUser.hasRole("role1")?"有role1这个角色":"没有role1这个角色");
  6. boolean []results=currentUser.hasRoles(Arrays.asList("role1","role2","role3"));
  7. System.out.println(results[0]?"有role1这个角色":"没有role1这个角色");
  8. System.out.println(results[1]?"有role2这个角色":"没有role2这个角色");
  9. System.out.println(results[2]?"有role3这个角色":"没有role3这个角色");
  10. System.out.println(currentUser.hasAllRoles(Arrays.asList("role1","role2"))?"role1,role2这两个角色都有":"role1,role2这个两个角色不全有");
  11. currentUser.logout();
  12. }
  13. @Test
  14. public void testCheckRole() {
  15. Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "java1234", "123456");
  16. // Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "jack", "123");
  17. currentUser.checkRole("role1");
  18. currentUser.checkRoles(Arrays.asList("role1","role2"));
  19. currentUser.checkRoles("role1","role2","role3");
  20. currentUser.logout();
  21. }

shiro_permission.ini配置

  1. [users]
  2. java1234=123456,role1,role2
  3. jack=123,role1
  4. [roles]
  5. role1=user:select
  6. role2=user:add,user:update,user:delete

PermissionTest测试类

  1. @Test
  2. public void testIsPermitted() {
  3. Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");
  4. // Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "jack", "123");
  5. System.out.println(currentUser.isPermitted("user:select")?"有user:select这个权限":"没有user:select这个权限");
  6. System.out.println(currentUser.isPermitted("user:update")?"有user:update这个权限":"没有user:update这个权限");
  7. boolean results[]=currentUser.isPermitted("user:select","user:update","user:delete");
  8. System.out.println(results[0]?"有user:select这个权限":"没有user:select这个权限");
  9. System.out.println(results[1]?"有user:update这个权限":"没有user:update这个权限");
  10. System.out.println(results[2]?"有user:delete这个权限":"没有user:delete这个权限");
  11. System.out.println(currentUser.isPermittedAll("user:select","user:update")?"有user:select,update这两个权限":"user:select,update这两个权限不全有");
  12. currentUser.logout();
  13. }
  14. @Test
  15. public void testCheckPermitted() {
  16. Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");
  17. // Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "jack", "123");
  18. currentUser.checkPermission("user:select");
  19. currentUser.checkPermissions("user:select","user:update","user:delete");
  20. currentUser.logout();
  21. }
posted @ 2022-08-11 18:51  码海兴辰  阅读(48)  评论(0编辑  收藏  举报