shiro(5)-重点研究的urls配置

时间:2022-05-05
本文章向大家介绍shiro(5)-重点研究的urls配置,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在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中默认的过滤器

过滤器名称

过滤器类

描述

anon

org.apache.shiro.web.filter.authc.AnonymousFilter

匿名过滤器

authc

org.apache.shiro.web.filter.authc.FormAuthenticationFilter

如果继续操作,需要做对应的表单验证否则不能通过

authcBasic

org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

基本http验证过滤,如果不通过,跳转屋登录页面

logout

org.apache.shiro.web.filter.authc.LogoutFilter

登录退出过滤器

noSessionCreation

org.apache.shiro.web.filter.session.NoSessionCreationFilter

没有session创建过滤器

perms

org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter

权限过滤器

port

org.apache.shiro.web.filter.authz.PortFilter

端口过滤器,可以设置是否是指定端口如果不是跳转到登录页面

rest

org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

http方法过滤器,可以指定如post不能进行访问等

roles

org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

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

ssl

org.apache.shiro.web.filter.authz.SslFilter

请求需要通过ssl,如果不是跳转回登录页

user

org.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
...