apache shiro的urls配置

来源:互联网 发布:php floor 编辑:程序博客网 时间:2024/04/28 16:57

shiro.ini 中配置的结点urls可能是shiro中处理web项目比较核心的部分,在这里边配置各个过滤器的规则。

如果你想使用需要在web.xml中配置

?
<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
 
<filter-mapping>
    <filter-name>ShiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

可以动态的设置shiro.ini的路径,一种是写到配置文件中,一种是使用程序加载。

在web.xml中可以动态配置shiro.ini的位置

示例如下:

?
<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.IniShiroFilter</filter-class>
    <init-param>
        <param-name>configPath</param-name>
        <param-value>/WEB-INF/anotherFile.ini</param-value>
    </init-param>
</filter>

还有一种方式是通过应用程序加载

根据示例中的spring-hibernate

配置

?
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

设置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.FilterImplementation
myFilter.property1 = value1
...
 
[urls]
...
/some/path/** = myFilter

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.session.NoSessionCreationFilter没有session创建过滤器permsorg.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter权限过滤器portorg.apache.shiro.web.filter.authz.PortFilter端口过滤器,可以设置是否是指定端口如果不是跳转到登录页面restorg.apache.shiro.web.filter.authz.HttpMethodPermissionFilterhttp方法过滤器,可以指定如post不能进行访问等rolesorg.apache.shiro.web.filter.authz.RolesAuthorizationFilter角色过滤器,判断当前用户是否指定角色sslorg.apache.shiro.web.filter.authz.SslFilter请求需要通过ssl,如果不是跳转回登录页userorg.apache.shiro.web.filter.authc.UserFilter如果访问一个已知用户,比如记住我功能,走这个过滤器

 当然也可以设置过滤器的使用或者禁用:

对应的设置如下:

?
[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中

?
<form ...>
 
   Username: <input type="text" name="username"/> <br/>
   Password: <input type="password" name="password"/>
   ...
   <input type="checkbox" name="rememberMe" value="true"/>Remember Me?
   ...
</form>

在不连接数据库的情况下

?
[main]
...
authc.loginUrl = /whatever.jsp
authc.usernameParam = somethingOtherThanUsername
authc.passwordParam = somethingOtherThanPassword
authc.rememberMeParam = somethingOtherThanRememberMe
...