<dependencies>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>java
基礎方法:mysql
package com.java1234.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; } }
shiro.inisql
[users]
java1234=123456,role1,role2
jack=123,role1apache
role驗證;maven
package com.java1234.shiro; import java.util.Arrays; import org.apache.shiro.subject.Subject; import org.junit.Test; import com.java1234.common.ShiroUtil; public class RoleTest { @Test public void testHasRole() { Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "java1234", "123456"); // Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "jack", "123"); System.out.println(currentUser.hasRole("role1")?"有role1這個角色":"沒有role1這個角色"); boolean []results=currentUser.hasRoles(Arrays.asList("role1","role2","role3")); System.out.println(results[0]?"有role1這個角色":"沒有role1這個角色"); System.out.println(results[1]?"有role2這個角色":"沒有role2這個角色"); System.out.println(results[2]?"有role3這個角色":"沒有role3這個角色"); System.out.println(currentUser.hasAllRoles(Arrays.asList("role1","role2"))?"role1,role2這兩個角色都有":"role1,role2這個兩個角色不全有"); currentUser.logout(); } @Test public void testCheckRole() { Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "java1234", "123456"); // Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "jack", "123"); currentUser.checkRole("role1"); System.out.println("驗證role1經過"); currentUser.checkRoles(Arrays.asList("role1","role2")); System.out.println("驗證role1和2經過"); currentUser.checkRoles("role1","role2","role3"); //這個沒有經過,直接被拋出了異常! System.out.println("驗證role1和2和3經過"); // 這句話沒有執行! currentUser.logout(); } }
testHasRole驗證結果:1 spa
身份認證成功!
有role1這個角色
有role2這個角色
沒有role3這個角色
role1,role2這兩個角色都有code
testCheckRole驗證結果 ...token
shiro.inici
[users]
java1234=123456,role1,role2
jack=123,role1
[roles]
role1=user:select
role2=user:add,user:update,user:deleteget
permission 驗證:
package com.java1234.shiro; import org.apache.shiro.subject.Subject; import org.junit.Test; import com.java1234.common.ShiroUtil; public class PermissionTest { @Test public void testIsPermitted() { Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456"); // Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "jack", "123"); System.out.println(currentUser.isPermitted("user:select")?"有user:select這個權限":"沒有user:select這個權限"); System.out.println(currentUser.isPermitted("user:update")?"有user:update這個權限":"沒有user:update這個權限"); boolean results[]=currentUser.isPermitted("user:select","user:update","user:delete"); System.out.println(results[0]?"有user:select這個權限":"沒有user:select這個權限"); System.out.println(results[1]?"有user:update這個權限":"沒有user:update這個權限"); System.out.println(results[2]?"有user:delete這個權限":"沒有user:delete這個權限"); System.out.println(currentUser.isPermittedAll("user:select","user:update")?"有user:select,update這兩個權限":"user:select,update這兩個權限不全有"); currentUser.logout(); } @Test public void testCheckPermitted() { //Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456"); Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "jack", "123"); currentUser.checkPermission("user:select"); System.out.println("驗證:user:select 經過!"); currentUser.checkPermissions("user:select","user:update","user:delete"); //驗證不經過,直接拋出異常 System.out.println("驗證:user:select ,user:update,user:delete 經過!"); currentUser.logout(); } }