Java高級_Shiro的自帶Quickstart(HelloWorld)

一、環境搭建

  1. 創建java project項目。
    這裏寫圖片描述
  2. 導入Quickstart必須的Jar包。
    shiro-all-1.3.2.jar
    log4j-1.2.15.jar
    slf4j-api-1.6.1.jar
    slf4j-log4j12-1.6.1.jar
  3. 加入log配置文件,以及用於shiro測試的配置文件。
    這裏寫圖片描述
  4. 新建包,把Quickstart.java文件放入包。
  5. 整個java環境目錄。
    這裏寫圖片描述

二、Quickstart類講解

public class Quickstart {


    public static void main(String[] args) {

        // 獲取例子配置文件
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);

        // 獲取當前得到 Subject 調用SecurityUtils.getSubject()方法。
        Subject currentUser = SecurityUtils.getSubject();

        // 測試使用Session
        // 獲取Session 調用subject的getSession()方法
        Session session = currentUser.getSession();
        session.setAttribute("someKey", "aValue");
        String value = (String) session.getAttribute("someKey");
        if (value.equals("aValue")) {
            System.out.println("==================>>>>>>>>>>>Retrieved the correct value! [" + value + "]");
        }

        // 測試當前的用戶已經被認證,既用戶是否登錄,調用 subject的isAuthenticated()。
        if (!currentUser.isAuthenticated()) {
            // 把用戶名和密碼封裝爲UsernamePasswordToken對象
            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
            // RememberMe
            token.setRememberMe(true);
            try {
                // 執行登錄
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                // 沒有指定用戶,shiro 拋出UnknownAccountException
                System.out.println("==================>>>>>>>>>>>There is no user with username of " + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {
                // 密碼不對,shiro 拋出IncorrectCredentialsException
                System.out.println("==================>>>>>>>>>>>Password for account " + token.getPrincipal() + " was incorrect!");
            } catch (LockedAccountException lae) {
                // 用戶被鎖定。shiro拋出LockedAccountException
                System.out.println("==================>>>>>>>>>>>The account for username " + token.getPrincipal() + " is locked.Please contact your administrator to unlock it.");
            } catch (AuthenticationException ae) {
                // 所有認證是異常的總類
            }
        }
        System.out.println("==================>>>>>>>>>>>User [" + currentUser.getPrincipal() + "] logged in successfully.");

        // 測試使用有這個角色
        if (currentUser.hasRole("schwartz")) {
            System.out.println("==================>>>>>>>>>>>May the Schwartz be with you!");
        } else {
            System.out.println("==================>>>>>>>>>>>Hello, mere mortal.");
        }

        // 測試用戶是否具備某個行爲(權限)
        if (currentUser.isPermitted("lightsaber:weild")) {
            System.out.println("==================>>>>>>>>>>>You may use a lightsaber ring.  Use it wisely.");
        } else {
            System.out.println("==================>>>>>>>>>>>Sorry, lightsaber rings are for schwartz masters only.");
        }

        // 測試用戶是否具備某個行爲(這個比較具體)
        if (currentUser.isPermitted("winnebago:drive:eagle5")) {
            System.out.println("==================>>>>>>>>>>>You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.Here are the keys - have fun!");
        } else {
            System.out.println("==================>>>>>>>>>>>Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
        }

        // 執行登出,調用subject的logout()
        currentUser.logout();

        System.exit(0);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章