注:大部分内容摘录Apache shiro使用手册

  1. import org.apache.shiro.SecurityUtils;  
  2. import org.apache.shiro.authc.*;  
  3. import org.apache.shiro.config.IniSecurityManagerFactory;  
  4. import org.apache.shiro.mgt.SecurityManager;  
  5. import org.apache.shiro.session.Session;  
  6. import org.apache.shiro.subject.Subject;  
  7. import org.apache.shiro.util.Factory;  
  8. import org.slf4j.Logger;  
  9. import org.slf4j.LoggerFactory;  
  10.  
  11. public class Quickstart {  
  12.  
  13.     private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);  
  14.  
  15.     public static void main(String[] args) {    
  16.         Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");  
  17.         SecurityManager securityManager = factory.getInstance();  
  18.         SecurityUtils.setSecurityManager(securityManager);  
  19.         Subject currentUser = SecurityUtils.getSubject();  
  20.         Session session = currentUser.getSession();  
  21.         session.setAttribute("someKey", "aValue");  
  22.         String value = (String) session.getAttribute("someKey");  
  23.         if (value.equals("aValue")) {  
  24.             log.info("Retrieved the correct value! [" + value + "]");  
  25.         }  
  26.         if (!currentUser.isAuthenticated()) {  
  27.             UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");  
  28.             token.setRememberMe(true);  
  29.             try {  
  30.                 currentUser.login(token);  
  31.             } catch (UnknownAccountException uae) {  
  32.                 log.info("There is no user with username of " + token.getPrincipal());  
  33.             } catch (IncorrectCredentialsException ice) {  
  34.                 log.info("Password "+token.getCredentials() +"for account " + token.getPrincipal() + " was incorrect!");  
  35.             } catch (LockedAccountException lae) {  
  36.                 log.info("The account for username " + token.getPrincipal() + " is locked.  " +  
  37.                         "Please contact your administrator to unlock it.");  
  38.             }  
  39.             catch (AuthenticationException ae) {  
  40.  
  41.             }  
  42.         }  
  43.  
  44.         log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");  
  45.         if (currentUser.hasRole("schwartz")) {  
  46.             log.info("May the Schwartz be with you!");  
  47.         } else {  
  48.             log.info("Hello, mere mortal.");  
  49.         }  
  50.  
  51.         if (currentUser.isPermitted("lightsaber:weild")) {  
  52.             log.info("You may use a lightsaber ring.  Use it wisely.");  
  53.         } else {  
  54.             log.info("Sorry, lightsaber rings are for schwartz masters only.");  
  55.         }  
  56.         if (currentUser.isPermitted("winnebago:drive:eagle5")) {  
  57.             log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +  
  58.                     "Here are the keys - have fun!");  
  59.         } else {  
  60.             log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");  
  61.         }  
  62.         //currentUser.logout();  
  63.         System.exit(0);  
  64.     }  
  65. }  
  66.  

程序大意解读:

1. Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
    SecurityManager securityManager = factory.getInstance();

读取配置文件获取SecurityManager实例;

2. SecurityUtils.setSecurityManager(securityManager);
    Subject currentUser = SecurityUtils.getSubject();
    Session session = currentUser.getSession();

securityManager作为参数获取当前操作用户currentUser ,获取当前用户(被shiro框架封装好)的session实例;

3.  UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
     token.setRememberMe(true);

生成用户名/密码口令,并选择了记住我(实用cookie保存了帐号的信息);

4. log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
        if (currentUser.hasRole("schwartz")) {
            log.info("May the Schwartz be with you!");
        } else {
            log.info("Hello, mere mortal.");
        }

        if (currentUser.isPermitted("lightsaber:weild")) {
            log.info("You may use a lightsaber ring.  Use it wisely.");
        } else {
            log.info("Sorry, lightsaber rings are for schwartz masters only.");
        }

获取当前登录帐号的信息,判断当前帐号的的角色和所对应的权限