简单登录流程:

1.  SecurityManager  
2.  SecurityUtils.setSecurityManager

3.  SecurityUtils.getSubject    
4.  token(UsernamePasswordToken等) 
5.  subject.login

Apache Shiro Configuration

Shiro 配置--ini

》适用于用户少且不需要在运行时动态创建的情景
1. web.xml 中自定义shiro.ini位置(默认位置)
  1. /WEB-INF/shiro.ini or classpath:shiro.ini
    配置内容
  1. <filter>
  2. <filter-name>ShiroFilter</filter-name>
  3. <filter-class>org.apache.shiro.web.servlet.IniShiroFilter</filter-class>
  4. <init-param>
  5. <param-name>configPath</param-name>
  6. <param-value>/WEB-INF/anotherFile.ini</param-value>
  7. </init-param>
  8. </filter>
2. Shiro.ini 示例
  1. # =======================
  2. # Shiro INI configuration
  3. # =======================
  4. [main]
  5. # Objects and their properties are defined here,
  6. # Such as the securityManager, Realms and anything
  7. # else needed to build the SecurityManager
  8. [users]
  9. # The 'users' section is for simple deployments
  10. # when you only need a small number of statically-defined
  11. # set of User accounts.
  12. [roles]
  13. # The 'roles' section is for simple deployments
  14. # when you only need a small number of statically-defined
  15. # roles.
  16. [urls]
  17. # The 'urls' section is used for url-based security
  18. # in web applications. We'll discuss this section in the
  19. # Web documentation


说明:


[main]  主配置

configure the application's SecurityManager instance and any of its dependencies, such as Realms.
配置SecurityManager 及其依赖,如Realms(安全数据源).
  1. [main]
  2. sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher # 加密密码匹配
  3. # true = hex, false = base64:
  4. sha256Matcher.storedCredentialsHexEncoded = false
  5. myRealm = com.company.security.shiro.DatabaseRealm
  6. myRealm.connectionTimeout = 30000
  7. myRealm.username = jsmith # 内部被转换为 myRealm.setUsername("jsmith");
  8. myRealm.password = secret
  9. myRealm.credentialsMatcher = $sha256Matcher # 凭证(密码)加密
  10. securityManager.sessionManager.globalSessionTimeout = 1800000

多个属性(逗号 ,)
  1. securityManager.sessionManager.sessionListeners = $sessionListener1, $sessionListener2
Map形式属性设置
  1. object1 = com.company.some.Class
  2. object2 = com.company.another.Class
  3. ...
  4. anObject = some.class.with.a.Map.property
  5. anObject.mapProperty = key1:$object1, key2:$object2
重复设置的属性,后写的覆盖前面的
  1. myRealm = com.company.security.MyRealm
  2. ...
  3. myRealm = com.company.security.DatabaseRealm #(覆盖了前面的myRealm)


[users] 用户配置

  1. [users]
  2. admin = secret # A password is required.
  3. lonestarr = vespa, goodguy, schwartz
  4. darkhelmet = ludicrousspeed, badguy, schwartz
  5. # user1 = sha256-hashed-hex-encoded password, role1, role2, ... # 可以使用 shiro 的Command Line Hasher 来加密密码。需要配置 credentialsMatcher
格式: 用户 = 密码,角色1,角色2,... ,角色N
  1. username = password, roleName1, roleName2, ..., roleNameN


[roles] 角色配置

  1. [roles]
  2. # 'admin' role has all permissions, indicated by the wildcard '*'
  3. admin = * # 所有权限
  4. # The 'schwartz' role can do anything (*) with any lightsaber:
  5. schwartz = lightsaber:* # 角色 schwartz 拥有对资源 lightsaber 做任何事的权限
  6. # The 'goodguy' role is allowed to 'drive' (action) the winnebago (type) with
  7. # license plate 'eagle5' (instance specific id)
  8. goodguy = winnebago:drive:eagle5 # 角色 goodguy 拥有对 id 为 eagle5 的 winnebago 资源执行 drive 的权限
格式:角色 = 权限定义1,权限定义2,... ,权限定义N
  1. rolename = permissionDefinition1, permissionDefinition2, ..., permissionDefinitionN

权限定义:http://shiro.apache.org/permissions.html


