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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值