Shiro框架(二)-ini文件详解

shiro.ini文件说明:

ini即InitializationFile,指初始文件。

在前文Shiro概述中介绍过,Shiro可以用来管理最简单的移动应用程序,也可以用来管理复杂的企业项目(不了解Shiro基本知识的可以点击这里查看:Shiro框架(一)-Shiro概述),那么在简单的程序中,我们很多时候都不需要使用到数据库,那么怎么获取数据呢?shiro.ini文件满足了我们的需求,在不连接数据库的情况下,我们可以在shiro.ini文件中配置静态数据。

shiro.ini文件的组成部分:

shiro.ini文件由四个部分组成:

1、[main]:定义全局变量信息

内置SecurityManager对象,操作对象时,在[main]里写数据。

[main]
securityManager.属性=值
property=xxx
securityManager.对象属性=$property

2、[users]:定义用户名和密码信息

[users]
# 定义用户名为xxx,密码为123
xxx=123
# 定义用户名为xxx,密码为123,同时具有a角色和b角色
xxx=123,a,b

3、[roles]:定义角色信息

[roles]
a=权限1,权限2
b=权限3,权限4

4、[urls]:定义路径信息生效,在web应用中使用

[urls]
# urlpath=内置Filter或自定义Filter
# 访问时出现/login的urlpath必须要验证,支持authc对应的Filter
/login=authc
# 任意的urlpath都不需要进行认证
/**=anno
# 所有的功能都必须保证用户已登录
/**=user
# url xxx访问时必须保证用户拥有角色a和角色b
/xxx=roles["a,b"]

案例:
# 只要是以/login.html开始的路径都放行
/login.html=anon
#所有的路径都放行
/**=anon
# 所有url必须认证通过后才能放行
/**=authc
# 匹配规则:只要满足一个规则就通过,因此如果要是用/**=anon,那么/**=anon要放在最后

Shiro过滤器:

过滤器名称过滤器类描述信息
anonorg.apache.shiro.web.filter.authc.AnonymousFilter匿名过滤器
authcorg.apache.shiro.web.filter.authc.FormAuthenticationFilter如果继续操作,需要做对应的表单验证,否则不能通过
authcBasicorg.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter基本http验证过滤,如果不通过,跳转到登录页面
logoutorg.apache.shiro.web.filter.authc.LogoutFilter登录退出过滤器
noSessionCreationorg.apache.shiro.web.filter.authc.NoSessionCreationFilter没有session创建过滤器
permsorg.apache.shiro.web.filter.authc.PermissionAuthorizationFilter权限过滤器
portorg.apache.shiro.web.filter.authc.PortFilter端口过滤器,可以设置是否是指定端口,如果不是
restorg.apache.shiro.web.filter.authc.HttpMethodPermissionFilterhttp方法过滤器,可以指定如post不能进行访问等
rolesorg.apache.shiro.web.filter.authc.RolesAuthorizationFilter

角色过滤器,判断当前用户是否指定角色

sslorg.apache.shiro.web.filter.authc.SslFilter请求需要通过ssl,如果不是跳转返回登录页
userorg.apache.shiro.web.filter.authc.UserFilter如果访问一个已用用户,比如记住我功能,需要走这一个过滤器

 


版权声明:本文为threelifeadv原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。