1.权限的3要素:用户,角色,权限
用户:在shiro中就是subject
角色:权限的集合,一个角色可以有多个权限
角色,用户可以访问的数据或者URL,可进行的操作[增删改查]
授权:shiro授权的方式有3种
1.编程式授权:
1.1基于角色的访问控制
1.2基于权限的访问控制
2.注解式授权:
3.jsp标签授权:
/**
1.1基于角色的访问控制
shiro_role.ini
[users]
JAVA=123456,role1,role2
PHP=1234567,role1
**/
/**
* @author xp
* @Title: ShiroUtil.java
* @Package com.xp.shiro.roleshiro
* @Description: TODO
* @date 2016年4月24日 下午1:20:35
* @version V1.0
*/
package com.xp.shiro.roleshiro;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
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;
/**
* @author xp
* @ClassName: ShiroUtil
* @Description: 基于角色的访问控制
* @date 2016年4月24日 下午1:20:35
*
*/
public class ShiroUtil {
public static Subject login(String configFile, String username, String password) {
//读取配置文件创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory(configFile);
//创建SecurityManager实例
SecurityManager securityManager = factory.getInstance();
//将securityManager和SecurityUtils绑定
SecurityUtils.setSecurityManager(securityManager);
//获取当前登录用户
Subject currentSubject = SecurityUtils.getSubject();
UsernamePasswordToken token = null;
try {
//更具当前用户的用户名和密码创建token令牌
token = new UsernamePasswordToken(username, password);
currentSubject.login(token);//这句话最好写在try外面,用户名密码不对应当马上抛异常
} catch (UnknownAccountException e) {
e.printStackTrace();
System.out.println("登录失败");//ComboPooledDataSource
}
return currentSubject;
}
}
/**
* @author xp
* @Title: RoleTest.java
* @Package com.xp.shiro.roleshiro
* @Description: TODO
* @date 2016年4月24日 下午1:32:30
* @version V1.0
*/
package com.xp.shiro.roleshiro;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
/**
* @author xp
* @ClassName: RoleTest
* @Description: TODO
* @date 2016年4月24日 下午1:32:30
*
*/
public class RoleTest {
@Test
public void testRole() throws Exception {
Subject subject = ShiroUtil.login("classpath:shiro_role.ini", "JAVA", "123456");
System.out.println(subject.hasRole("role1"));//判断JAVA用户是否拥有role1角色
System.out.println(subject.hasRole("role2"));//判断JAVA用户是否拥有role1角色
System.out.println(subject.hasRole("role3"));//判断JAVA用户是否拥有role1角色
boolean[] hasRoles = subject.hasRoles(Arrays.asList(new String[]{"role1","role2","role3"}));
for (boolean b : hasRoles) {
System.out.println(b);
}
}
@Test
public void testCheck() {
/*如果没有角色会抛UnauthorizedException异常
* org.apache.shiro.authz.UnauthorizedException: Subject does not have role [role3]
at org.apache.shiro.authz.ModularRealmAuthorizer.checkRole(ModularRealmAuthorizer.java:421)
*/
Subject subject = ShiroUtil.login("classpath:shiro_role.ini", "JAVA", "123456");
try {
//subject.checkRole("role21");
//subject.checkRoles(Arrays.asList(new String[] { "role1", "role2","role3" }));
subject.checkRoles("role1","role2","role3");
} catch (UnauthorizedException e) {
e.printStackTrace();
}
}
}