【Shiro 框架总结】9 Shiro.ini详解

一、INI简介

INI配置文件是一种key/value的键值对配置,提供了分类的概念,每一个类中的key不可重复,#号代表注释

shiro.ini文件默认在/WEB-INF/ 或classpath下,shiro会自动查找,INI配置文件一般适用于用户少且不需要在运行时动态创建的情景下使用。

 

1.在web.xml中配置shiro的过滤器

(1)Shiro 1.1及以前版本配置方式

要使用Shiro必须在web.xml中配置shiro,在web.xml 中自定义shiro.ini位置(默认位置)/WEB-INF/shiro.ini or classpath:shiro.ini, 加入以下内容:

<filter>
	<filter-name>iniShiroFilter</filter-name>
	<filter-class>org.apache.shiro.web.servlet.IniShiroFilter</filter-class>
	<init-param>
		<param-name>configPath</param-name>
		<param-value>classpath:shiro.ini</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>iniShiroFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

(2)Shiro 1.2及以后版本的配置方式

从 Shiro 1.2 开始引入了Environment/WebEnvironment的概念,即由它们的实现提供相应的SecurityManager及其相应的依赖。ShiroFilter会自动找到Environment然后获取相应的依赖。

<listener>
	<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>

通过EnvironmentLoaderListener 来创建相应的WebEnvironment , 并自动绑定到ServletContext,默认使用IniWebEnvironment实现。

可以通过如下配置修改默认实现及其加载的配置文件位置

<context-param>
	<param-name>shiroEnvironmentClass</param-name>
	<param-value>org.apache.shiro.web.env.IniWebEnvironment</param-value>
</context-param>
<context-param>
	<param-name>shiroConfigLocations</param-name>
	<param-value>classpath:shiro.ini</param-value>
</context-param>

shiroConfigLocations 默认是“/WEB-INF/shiro.ini”,IniWebEnvironment 默认是先从/WEB-INF/shiro.ini加载,如果没有就默认加载classpath:shiro.ini。

 

与Spring集成

<filter>
	<filter-name>shiroFilter</filter-name>
	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	<init-param>
		<param-name>targetFilterLifecycle</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>shiroFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

DelegatingFilterProxy作用是自动到spring容器查找名字为shiroFilter(filter-name)的bean并把所有Filter的操作委托给它。然后将ShiroFilter 配置到spring容器即可

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
	<property name="securityManager" ref="securityManager"/>
    其它忽略
</bean>

2.shiro.ini示例


[main]
shiro.loginUrl = /login.jsp

securityManager.rememberMeManager.cipherKey = kPH+bIxk5D2deZiIxcaaaA==

[users]
# format: username = password, role1, role2, ..., roleN
root = secret,admin
guest = guest,guest
presidentskroob = 12345,president
darkhelmet = ludicrousspeed,darklord,schwartz
lonestarr = vespa,goodguy,schwartz

[roles]
# format: roleName = permission1, permission2, ..., permissionN
admin = *
schwartz = lightsaber:*
goodguy = winnebago:drive:eagle5

[urls]
/login.jsp = authc
/logout = logout
/account/** = authc
/remoting/** = authc, roles[b2bClient], perms["remote:invoke:lan,wan"]

ini配置中主要配置有四大类:main,users,roles,urls

[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"]

 

二、详解

1、[main]

main主要配置shiro的一些对象,例如securityManager ,Realm,authenticator,authcStrategy 等等,例如

#声明一个realm  
MyRealm1=com.shiro.mutilrealm.MyRealm1
MyRealm2=com.shiro.mutilrealm.MyRealm2
 
#配置验证器
authenticator = org.apache.shiro.authc.pam.ModularRealmAuthenticator
 
# AllSuccessfulStrategy 表示 MyRealm1和MyRealm2 认证都通过才算通过
#配置策略
#authcStrategy = org.apache.shiro.authc.pam.AllSuccessfulStrategy
authcStrategy = com.shiro.authenticationstrategy.MyAuthenticationStrategy
 
 
#将验证器和策略关联起来
authenticator.authenticationStrategy = $authcStrategy
 
 
#配置验证器所使用的Realm
authenticator.realms=$MyRealm2,$MyRealm1
 
#把Authenticator设置给securityManager
securityManager.authenticator = $authenticator

在web应用中,我们可以进行以下配置,表示如果用户没有登录进行访问时,将自动跳转到/login页面

authc.loginUrl=/login

2、[users]

[users]允许你配置一组静态的用户,包含用户名,密码,角色,一个用户可以有多个角色,可以配置多个角色,例如:

username = password, roleName1, roleName2, …, roleNameN

这里涉及到密码,就牵扯到加密的问题,我们可以MD5,Sha1,Sha256等算法进行加密

[main]
 
#告诉shiro我们用哪个加密算法
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
iniRealm.credentialsMatcher = $sha256Matcher
 
[users]
 
#用户名=密码,角色
admin=355b1bbfc96725cdce8f4a2708fda310a80e6d13315aec4e5eed2a75fe8032ce,role1

3、[roles]

[roles]将角色和权限关联起来,格式为:角色名=权限字符串1,权限字符串2…..,例如

role1 = printer:create,printer:query

4、[urls]

这部分配置主要在web应用中,格式为:url=拦截器[参数],拦截器[参数]……,例如

/login=anon
/unauthorized=anon
/static/**=anon
/authenticated=authc
/role=authc,roles[admin]
/permission=authc,perms["user:create"]

URL匹配模式

url 模式使用 Ant 风格模式
• Ant 路径通配符支持 ?、*、**,注意通配符匹配不包括目录分隔符“/”
(1)  ?:匹配一个字符,如 /admin? 将匹配 /admin1,但不匹配 /admin 或 /admin/
(2)   *:匹配零个或多个字符串,如 /admin 将匹配 /admin、/admin123,但不匹配 /admin/1;
(3)   **:匹配路径中的零个或多个路径,如 /admin/** 将匹配 /admin/a 或 /admin/a/b

URL匹配顺序

URL 权限采取第一次匹配优先的方式,即从头开始使用第一个匹配的 url 模式对应的拦截器链

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值