INI配置說明
- 1、對象名=全限定類名相對於調用public 無參構造器創建對象
- 2、對象名.屬性名=值相當於調用setter方法設置常量值
- 3、對象名.屬性名=$對象引用相當於調用setter方法設置對象引用
[main]
提供了對根對象securityManager及其依賴的配置
securityManager=org.apache.shiro.mgt.DefaultSecurityManager
securityManager.realms=$jdbcRealm
[users]
提供了對用戶/密碼及其角色的配置,用戶名=密碼,角色1,角色2
username=password,role1,role2
[roles]
提供了角色及權限之間關係的配置,角色=權限1,權限2
role1=permission1,permission2
[urls]
用於web,提供了對web url攔截相關的配置,url=攔截器[參數],攔截器
/index.html = anon
/admin/** = authc, roles[admin], perms["permission1"]
[main]部分
提供了對根對象securityManager及其依賴對象的配置。
創建對象
securityManager=org.apache.shiro.mgt.DefaultSecurityManager
其構造器必須是public 空參構造器,通過反射創建相應的實例。
常量值 setter注入
dataSource.driverClassName=com.mysql.jdbc.Driver
jdbcRealm.permissionsLookupEnabled=true
會自動調用jdbcRealm.setPermissionsLookupEnabled(true),對於這種常量值會自動類型轉
換。
對象引用setter注入
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
authenticator.authenticationStrategy=$authenticationStrategy
securityManager.authenticator=$authenticator
會自動通過securityManager.setAuthenticator(authenticator)注入引用依賴。
嵌套屬性 setter注入
securityManager.authenticator.authenticationStrategy=$authenticationStrategy
也支持這種嵌套方式的setter注入。
byte數組setter注入
base64 byte[]
authenticator.bytes=aGVsbG8=
hex byte[]
authenticator.bytes=0x68656c6c6f
默認需要使用Base64 進行編碼,也可以使用0x 十六進制。
Array/Set/List setter注入
authenticator.array=1,2,3
authenticator.set=$jdbcRealm,$jdbcRealm
多個之間通過“,”分割。
Map setter注入
authenticator.map=$jdbcRealm:$jdbcRealm,1:1,key:abc
即格式是:map=key:value,key:value,可以注入常量及引用值,常量的話都看作字符串
(即使有泛型也不會自動造型)。
實例化/注入順序
realm=Realm1
realm=Realm12
authenticator.bytes=aGVsbG8=
authenticator.bytes=0x68656c6c6f
後邊的覆蓋前邊的注入。
測試用例請參考配置文件shiro-config-main.ini。
securityManager.
[users]部分
配置用戶名/密碼及其角色,格式:“用戶名=密碼,角色1,角色2”,角色部分可省略。
如:
[users]
zhang=123,role1,role2
wang=123
#[roles]部分
配置角色及權限之間的關係,格式:“角色=權限1,權限2”;
如:
[roles]
role1=user:create,user:update
role2=*
如果只有角色沒有對應的權限,可以不配roles
[urls]部分
配置 url及相應的攔截器之間的關係,格式:“url=攔截器[參數],攔截器[參數],
如:
[urls]
/admin/** = authc, roles[admin], perms["permission1"]