下面内容是在看了涛哥的《跟我一起学shiro》 和 视频《一头扎入进shiro》 后整理出来备忘和方便自己和其它人学习。
个人主页:http://www.itit123.cn/ 很多其它干货等你来拿
授权相关概念了解
权限认证:什么样的用户拥有什么样的权限做什么样的事。
三要素:权限,角色,用户。
角色:权限的集合。一个角色能够拥有多个权限
用户:角色的集合。一个用户能够拥有多个角色。也就是Subject
上代码:
为了方便函数的调用,将用户登录代码封装一下:
package com.shiro.utils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
public class ShiroUtil {
public static Subject login(String config , String userName ,String password){
// 1.读取配置文件,初始化SecurityManager工厂
Factory<SecurityManager> factory=new IniSecurityManagerFactory(config);
// 2.获取securityManager实例
SecurityManager securityManager=factory.getInstance();
// 3.把securityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
// 4.获取当前运行的用户
Subject currentUser=SecurityUtils.getSubject();
// 5.创建token令牌。username/密码
UsernamePasswordToken token=new UsernamePasswordToken(userName, password);
try{
// 6.登录时认证身份
currentUser.login(token);
System.out.println("身份认证成功!
"); }catch(AuthenticationException e){ e.printStackTrace(); System.out.println("身份认证失败!"); } return currentUser; } }
基于角色的訪问控制
在之前的基础上新建shiro_role.ini
[users]
ITDragon=123456,role1,role2
other=123456,role1
another=123456,role2
单元測试类:
hasRole拥有什么权限,checkRole检查有什么权限
// 基于角色
@Test
public void hasRoleTest(){
Subject user = ShiroUtil.login("classpath:shiro_role.ini", "other", "123456");
System.out.println(user.hasRole("role1")?
"是role1角色":"不是role1角色"); // 測试时不能同一时候存在。由于另外一个还没有退出 //Subject user2 = ShiroUtil.login("classpath:shiro_role.ini", "ITDragon", "123456"); boolean[] result = user.hasRoles(Arrays.asList("role1","role2")); for (boolean role : result) { System.out.println(role); } System.out.println(user.hasAllRoles(Arrays.asList("role1","role2"))?"都拥有role1。role2角色":"不全拥有role1,role2角色"); user.logout(); } @Test public void checkRoleTest(){ Subject user = ShiroUtil.login("classpath:shiro_role.ini", "ITDragon", "123456"); // 验证不通过报错 user.checkRole("role1"); user.checkRoles(Arrays.asList("role1","role2")); user.checkRoles("role1","role2"); user.logout(); }
基于权限的訪问控制:
[users]
ITDragon=123456,role1,role2
other=123456,role1
another=123456,role2
[roles]
role1=user:select
role2=user:update,user:delete
单元測试类:
// 基于权限
@Test
public void isPermittedTest(){
Subject user = ShiroUtil.login("classpath:shiro_permission.ini", "other", "123456");
System.out.println(user.isPermitted("user:select")?"拥有select权限":"没有select权限");
boolean[] result = user.isPermitted("user:select","user:update","user:delete");
for (boolean role : result) {
System.out.println(role);
}
System.out.println(user.isPermittedAll("user:select","user:update")?"都拥有select,updata权限":"不全拥有select,updata权限");
user.logout();
}
@Test
public void checkPermittedTest(){
Subject user = ShiroUtil.login("classpath:shiro_permission.ini", "other", "123456");
user.checkPermission("user:select");
user.checkPermissions("user:select","user:update","user:delete");
user.logout();
}
犯的错误:
在一个測试方法中登入了两个用户,并没有做用户退出操作。影响:权限推断错误。