[url] web应用中url过滤

  1. [urls]
  2. /index.html = anon
  3. /user/create = anon
  4. /account/**=ssl,authc # Any request to my application's path of /account or any of it's sub paths (/account/foo, /account/bar/baz, etc) will trigger the 'ssl, authc' filter chain
  5. /user/** = authc
  6. /admin/** = authc, roles[administrator]
  7. /rest/** = authc, rest
  8. /remoting/rpc/** = authc, perms["remote:invoke"]
格式:
  1. URL_Ant_Path_Expression = Path_Specific_Filter_Chain
注意:
1. 所有URL都是相对于 HttpServletRequest.getContextPath()  值而言的
2. URL过滤原则是 FIRST MATCH WINS.  后面的不会覆盖前面的!
url中过滤器(anno等)定义:
  1. filter1[optional_config1], filter2[optional_config2], ..., filterN[optional_configN] # 如 authc, perms["remote:invoke"]
注意:若filter不是shiro中已定义的(DefaultFilter),而是自己继承 PathMatchingFilter 实现的,则需要在【main】中先声明
测试时,可以在【main】中禁用 filter
  1. [main]
  2. ...
  3. # configure Shiro's default 'ssl' filter to be disabled while testing:
  4. ssl.enabled = false

登录请求login.do不能设为authc

Java安全框架 Apache Shiro学习-1-ini 配置的更多相关文章

  1. Apache Shiro学习-2-Apache Shiro Web Support

     Apache Shiro Web Support  1. 配置 将 Shiro 整合到 Web 应用中的最简单方式是在 web.xml 的 Servlet ContextListener 和 Fil ...

  2. Apache Shiro 学习记录5

    本来这篇文章是想写从Factory加载ini配置到生成securityManager的过程的....但是貌似涉及的东西有点多...我学的又比较慢...很多类都来不及研究,我又怕等我后面的研究了前面的都 ...

  3. apache shiro学习笔记

    一.权限概述 1.1 认证与授权 认证:系统提供的用于识别用户身份的功能,通常登录功能就是认证功能-----让系统知道你是谁?? 授权:系统授予用户可以访问哪些功能的许可(证书)----让系统知道你能 ...

  4. Apache shiro学习总结

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  5. Apache Shiro 学习记录2

    写完上篇随笔以后(链接).....我也想自己尝试一下写一个Strategy.....Shiro自带了3个Strategy,教程(链接)里作者也给了2个.....我想写个都不一样的策略.....看来看去 ...

  6. Apache Shiro 学习记录1

    最近几天在学习Apache Shiro......看了一些大神们的教程.....感觉收获不少.....但是毕竟教程也只是指引一下方向....即使是精品教程,仍然有很多东西都没有说明....所以自己也稍 ...

  7. 《Shiro框架》shiro学习中报错解决方法

    [1] 最近在学习shiro,在学习过程中出现了一个问题,报错如下: org.apache.shiro.UnavailableSecurityManagerException: No Security ...

  8. Apache Shiro(六)-基于URL配置权限

    数据库 先准备数据库啦. DROP DATABASE IF EXISTS shiro; CREATE DATABASE shiro DEFAULT CHARACTER SET utf8; USE sh ...

  9. 权限框架Apache Shiro 和 Spring Security

    Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势.Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证.授 ...

随机推荐

  1. Adobe Acrobat 9 Pro破解方法

    首先安装Adobe Acrobat 9 Pro,默认安装在C盘. 如果你的系统盘是C盘,那么就删除:c:/Documents and Settings/All Users/Application Da ...

  2. PHP 读取和导出 CSV文件

    PHP 读取和导出 CSV文件,速度比phpexcel快80%,而phpexcel 占内存,在数据大的情况下,5万条只需几秒不到,几乎感觉不出来 如果遇到数字是科学计算法,可以在前面加一个 ' 单引号 ...

  3. IO流-文件夹的拷贝

    文件夹的拷贝操作 要求: 完成文件夹的拷贝,包括子目录的拷贝和所有文件的拷贝 分析: 首先,得在目标目录下创建一个与源文件夹名称相同的文件夹 遍历源文件夹中的所有文件对象,判断子文件是目录还是文件 如 ...

  4. 关于浏览器和IIS基础的简单理解

    浏览器 输入域名或者IP地址,按回车访问后:发生了什么??IIS是如何工作的?为什么能这么工作?? 1    浏览器和IIS 分别是两个应用程序:浏览器访问网址实际就是  两个应用程序的数据交互往来: ...

  5. bower.json 的版本范围

    bower.json 的版本范围 有小伙伴问 ~2.2.0 什么意思. 而且在git 的tags 中没有了 2.2.0 版本,怎么样? 实际上 ~2.2.0 的意思是 >=2.2.0 <2 ...

  6. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

  7. 谈谈JS中的高级函数

    博客原文地址:Claiyre的个人博客如需转载,请在文章开头注明原文地址 在JavaScript中,函数的功能十分强大.它们是第一类对象,也可以作为另一个对象的方法,还可以作为参数传入另一个函数,不仅 ...

  8. NumPy-快速处理数据--ufunc运算--广播--ufunc方法

    本文摘自<用Python做科学计算>,版权归原作者所有. 1. NumPy-快速处理数据--ndarray对象--数组的创建和存取 2. NumPy-快速处理数据--ndarray对象-- ...

  9. python2.7 + ubuntu14.4 + dlib19.7

    0.首先需要Cmake以及编译C++成python程序的工具 sudo apt-get install libboost-python-dev cmake 1.download dlib19.7 fr ...

  10. composer包php-amqplib

    php-amqplib官方文档 url:http://www.rabbitmq.com/tutorials/tutorial-one-php.html #测试demo: url: http://**. ...