在shiro.ini 中配置的结点urls可能是shiro中处理web项目比较核心的部分,在这里边配置各个过滤器的规则。
如果你想使用需要在web.xml中配置
ShiroFilter org.apache.shiro.web.servlet.ShiroFilter ShiroFilter /*
可以动态的设置shiro.ini的路径,一种是写到配置文件中,一种是使用程序加载。
在web.xml中可以动态配置shiro.ini的位置
示例如下:
ShiroFilter org.apache.shiro.web.servlet.IniShiroFilter configPath /WEB-INF/anotherFile.ini
还有一种方式是通过应用程序加载
根据示例中的spring-hibernate
配置
设置filterChainDefinitions属性,就可以将设置中的值动态的加载到对应的INI类中。也可以实现加载配置过滤器的效果。
shiro.ini的[urls]节点
# [main], [users] and [roles] above here...[urls]...
节点下的配置信息如下格式
URL_Ant_Path_Expression = Path_Specific_Filter_Chain
示例如下:
...[urls]/index.html = anon/user/create = anon/user/** = authc/admin/** = authc, roles[administrator]/rest/** = authc, rest/remoting/rpc/** = authc, perms["remote:invoke"]
指定对应的url执行的过滤器链。
如果出现下面的这种过滤情况
/account/** = ssl, authc/account/signup = anon
则下面的默认永远都不执行,即访问/account/signup/index.html 的时候,只执行上面的过滤器,不执行下面的。
如果shiro提供的过滤器不能满足要求,则可以使用自定义的过滤器,设置的规则如下:
[main]...myFilter = com.company.web.some.FilterImplementationmyFilter.property1 = value1...[urls].../some/path/** = myFilter
shiro中默认的过滤器
过滤器名称 | 过滤器类 | 描述 |
anon | 匿名过滤器 | |
authc | 如果继续操作,需要做对应的表单验证否则不能通过 | |
authcBasic | 基本http验证过滤,如果不通过,跳转屋登录页面 | |
logout | 登录退出过滤器 | |
noSessionCreation | 没有session创建过滤器 | |
perms | 权限过滤器 | |
port | 端口过滤器,可以设置是否是指定端口如果不是跳转到登录页面 | |
rest | http方法过滤器,可以指定如post不能进行访问等 | |
roles | 角色过滤器,判断当前用户是否指定角色 | |
ssl | 请求需要通过ssl,如果不是跳转回登录页 | |
user | 如果访问一个已知用户,比如记住我功能,走这个过滤器 |
当然也可以设置过滤器的使用或者禁用:
对应的设置如下:
[main]...# configure Shiro's default 'ssl' filter to be disabled while testing:ssl.enabled = false[urls].../some/path = ssl, authc/another/path = ssl, roles[admin]...
基本表单登录
[main]authc.loginUrl = /login.jsp[urls]# your login form page here:login.jsp = authc
在login.jsp中
在不连接数据库的情况下
[main]...authc.loginUrl = /whatever.jspauthc.usernameParam = somethingOtherThanUsernameauthc.passwordParam = somethingOtherThanPasswordauthc.rememberMeParam = somethingOtherThanRememberMe...