Écrire le fichier de configuration shiro-permission.ini
[users]
#用户zhang的密码是123,此用户具有role1和role2两个角色
zhangsan=666,role1,role2
lisi=888,role2
[roles]
#角色role1对资源user拥有create、update权限
role1=user:create,user:update
#角色role2对资源user拥有create、delete权限
role2=user:create,user:delete
#角色role3对资源user拥有create权限
role3=user:create
Programme de test d'écriture
public class MyTest {
@Test
public void hasRole(){
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-permission.ini");
SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "666");
try {
subject.login(token);
}catch (Exception e){
e.printStackTrace();
}
System.out.println(subject.isPermitted("user:delete"));
System.out.println(subject.isPermittedAll("user:create","user:delete"));
System.out.println(Arrays.toString(subject.isPermitted("user:list","user:create")));
subject.checkPermission("user:list");
}
}