shiro.ini实现授权
shiro.ini實現授權
前提:必須先認證通過之后有授權之說
1,授權概述
授權,也叫訪問控制,即在應用中控制誰能訪問哪些資源(如訪問頁面/編輯數據/頁面操作等)。在授權中需了解的幾個關鍵對象:主體(Subject)、資源(Resource)、權限(Permission)、角色(Role)。
2,關鍵對象介紹
1,主體
主體,即訪問應用的用戶,在Shiro中使用Subject代表該用戶。用戶只有授權后才允許訪問相應的資源。
2,資源
在應用中用戶可以訪問的任何東西,比如訪問JSP 頁面、查看/編輯某些數據、訪問某個業務方法、打印文本等等都是資源。用戶只要授權后才能訪問。
3,權限
安全策略中的原子授權單位,通過權限我們可以表示在應用中用戶有沒有操作某個資源的權力。即權限表示在應用中用戶能不能訪問某個資源,如:訪問用戶列表頁面查看/新增/修改/刪除用戶數據(即很多時候都是CRUD(增查改刪)式權限控制)打印文檔等等。。。
4,角色
角色代表了操作集合,可以理解為權限的集合,一般情況下我們會賦予用戶角色而不是權限,即這樣用戶可以擁有一組權限,賦予權限時比較方便。典型的如:項目經理、技術總監、CTO、開發工程師等都是角色,不同的角色擁有一組不同的權限。
3,授權流程
4,相關方法說明
1 subject.hasRole(“”); 判斷是否有角色
2 subject.hasRoles(List);分別判斷用戶是否具有List中每個內容
3 subject.hasAllRoles(Collection);返回boolean,要求參數中所有角色用戶都需要具有.
4 subject.isPermitted(“”);判斷是否具有權限.
5 subject.isPermittedAll(“”);判斷是否具有權限.
shiro.ini
#配置用戶 [users] zhangsan=123456,role1 lisi=123456,role2 wangwu=123456,role3 zhaoliu=123456,role2,role3 sunqi=123456,role4#聲明角色 [roles] role1=user:query,user:add,user:update,user:delete,user:export role2=user:query,user:add role3=user:query,user:export role4=*:*TestAuthorizationApp.java
package com.sxt.shiro;import java.util.Arrays; import java.util.List;import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationToken; 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; import org.slf4j.Logger; import org.slf4j.LoggerFactory;/*** shiro的認證使用shiro.ini文件**/ @SuppressWarnings("deprecation") public class TestAuthorizationApp {// 日志輸出工具private static final transient Logger log = LoggerFactory.getLogger(TestAuthorizationApp.class);public static void main(String[] args) {String username = "zhangsan";String password = "123456";log.info("My First Apache Shiro Application");// 1,創建安全管理器的工廠對象 org.apache.shiro.mgt.SecurityManager; 不能使用java.lang.SecurityManagerFactory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");// 2,使用工廠創建安全管理器SecurityManager securityManager = factory.getInstance();// 3,把當前的安全管理器綁定當到線的線程SecurityUtils.setSecurityManager(securityManager);// 4,使用SecurityUtils.getSubject得到主體對象Subject subject = SecurityUtils.getSubject();// 5,封裝用戶名和密碼AuthenticationToken token = new UsernamePasswordToken(username, password);// 6,得到認證try {subject.login(token);System.out.println("認證通過");} catch (AuthenticationException e) {System.out.println("用戶名或密碼不正確");} //subject.logout();//退出的方法//判斷用戶是否認證通過boolean authenticated = subject.isAuthenticated();System.out.println("是否認證通過:"+authenticated);//角色判斷boolean hasRole1 = subject.hasRole("role1");System.out.println("是否有role1的角色:"+hasRole1);//分別判斷集合里面的角色 返回數組List<String> roleIdentifiers=Arrays.asList("role1","role2","role3");boolean[] hasRoles = subject.hasRoles(roleIdentifiers);for (boolean b : hasRoles) {System.out.println(b);}//判斷當前用戶是否有roleIdentifiers集合里面的所有角色boolean hasAllRoles = subject.hasAllRoles(roleIdentifiers);System.out.println(hasAllRoles);//權限判斷boolean permitted = subject.isPermitted("user:query");System.out.println("判斷當前用戶是否有user:query的權限 "+permitted);boolean[] permitted2 = subject.isPermitted("user:query","user:add","user:export");for (boolean b : permitted2) {System.out.println(b);}boolean permittedAll = subject.isPermittedAll("user:query","user:add","user:export");System.out.println(permittedAll);}}?
?
?
?
?
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的shiro.ini实现授权的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shiro实现认证_ini
- 下一篇: 自定义Realm实现认证