基于角色:
shiro_role.ini:
[users]
zb=123,role1,role2
zz=123,role1
// 读取配置文件,初始化SecurityManager工厂
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro_role.ini");
// 获取securityManager实例
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
// 把securityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
// 得到当前执行的用户
Subject currentUser = SecurityUtils.getSubject();
// 创建token令牌,用户名/密码
UsernamePasswordToken token = new UsernamePasswordToken("zb", "123");
try {
// 身份认证
currentUser.login(token);
System.out.println("身份认证成功!" + token.getUsername());
} catch (IncorrectCredentialsException e) {
System.out.println("登录密码错误");
} catch (ExcessiveAttemptsException e) {
System.out.println("登录失败次数过多");
} catch (LockedAccountException e) {
System.out.println("帐号已被锁定");
} catch (DisabledAccountException e) {
System.out.println("帐号已被禁用");
} catch (ExpiredCredentialsException e) {
System.out.println("帐号已过期");
} catch (UnknownAccountException e) {
System.out.println("帐号不存在");
}
boolean[] results = currentUser.hasRoles(Arrays.asList("role1", "role2", "role3"));
// 角色检查role1
if (currentUser.hasRole("role1")) {
System.out.println(currentUser.getPrincipal().toString() + "有role1这个角色");
} else {
System.out.println(currentUser.getPrincipal().toString() + "没有role1这个角色");
}
// 角色检查role1
if (results[0]) {
System.out.println(currentUser.getPrincipal().toString() + "有role1这个角色");
} else {
System.out.println(currentUser.getPrincipal().toString() + "没有role1这个角色");
}
// 角色检查role2
if (results[1]) {
System.out.println(currentUser.getPrincipal().toString() + "有role2这个角色");
} else {
System.out.println(currentUser.getPrincipal().toString() + "没有role2这个角色");
}
// 角色检查role3
if (results[2]) {
System.out.println(currentUser.getPrincipal().toString() + "有role3这个角色");
} else {
System.out.println(currentUser.getPrincipal().toString() + "没有role3这个角色");
}
System.out.println(currentUser.hasAllRoles(Arrays.asList("role1", "role2")) ? "role1,role2这两个角色都有"
: "role1,role2这个两个角色不全有");
// 退出
currentUser.logout();
基于权限:
shiro_permission:
[users]
java1234=123456,role1,role2
jack=123,role1
[roles]
role1=user:select
role2=user:add,user:update,user:delete
// 读取配置文件,初始化SecurityManager工厂
Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro_role.ini");
// 获取securityManager实例
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
// 把securityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
// 得到当前执行的用户
Subject currentUser = SecurityUtils.getSubject();
// 创建token令牌,用户名/密码
UsernamePasswordToken token = new UsernamePasswordToken("zb", "123");
try {
// 身份认证
currentUser.login(token);
System.out.println("身份认证成功!" + token.getUsername());
} catch (IncorrectCredentialsException e) {
System.out.println("登录密码错误");
} catch (ExcessiveAttemptsException e) {
System.out.println("登录失败次数过多");
} catch (LockedAccountException e) {
System.out.println("帐号已被锁定");
} catch (DisabledAccountException e) {
System.out.println("帐号已被禁用");
} catch (ExpiredCredentialsException e) {
System.out.println("帐号已过期");
} catch (UnknownAccountException e) {
System.out.println("帐号不存在");
}
System.out.println(currentUser.isPermitted("user:select") ? "有user:select这个权限" : "没有user:select这个权限");
System.out.println(currentUser.isPermitted("user:update") ? "有user:update这个权限" : "没有user:update这个权限");
boolean results[] = currentUser.isPermitted("user:select", "user:update", "user:delete");
System.out.println(results[0] ? "有user:select这个权限" : "没有user:select这个权限");
System.out.println(results[1] ? "有user:update这个权限" : "没有user:update这个权限");
System.out.println(results[2] ? "有user:delete这个权限" : "没有user:delete这个权限");
System.out.println(currentUser.isPermittedAll("user:select", "user:update") ? "有user:select,update这两个权限"
: "user:select,update这两个权限不全有");
// 退出
currentUser.logout();