#shiro简介

一、简介

  • Apache Shiro 是一个强大而灵活的开源安全框架,可以用于应用程序的身份验证,授权,会话管理和加密。

    • Authentication:有时也简称为“登录”,这是一个证明用户是他们所说的他们是谁的行为。
    • Authorization:访问控制的过程,也就是绝对“谁”去访问“什么”。
    • Session Management:管理用户特定的会话,即使在非 Web 或 EJB 应用程序。
    • Cryptography:通过使用加密算法保持数据安全同时易于使用。

二、主要功能&附加特性

三、第一个shiro程序

1、使用说明

  • maven构建工程,log4j输出日志

  • idea终端里,当前工程目录下使用 mvn compile exec:java 命令运行程序,直接运行测试的类不会输出测试的信息

2、目录结构

3、关键文件内容及代码

  • pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.stan</groupId>
    <artifactId>shiro</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>shiro-start</name>
    <packaging>jar</packaging>
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <encoding>${project.build.sourceEncoding}</encoding>
    </configuration>
    </plugin>
    <!-- This plugin is only to test run our little application. It is not
    needed in most Shiro-enabled applications: -->
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
    <execution>
    <goals>
    <goal>java</goal>
    </goals>
    </execution>
    </executions>
    <configuration>
    <classpathScope>test</classpathScope>
    <mainClass>QuickStart</mainClass>
    </configuration>
    </plugin>
    </plugins>
    </build>
    <dependencies>
    <dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-core</artifactId>
    <version>1.1.0</version>
    </dependency>
    <!-- Shiro use SLF4J for logging. We'll use the 'simple' binding
    in this example app. See http://ww.slf4j.org for more info. -->
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.6.1</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    </project>
  • shiro配置文件
    [users]
    root = secret, admin
    guest = guest, guest
    presidentskroob = 12345, president
    darkhelmet = ludicrousspeed, darklord, schwartz
    lonestarr = vespa, goodguy, schwartz [roles]
    admin = *
    schwartz = lightsaber:*
    goodguy = winnebago:drive:eagle5
  • log4j配置文件
    log4j.rootLogger=INFO, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n
    # General Apache libraries
    log4j.logger.org.apache=WARN
    # Spring
    log4j.logger.org.springframework=WARN
    # Default Shiro logging
    log4j.logger.org.apache.shiro=TRACE
    # Disable verbose logging
    log4j.logger.org.apache.shiro.util.ThreadContext=WARN
    log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN
  • 测试类
    public class QuickStart {
    private static final transient Logger log = LoggerFactory.getLogger(QuickStart.class); public static void main(String[] args) {
    log.info("My First Apache Shiro Application");
    Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
    //get the currently executing user:
    Subject currentUser = 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 vlaue! [" + value + "]");
    }
    //let's login the current user so we can check against roles and permissions:
    if (!currentUser.isAuthenticated()) {
    UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "1223");
    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();
    System.exit(0);
    }
    }

4、总结

  • SecurityManager 不是 java.lang 包里的那个,而是 shiro框架里的,是shiro的核心组件,shiro很多功能的实现都要依赖于这个组件;
  • shiro 里的 subject 代表的是当前用户的概念

#shiro的架构&配置

一、架构

1、架构图

