ITPub博客

首页 > 应用开发 > IT综合 > shiro 的INI配置

shiro 的INI配置

IT综合 作者:xz43 时间:2016-05-18 22:25:59 0 删除 编辑

一、概述

INI配置文件是一种key/value的键值对配置,提供了分类的概念,每一个类中的key不可重复。在这个示例中我们使用一个INI文件来配置Shiro SecurityManager,首先,在pom.xml同目录中创建一个src/main/resources子目录,在该子目录中创建一个shiro.ini文件,内容如下:

点击(此处)折叠或打开

  1. # =======================
  2. # Shiro INI 配置
  3. # =======================
  4. [main]
  5. # 对象和它们的属性在这里定义
  6. # 例如 SecurityManager, Realms 等。
  7. [users]
  8. # 用户在这里定义,如果只是一小部分用户。(实际使用中,使用这种配置显然不合适)
  9. [roles]
  10. # 角色在这里定义,(实际应用中也不会在这里定义角色,一般都是存储于数据库中)
  11. [urls]
  12. # web系统中,基于url的权限配置,web章节会介绍。

二、详细介绍

[main]部分

主要是SecurityManager及其依赖项的配置,例如Realm。
下边是main的一个配置的例子。
		

点击(此处)折叠或打开

  1. [main]
  2. sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
  3. myRealm = com.company.security.shiro.DatabaseRealm
  4. myRealm.connectionTimeout = 30000
  5. myRealm.username = jsmith
  6. myRealm.password = secret
  7. myRealm.credentialsMatcher = $sha256Matcher
  8. securityManager.sessionManager.globalSessionTimeout = 1800000
定义对象:
		
  1. 点击(此处)折叠或打开

    1. [main]
    2. myRealm = com.company.shiro.realm.MyRealm
    3. ...
设置对象的值:
	
  1. 点击(此处)折叠或打开

    1. ...
    2. myRealm.connectionTimeout = 30000
    3. myRealm.username = jsmith
    4. ...
上面的配置会转化为方法的调用
		
  1. 点击(此处)折叠或打开

    1. ...
    2. myRealm.setConnectionTimeout(30000);
    3. myRealm.setUsername("jsmith");
    4. ...
对象的注入:
		
  1. 点击(此处)折叠或打开

    1. ...
    2. sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
    3. ...
    4. myRealm.credentialsMatcher = $sha256Matcher
    5. ...
会调用myRealm.setCredentialsMatcher(sha256Matcher),完成对象的注入。

嵌套属性赋值:
		

点击(此处)折叠或打开

  1. ...
  2. securityManager.sessionManager.globalSessionTimeout = 1800000
  3. ...
转化为java代码securityManager.getSessionManager().setGlobalSessionTimeout(1800000);

数组类型注入:

		
  1. 点击(此处)折叠或打开

    1. # 'cipherKey' 是数组类型的默认使用Base64编码
    2. securityManager.rememberMeManager.cipherKey = kPH+bIxk5D2deZiIxcaaaA==
    3. ...
也可以设置为以0x开头的十六进制
		
  1. securityManager.rememberMeManager.cipherKey = 0x3707344A4093822299F31D008

集合类型的注入:

Array/Set/List setter,多个值通过逗号分隔

		
  1. 点击(此处)折叠或打开

    1. sessionListener1 = com.company.my.SessionListenerImplementation
    2. ...
    3. sessionListener2 = com.company.my.other.SessionListenerImplementation
    4. ...
    5. 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.  
  6. anObject.mapProperty = key1:$object1, key2:$object2
key也可以是引用类型的。
		
  1. 点击(此处)折叠或打开

    1. anObject.map = $objectKey1:$objectValue1, $objectKey2:$objectValue2
    2. ...

注入顺序:

后边的会覆盖前面的值。
		

点击(此处)折叠或打开

  1. ...
  2. myRealm = com.company.security.MyRealm
  3. ...
  4. myRealm = com.company.security.DatabaseRealm
  5. ...

默认值:

在上面的例子中,我们没有定义SecurityManager却可以对它赋值,这是因为shiro会创建一个SecurityManager默认实现,而对于用户来说,无需关注,如果你要显示的指定自定义的SecurityManager也是可以的。
		

点击(此处)折叠或打开

  1. ...
  2. securityManager = com.company.security.shiro.MyCustomSecurityManager
  3. ...

[users]

用户账户的静态配置,例如配置用户名/密码以及角色
		

点击(此处)折叠或打开

  1. [users]
  2. admin = 123
  3. lonestarr = 123, role1, role2
  4. darkhelmet = 123, role1, role2
默认规则
		
  1. username = password, roleName1, roleName2, ..., roleNameN
密码的加密
	

点击(此处)折叠或打开

  1. [main]
  2. ...
  3. sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
  4. ...
  5. iniRealm.credentialsMatcher = $sha256Matcher
  6. ...
  7. [users]
  8. # user1 = sha256-hashed-hex-encoded password, role1, role2, ...
  9. user1 = 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b, role1, role2, ...
  1. 如果你想让你的密码是base64编码,则如下配置。
		

点击(此处)折叠或打开

  1. [main]
  2. ...
  3. # true = hex, false = base64:
  4. sha256Matcher.storedCredentialsHexEncoded = false

[roles]

		

点击(此处)折叠或打开

  1. [roles]
  2. # 'admin' 拥有所有权限
  3. admin = *
  4. # 'schwartz' 拥有lightsaber下所有权限
  5. schwartz = lightsaber:*
  6. # 'goodguy' 拥有winnebago下的'drive' 下的 'eagle5'权限
  7. goodguy = winnebago:drive:eagle5
规则
			
  1. rolename = permissionDefinition1, permissionDefinition2, ..., permissionDefinitionN
更多请参考授权章节

[urls]

配置url及相应的拦截器之间的关系,格式:“url=拦截器[参数],拦截器[参数],如:
		

点击(此处)折叠或打开

  1. [urls]
  2. /admin/** = authc, roles[admin], perms["permission1"]
具体参考web章节

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9399028/viewspace-2102780/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
鱼儿的学习空间

注册时间:2010-11-16

  • 博文量
    433
  • 访问量
    1863708