shiro(1)-简介

简介: 简介 apache shiro 是一个功能强大和易于使用的Java安全框架,为开发人员提供一个直观而全面的的解决方案的认证,授权,加密,会话管理。 在实际应用中,它实现了应用程序的安全管理的各个方面。

简介

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();

 

目录
相关文章
|
6月前
|
缓存 算法 安全
快速学会如何使用Shiro
快速学会如何使用Shiro
85 2
|
6月前
|
缓存 安全 Java
【Shiro】入门概述
1.是什么 Apache Shiro 是一个功能强大且易于使用的 Java 安全(权限)框架。Shiro 可以完 成:认证、授权、加密、会话管理、与 Web 集成、缓存 等。借助 Shiro 您可以快速轻松 地保护任何应用程序——从最小的移动应用程序到最大的 Web 和企业应用程序。
68 0
|
9月前
|
存储 缓存 安全
【Shiro】第二章 Shiro概述
【Shiro】第二章 Shiro概述
61 0
|
9月前
|
缓存 数据库 数据安全/隐私保护
【Shiro】第三章 Shiro入门(四)
【Shiro】第三章 Shiro入门(四)
55 0
|
9月前
|
存储 算法 程序员
【Shiro】第三章 Shiro入门(二)
【Shiro】第三章 Shiro入门(二)
55 1
|
9月前
|
存储 算法 数据安全/隐私保护
【Shiro】第三章 Shiro入门(三)
【Shiro】第三章 Shiro入门(三)
60 1
|
9月前
|
安全 数据安全/隐私保护
【Shiro】第三章 Shiro入门(一)
【Shiro】第三章 Shiro入门
53 1
|
9月前
|
SQL 存储 缓存
Shiro安全框架简介
基本上只要涉及到用户参数的系统都要进行权限管理,使用权限管理实现了对用户访问系统的控制,不同的用户访问不同的资源。按照安全规则或者安全策略控制用户访问资源,而且只能访问被授权的资源 权限管理包括认证和授权两部分,当用户访问资源时先对其进行身份的认证,认证通过后即可访问已经授权的资源。
52 0
|
9月前
|
存储 缓存 安全
Shiro学习之Shiro简介
Shiro学习之Shiro简介
71 0
|
存储 数据采集 缓存
Shiro简介
.什么是Shiro - .ApacheShiro 是一个Java的安全(权限)框架。 - .Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境。 - .Shiro可以完成,认证,授权,加密,会话管理,Web集成,缓存等。
Shiro简介

热门文章

最新文章