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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章