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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值