package com.shi.authorization; import java.util.Arrays; import org.apache.shiro.SecurityUtils; 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.junit.Test; /** * 這是對用戶 受權的測試 * [@author](https://my.oschina.net/arthor) SHF * */ public class AuthorizationTest { //角色受權,資源受權 [@Test](https://my.oschina.net/azibug) public void authorizationTest(){ //1 建立咱們的securityManager工廠 Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro-permission.ini"); //2 建立咱們securityManager SecurityManager securityManager=factory.getInstance(); //3 將咱們的SecrityManager設置到運行環境 SecurityUtils.setSecurityManager(securityManager); //4 建立主題 Subject subject=SecurityUtils.getSubject(); //5 建立令牌 UsernamePasswordToken token=new UsernamePasswordToken("zhangsan","123"); try { //6 主體登陸 執行認證 subject.login(token); } catch (Exception e) { e.printStackTrace(); } //7看是否定證經過認證 System.out.println("認證狀態:"+subject.isAuthenticated()); //8 認證經過後執行受權 //8.1 基於角色的受權 boolean ishashRole=subject.hasRole("role1");//hasRole 穿入角色標識 boolean hasAllRoles=subject.hasAllRoles(Arrays.asList("role1","role2")); System.out.println("單個的角色:"+ishashRole); System.out.println("多個的角色:"+hasAllRoles); //8.2基於資源的受權 boolean isPermitted=subject.isPermitted("user:create"); boolean isPermittedAll=subject.isPermittedAll("user:create","user:update"); System.out.println("單個資源"+isPermitted); System.out.println("多個資源"+isPermittedAll); //經過check方法進行檢驗受權 不經過就拋出異常 try { subject.checkPermission("items:add:1"); } catch (AuthorizationException e) { e.printStackTrace(); } } }
shiro-permission.ini 文件java
#用戶,角色 [users] #用戶zhang的密碼是123,次用戶具備role1和role2倆個角色 zhangsan=123,role1,role2 wang=123,role2 #角色 權限 [roles] #角色role1 對資源user擁有create,update權限 role1=user:create,user:update #角色role2 對資源user擁有create,delete權限 role2=user:create,user:delete #role3 對資源items擁有create權限 role3=items:create
package com.shi.realm; import java.util.ArrayList; import java.util.List; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; public class CustomRealm extends AuthorizingRealm{ //設置realm的名字 @Override public void setName(String name) { super.setName("customRealm"); } /** * 用於認證 */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //1 從token中取出身份信息(token是用戶輸入的) String userCode=(String) token.getPrincipal(); //2 根據用戶輸入的userCode從數據庫查詢 //... 模擬數據庫中取出的密碼是"111111" String password="111111"; //3 若是 查詢不到返回null if(!"zhangsan".equals(userCode)){ return null; } //若是查詢到 返回認證信息AuthenticationInfo SimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(userCode, password, this.getName()); return simpleAuthenticationInfo; } /** * 用於受權 */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { /** * 1 從principals中得到主身份信息 * 將getPrimaryPrincipal方法返回值轉爲真實身份類型, * (在上邊的doGetAuthenticationInfo認證經過填充到SimpleAuthenticationInfo) */ String userCode=(String) principals.getPrimaryPrincipal(); /** * 2 根據身份信息獲取權限信息(從數據庫中查詢) * 模擬查詢到的數據 */ List<String> permissions=new ArrayList<String>(); permissions.add("user:create");//用戶的建立 permissions.add("items:add:1");//商品添加 //3 查詢到數據返回受權信息 SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo(); //4 將上面查詢到數據填充到SimpleAuthorizationInfo對象中 simpleAuthorizationInfo.addStringPermissions(permissions); return simpleAuthorizationInfo; } }
shiro-realm.ini 文件spring
[main] #自定義realm customRealm=com.shi.realm.CustomRealm #講realm設置到securityManager中,至關於spring中的注入 securityManager.realm=$customRealm
測試程序數據庫
//2 自定義realm測試 資源受權 @Test public void authorizationTestCustomerRealm(){ //1 建立咱們的securityManager工廠 Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro-realm.ini"); //2 建立咱們securityManager SecurityManager securityManager=factory.getInstance(); //3 將咱們的SecrityManager設置到運行環境 SecurityUtils.setSecurityManager(securityManager); //4 建立主題 Subject subject=SecurityUtils.getSubject(); //5 建立令牌 UsernamePasswordToken token=new UsernamePasswordToken("zhangsan","111111"); try { //6 主體登陸 執行認證 subject.login(token); } catch (Exception e) { e.printStackTrace(); } //7看是否定證經過認證 System.out.println("認證狀態:"+subject.isAuthenticated()); //8.2基於資源的受權 boolean isPermitted=subject.isPermitted("user:create"); boolean isPermittedAll=subject.isPermittedAll("user:create","user:update"); System.out.println("單個資源"+isPermitted); System.out.println("多個資源"+isPermittedAll); //經過check方法進行檢驗受權 不經過就拋出異常 try { subject.checkPermission("items:add:1"); } catch (AuthorizationException e) { e.printStackTrace(); } }