一,Shiro概述(1)

来源:互联网 发布:set 协议属于网络 编辑:程序博客网 时间:2024/04/30 11:31

1,什么是Apache Shiro?

Apache Shiro是一个强大而灵活的开源框架,通常用来处理认证,授权,企业会话管理和加密等。利用Apache Shiro可以实现以下功能:
  • 鉴定用户的识别身份
  • 控制用户的访问权限
  • 在任何环境中使用session API,即使没有WEB或是EJB容器
  • 在认证,访问控制,session管理期间,对事件做出响应

2,Apache Shiro features

shiro模块功能

  • Authentication:认证,即根据用户提供的凭据,用户是否能够登陆到网站中去。
  • Authorization:授权,用户是否拥有权限去访问系统资源。
  • Session Management:会话管理,管理用户特定的会话。
  • Cryptography:加密,能够对用户输入的数据进行base64或者是MD5加密。
  • Web Support:web环境支持。
  • Caching:缓存。
  • Concurrency:利用并发的特性来支持多线程的应用。
  • Testing:能够进行单元测试。
  • Run As:该功能允许用户假装成另一个用户来运行,在该用户允许的情况下。
  • Remember Me:记住我。

3, 第一个shiro应用程序


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Tutorial {

private static final transient Logger log = LoggerFactory.getLogger(Tutorial.class);public static void main(String[] args) {    log.info("My First Apache Shiro Application");    /*     * 使用IniSecurityManagerFactory去提取位于classpath下面的shiro.ini文件,     * classpath前缀是个统一资源定位符,告诉shiro到什么位置下加载文件     */    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");    //解析 INI 文件并返回反映该配置的 SecurityManager 实例。    SecurityManager securityManager = factory.getInstance();    /*     * 我们将SecurityManager设置成静态的单利模式,能够跨虚拟机访问,     * 但是在单个虚拟机中存在多个shiro应用程序这种模式是不可取的,     * 在这样的情况下,我们通常将SecurityManager放在应用程序特定的容器中。     * 例如web应用的ServletContext,spring,Guice或者是JBoss依赖注入的实例容器中     */    SecurityUtils.setSecurityManager(securityManager);    //通过SecurityUtils获得当前执行的主体    Subject currentUser = SecurityUtils.getSubject();    Session session = currentUser.getSession();    session.setAttribute("someKey", "aValue");    String value = (String)session.getAttribute("someKey");    if(value.equals("aValue")){        log.info("取得session中的数据:" + value);    }    if(!currentUser.isAuthenticated()){        UsernamePasswordToken token = new UsernamePasswordToken("lonestarr","vespa");        token.setRememberMe(true);        try{            currentUser.login(token);        }catch(UnknownAccountException e){            log.info("用户名不存在!");        }catch (IncorrectCredentialsException e) {            log.info("密码错误!");        }catch (LockedAccountException e) {            log.info("用户被锁定!");        }    }    log.info("用户["+currentUser.getPrincipal()+"]登录成功!");    //检查当前用户是否有某个具体角色    if (currentUser.hasRole("schwartz")) {        log.info("拥有Schwartz角色!");    } else {        log.info("没有Schwartz角色!");    }    //检查当前用户是否有某个具体权限    if (currentUser.isPermitted("lightsaber:weild")) {        log.info("拥有lightsaber:weild权限.");    } else {        log.info("没有lightsaber:weild权限.");    }    //检查当前用户是否有某个具体权限    if (currentUser.isPermitted("winnebago:drive:eagle5")) {        log.info("拥有winnebago:drive:eagle5权限.");    } else {        log.info("没有winnebago:drive:eagle5权限");    }    //用户退出    currentUser.logout();    System.exit(0);}

}

0 0