權限認證也就是訪問控制,即在應用中控制誰能訪問哪些資源java
抽取公共代碼生成 ShiroUtilapache
package com.zhen.common; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; 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; 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 currentUser = SecurityUtils.getSubject(); //建立token令牌,用戶名/密碼 UsernamePasswordToken token = new UsernamePasswordToken(userName, password); try { //身份認證 currentUser.login(token); System.out.println("身份認證成功!"); } catch (AuthenticationException e) { e.printStackTrace(); System.out.println("身份認證失敗!"); } return currentUser; } }
[users] zhen=123,role1,role2 jack=jack,role1
package com.zhen.shiro; import java.util.ArrayList; import java.util.List; import org.apache.shiro.subject.Subject; import org.junit.Test; import com.zhen.common.ShiroUtil; import junit.framework.TestCase; //基於角色的 public class RoleTest extends TestCase { @Test public void testHasRole(){ String configFile = "classpath:shiro_role.ini"; String userName = "jack"; String password = "jack"; Subject currentUser = ShiroUtil.login(configFile, userName, password); if (currentUser.hasRole("role2")) { System.out.println(userName+"有 role2 權限"); }else{ System.out.println(userName+"沒有 role2 權限"); } currentUser.logout(); } @Test public void testHasRoles(){ String configFile = "classpath:shiro_role.ini"; String userName = "jack"; String password = "jack"; Subject currentUser = ShiroUtil.login(configFile, userName, password); List<String> roles = new ArrayList<String>(); roles.add("role1"); roles.add("role2"); //返回一個boolean數組 boolean[] results = currentUser.hasRoles(roles); for (int i = 0; i < results.length; i++) { if(results[i]){ System.out.println(userName+"有 "+roles.get(i)+" 權限"); }else{ System.out.println(userName+"沒有 "+roles.get(i)+" 權限"); } } currentUser.logout(); } @Test public void testHasAllRoles(){ String configFile = "classpath:shiro_role.ini"; String userName = "zhen"; String password = "123"; Subject currentUser = ShiroUtil.login(configFile, userName, password); List<String> roles = new ArrayList<String>(); roles.add("role1"); roles.add("role2"); //是否擁有全部權限 boolean result = currentUser.hasAllRoles(roles); if(result){ System.out.println(userName+"有 全部權限"); }else{ System.out.println(userName+"沒有 全部權限"); } currentUser.logout(); } @Test public void testCheckRoles(){ //check 沒有返回值,沒有該權限的話就會拋異常 String configFile = "classpath:shiro_role.ini"; String userName = "jack"; String password = "jack"; Subject currentUser = ShiroUtil.login(configFile, userName, password); List<String> roles = new ArrayList<String>(); roles.add("role1"); roles.add("role2"); currentUser.checkRole(roles.get(1)); currentUser.logout(); } }
[users] zhen=123,role1,role2 jack=jack,role1 [roles] role1=user:select role2=user:add,user:update,user:delete
role1 對應有 user:select 權限
role2 對應有 user:add , user:update , user:delete 權限編程
package com.zhen.shiro; import org.apache.shiro.subject.Subject; import org.junit.Test; import com.zhen.common.ShiroUtil; import junit.framework.TestCase; //基於權限的 public class PermissionTest extends TestCase { @Test public void testIsPermission(){ String configFile = "classpath:shiro_permission.ini"; String userName = "zhen"; String password = "123"; Subject currentUser = ShiroUtil.login(configFile, userName, password); System.out.println(currentUser.isPermitted("user:add")?"有add權限":"沒有add權限"); System.out.println(currentUser.isPermitted("user:select")?"有select權限":"沒有select權限"); boolean[] results = currentUser.isPermitted("user:add","user:select"); System.out.println(results[0]?"有add權限":"沒有add權限"); System.out.println(results[1]?"有select權限":"沒有select權限"); System.out.println(currentUser.isPermittedAll("user:add","user:select")?"有user:add&user:select權限":"user:add&user:select權限不全有"); currentUser.logout(); } @Test public void testCheckPermission(){ String configFile = "classpath:shiro_permission.ini"; String userName = "zhen"; String password = "123"; Subject currentUser = ShiroUtil.login(configFile, userName, password); currentUser.checkPermission("user:add"); currentUser.checkPermission("user:select"); currentUser.checkPermissions("user:add","user:select"); currentUser.logout(); } }