zoukankan      html  css  js  c++  java
  • Shiro-Quickstart

    1. Quickstart:

    public class Quickstart {
        private static final transient Logger log = LoggerFactory.getLogger(Quickstart.class);
        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 session = currentUser.getSession();
            session.setAttribute("someKey", "aValue");
            String value = (String) session.getAttribute("someKey");
            if (value.equals("aValue")) {
                log.info("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未知的账户异常
                    log.info("There is no user with username of " + token.getPrincipal());
                } catch (IncorrectCredentialsException ice) {//若账户存在,密码不匹配,shiro会抛出IncorrectCredentialsException错误的凭证异常
                    log.info("Password for account " + token.getPrincipal() + " was incorrect!");
                } catch (LockedAccountException lae) {//若账户被锁定,则抛出LockedAccountException账户锁定异常
                    log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                            "Please contact your administrator to unlock it.");
                }
                //所有认证异常的父类(总认证异常)
                catch (AuthenticationException ae) {
                    //unexpected condition?  error?
                }
            }
            //currentUser.getPrincipal()获取用户名
            log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
            //测试是否拥有某一个角色,调用Subject的hasRole()方法
            if (currentUser.hasRole("schwartz")) {
                log.info("May the Schwartz be with you!");
            } else {
                log.info("Hello, mere mortal.");
            }
            //测试拥有某一个行为,调用Subject的isPermitted()方法
            if (currentUser.isPermitted("lightsaber:wield")) {
                log.info("You may use a lightsaber ring.  Use it wisely.");
            } else {
                log.info("Sorry, lightsaber rings are for schwartz masters only.");
            }
            //测试拥有某一个更加具体行为,比如:让user对zhangsan进行selete(让用户对张三进行删除)
            if (currentUser.isPermitted("winnebago:drive:eagle5")) {
                log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                        "Here are the keys - have fun!");
            } else {
                log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
            }
            //注销注销,调用Subject的logout()方法
            currentUser.logout();
            System.exit(0);
        }
    }
  • 相关阅读:
    有关怎样入门ACM
    在线安装eclipse中html/jsp/xml editor插件(很可靠)
    如何才干高速成为优秀的程序猿
    JavaScript必知的特性(继承)
    Maven构建真正的J2EE项目
    HLJU 1223: 寻找区间和 (交替推进法)
    重要经验五:block作为属性的注意事项
    Android Studio第一次启动的Fetching android sdk component information的问题
    树莓派学习笔记——apt方式安装opencv
    大型项目开发: 隔离 (《大规模C++程序设计》书摘)
  • 原文地址:https://www.cnblogs.com/luliang888/p/11154888.html
Copyright © 2011-2022 走看看