shiro(1)-简介

时间:2022-05-04
本文章向大家介绍shiro(1)-简介,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

简介

apache shiro 是一个功能强大和易于使用的Java安全框架,为开发人员提供一个直观而全面的的解决方案的认证,授权,加密,会话管理。

在实际应用中,它实现了应用程序的安全管理的各个方面。

shiro的功能

apache shiro能做什么?

支持认证跨一个或多个数据源(LDAP,JDBC,kerberos身份等)

执行授权,基于角色的细粒度的权限控制。

增强的缓存的支持。

支持web或者非web环境,可以在任何单点登录(SSO)或集群分布式会话中使用。

主要功能是:认证,授权,会话管理和加密。

下载并且使用

1,确保系统内安装JDK1.5+和maven2.2+。

2,到shiro主页下载shiro.

3,解压缩

unzip shiro-root-1.1.0-source-release.zip

4,进入到quickstart目录

cd shiro-root-1.1.0/samples/quickstart

5,运行quickstart

 mvn compile exec:java

执行完成如下图:

Quickstart.java

 // get the currently executing user:
 Subject currentUser = SecurityUtils.getSubject();

使用SecurityUtils.getSubject(),我们可以得到当前正在执行的主题。

得到主题之后,你可以得到他对应的会话信息

 // Do some stuff with a Session (no need for a web or EJB container!!!)
        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 + "]");
        }

你可以得到http的session信息,也可以在非web环境中使用,得到相对应的会话信息。

如果在web应用程序中部署应用,默认情况下,应用将以HttpSession为基础。在企业级应用中,你在多个应用中可以使用相同的API,无论部署环境。而且使用任何客户端技术你都可以共享会话数据。

接下来判断登录信息

 // let's login the current user so we can check against roles and permissions:
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
            token.setRememberMe(true);
            try {
                currentUser.login(token);
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of " + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
            }
            // ... catch more exceptions here (maybe custom ones specific to your application?
            catch (AuthenticationException ae) {
                //unexpected condition?  error?
            }
        }

如果正确可以向下执行,如果不正确,就会对不同的业务进行处理。

比如用户名不正确,密码不正确,用户被锁定的异常,当然也可以使用自定义抛出的异常。

如果登录成功,那么下一步可以做什么呢?

提示当前用户:

//say who they are:
        //print their identifying principal (in this case, a username):
        log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

接着测试是否还有其它角色

//test a role:
        if (currentUser.hasRole("schwartz")) {
            log.info("May the Schwartz be with you!");
        } else {
            log.info("Hello, mere mortal.");
        }

接着测试是否有特定的权限

//test a typed permission (not instance-level)
        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.");
        }

接着验证一个非常强大的实例级权限

 //a (very powerful) Instance Level permission:
        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!");
        }

最后是使用程序注销:

//all done - log out!
        currentUser.logout();