2、shiro各个组件的说明

  • Subject:当前与软件进行交互的实体(用户,第三方服务等)的安全特定“视图”。
  • SecurityManager:SecurityManager 是 Shiro 架构的心脏。它基本上是一个“保护伞”对象,协调其管理的组件以确保它们能够一起顺利的工作。它还管理每个应用程序用户的 Shiro 的视图,因此它知道如何执行每个用户的安全操作。
  • Authenticator:Authenticator 是一个对执行及对用户的身份验证(登录)尝试负责的组件。Authenticator 知道如何与一个或多个 Realm 协调来存储相关的用户信息,从中获得的数据被用来验证用户的身份来保证用户确实是他们所说的他们是谁。
  • Authorizer:是负责在应用程序中决定用户的访问控制的组件,是一种最终判定用户是否被允许做某事的机制。他知道如何协调多个后台数据源来访问角色特化权限信息,然后使用该信息来准确地决定用户是否被允许执行给定的动作。
  • SessionManager:SessionManager知道如何去创建及管理用户 Session 生命周期来为所有环境下的用户提供一个强健的 Session体验。Shiro 拥有能够在任何环境下本地化管理用户Session的能力,不依赖于特定的应用环境(如web环境),它将会使用它内置的企业级会话管理来提供同样的编程体验。
  • SessionDAO:代表 SessionManager 执行 Session 持久化(CRUD)操作。这允许任何数据存储被插入到会话管理的基础之中。
  • CacheManager:创建并管理其他 Shiro 组件使用的 Cache 实例生命周期,支持集成第三方的缓存框架来提高性能
  • Cryptography:shiro有很多易于使用和理解编码器实现。Shiro的加密组件简化了复杂的Java复杂的本地密码支持,使加密对于普通人也易于使用Realms:Realm 充当了 Shiro 与应用安全数据间的“桥梁”或者“连接器”。也就是说,当对用户执行认证(登录)和授权(访问控制)验证时,Shiro 会从应用配置的 Realm 中查找用户及其权限信息。从这个意义上讲,Realm 实质上是一个安全相关的 DAO:它封装了数据源的连接细节,并在需要时将相关数据提供给 Shiro 。当配置 Shiro时,你必须至少指定一个 Realm ,用于认证和(或)授权。配置多个 Realm 是可以的,但是至少需要一个。 Shiro 内置了可以连接大量安全数据源(又名目录)的 Realm,如 LDAP、关系数据库(JDBC)、类似 INI 的文本配置资源以及属性文件等。如果缺省的 Realm 不能满足需求,你还可以插入代表自定义数据源的自己的 Realm 实现

3、SecurityManger的设计

  • SecurityManager要实现的功能很多,一个类要实现这么多的功能任务会比较繁重;
  • SM实际上就是一个轻量级的容器,通过组合的设计思想将实现具体功能的各个类组合在一起进行管理,提高了框架的灵活性和可插拔性;
  • Sm及其组件都是兼容javaBean的,可以很方便配置到Spring等框架中使用,并且有很好的可插拔性;

二、核心配置

#

最新文章

  1. NodeJs:module.filename、__filename、__dirname、process.cwd()和require.main.filename
  2. Postman-进阶
  3. NodeJS实战:Express+Mongoose+ejs
  4. yum源的配置(centos6.5)
  5. ASP.NET MVC 教程-MVC简介
  6. Struts2 框架的快速搭建
  7. pycharm的安装
  8. Struts2 Convention插件的使用
  9. jquery easyui datagrid字段绑定问题
  10. TREEVIEW节点拖拽
  11. MQTT学习笔记——Yeelink MQTT维修 采用mqtt.js和paho-mqtt
  12. gradle入门(1-2)gradle的依赖管理
  13. nginx系列2:搭建nginx环境
  14. [LeetCode&amp;Python] Problem 235. Lowest Common Ancestor of a Binary Search Tree
  15. 异常来自 HRESULT:0x80070057 (E_INVALIDARG)(转)
  16. 嵌入式开发之hi3519---进程线程间的同步和互斥,条件变量、信号了、互斥锁等
  17. 【转】js 对象按照键值(不分区大小写)排序,生成签名方法
  18. springboot学习章节-spring常用配置
  19. ios开发UI篇--UILabel
  20. 一些VS2013的使用技巧(转载)

热门文章

  1. Win7系统中wmiprvse.exe占用CPU高如何解决
  2. gb2312的6763个汉字
  3. Ubuntu 安装 setuptools
  4. arcgis几何对象
  5. 合唱队形 ( 双向LIS )
  6. 层次分析法MATLAB
  7. vue中router以及route的使用
  8. Ubuntu中NS2安装详细教程
  9. 跟我一起安装jenkins
  10. 2015全球商业地产商影响力排行TOP10:中国占据5个