shiro基於角色的訪問控制

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();
        }
    }
    

}

相關文章
相關標籤/搜索