`

[转]shrio 权限管理filterChainDefinitions过滤器配置

阅读更多
转自:http://www.cppblog.com/guojingjia2006/archive/2014/05/14/206956.html

/**

* Shiro-1.2.2内置的FilterChain

* @see =============================================================================================================================

* @see 1)Shiro验证URL时,URL匹配成功便不再继续匹配查找(所以要注意配置文件中的URL顺序,尤其在使用通配符时)

* @see   故filterChainDefinitions的配置顺序为自上而下,以最上面的为准

* @see 2)当运行一个Web应用程序时,Shiro将会创建一些有用的默认Filter实例,并自动地在[main]项中将它们置为可用

* @see   自动地可用的默认的Filter实例是被DefaultFilter枚举类定义的,枚举的名称字段就是可供配置的名称

* @see   anon---------------org.apache.shiro.web.filter.authc.AnonymousFilter

* @see   authc--------------org.apache.shiro.web.filter.authc.FormAuthenticationFilter

* @see   authcBasic---------org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter

* @see   logout-------------org.apache.shiro.web.filter.authc.LogoutFilter

* @see   noSessionCreation--org.apache.shiro.web.filter.session.NoSessionCreationFilter

* @see   perms--------------org.apache.shiro.web.filter.authz.PermissionAuthorizationFilter

* @see   port---------------org.apache.shiro.web.filter.authz.PortFilter

* @see   rest---------------org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter

* @see   roles--------------org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

* @see   ssl----------------org.apache.shiro.web.filter.authz.SslFilter

 *@see   user---------------org.apache.shiro.web.filter.authz.UserFilter

* @see =============================================================================================================================

* @see 3)通常可将这些过滤器分为两组

* @see   anon,authc,authcBasic,user是第一组认证过滤器

* @see   perms,port,rest,roles,ssl是第二组授权过滤器

* @see   注意user和authc不同:当应用开启了rememberMe时,用户下次访问时可以是一个user,但绝不会是authc,因为authc是需要重新认证的

* @see                      user表示用户不一定已通过认证,只要曾被Shiro记住过登录状态的用户就可以正常发起请求,比如rememberMe

* @see                      说白了,以前的一个用户登录时开启了rememberMe,然后他关闭浏览器,下次再访问时他就是一个user,而不会authc

* @see =============================================================================================================================

* @see 4)举几个例子

* @see   /admin=authc,roles[admin]      表示用户必需已通过认证,并拥有admin角色才可以正常发起'/admin'请求

* @see   /edit=authc,perms[admin:edit]  表示用户必需已通过认证,并拥有admin:edit权限才可以正常发起'/edit'请求

* @see   /home=user                     表示用户不一定需要已经通过认证,只需要曾经被Shiro记住过登录状态就可以正常发起'/home'请求

* @see =============================================================================================================================

* @see 5)各默认过滤器常用如下(注意URL Pattern里用到的是两颗星,这样才能实现任意层次的全匹配)

* @see   /admins/**=anon             无参,表示可匿名使用,可以理解为匿名用户或游客

* @see   /admins/user/**=authc       无参,表示需认证才能使用

* @see   /admins/user/**=authcBasic  无参,表示httpBasic认证

* @see   /admins/user/**=user        无参,表示必须存在用户,当登入操作时不做检查

* @see   /admins/user/**=ssl         无参,表示安全的URL请求,协议为https

* @see   /admins/user/**=perms[user:add:*]

* @see       参数可写多个,多参时必须加上引号,且参数之间用逗号分割,如/admins/user/**=perms["user:add:*,user:modify:*"]

* @see       当有多个参数时必须每个参数都通过才算通过,相当于isPermitedAll()方法

* @see   /admins/user/**=port[8081]

* @see       当请求的URL端口不是8081时,跳转到schemal://serverName:8081?queryString

* @see       其中schmal是协议http或https等,serverName是你访问的Host,8081是Port端口,queryString是你访问的URL里的?后面的参数

* @see   /admins/user/**=rest[user]

* @see       根据请求的方法,相当于/admins/user/**=perms[user:method],其中method为post,get,delete等

* @see   /admins/user/**=roles[admin]

* @see       参数可写多个,多个时必须加上引号,且参数之间用逗号分割,如/admins/user/**=roles["admin,guest"]

* @see       当有多个参数时必须每个参数都通过才算通过,相当于hasAllRoles()方法

* @see

http://liureying.blog.163.com/blog/static/61513520136205574873/

spring中 shiro logout 配置方式
有两种方式实现logout
1. 普通的action中 实现自己的logout方法,取到Subject,然后logout
这种需要在ShiroFilterFactoryBean 中配置 filterChainDefinitions 
对应的action的url为anon
<property name="filterChainDefinitions">
            <value>
                # some example chain definitions:
                /index.htm = anon
                /logout = anon
                /unauthed = anon
                /console/** = anon
                /css/** = anon
                /js/** = anon
                /lib/** = anon
                /admin/** = authc, roles[admin]
                /docs/** = authc, perms[document:read]
                /** = authc
                # more URL-to-FilterChain definitions here
            </value>

2. 使用shiro提供的logout filter
需要定义 相应的bean
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
        <property name="redirectUrl" value="/loginform" />
    </bean>

然后将相应的url filter配置为logout如下
<property name="filterChainDefinitions">
            <value>
                # some example chain definitions:
                /index.htm = anon
                /logout = logout
                /unauthed = anon
                /console/** = anon
                /css/** = anon
                /js/** = anon
                /lib/** = anon
                /admin/** = authc, roles[admin]
                /docs/** = authc, perms[document:read]
                /** = authc
                # more URL-to-FilterChain definitions here
            </value>

http://kdboy.iteye.com/blog/1154652
http://blog.csdn.net/peterwanghao/article/details/8084126
http://www.oschina.net/question/593111_62454
http://blog.csdn.net/shadowsick/article/details/17265625
分享到:
评论

相关推荐

    shiro 安全框架 最全中文配置文档.docx

    例如,你可以配置一个过滤器链来处理所有请求,或者根据URL模式动态创建过滤器链。这允许你在运行时更改安全策略,无需修改`web.xml`。 **配置示例**: ```xml &lt;bean id="securityManager" class="org.apache.shiro...

    SSM整合shiro实现角色权限

    Shiro提供了多种内置过滤器,如authc(认证过滤器)、perms(权限过滤器)等,可以根据需求进行配置: ```xml &lt;bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"&gt; &lt;!-- 配置...

    Spring整合Shiro做权限控制模块详细案例分析

    2. **配置Shiro**:在Spring配置文件中声明Shiro的过滤器链,定义哪些URL需要进行权限控制。例如,创建一个`shiroFilter` bean,设置过滤规则,如登录拦截器、匿名访问等。 ```xml &lt;bean id="shiroFilter" class=...

    shiro 配置文件

    `ShiroFilter` 是 Shiro 提供的核心过滤器,负责拦截请求并执行相应的安全逻辑。而 `DelegatingFilterProxy` 则是 Spring 提供的 Filter,用于代理 Spring 管理的 Bean,通常用来指向 Spring 配置中的 Shiro 相关 ...

    Spring Boot整合Shiro搭建权限管理系统.pdf

    - 创建`ShiroConfig`类,进行Shiro的配置,包括 Realm(领域对象)、过滤器链定义等。 ```java @Configuration public class ShiroConfig { // 配置Realm @Bean public MyRealm myRealm() { return new ...

    springmvc+shiro配置教程

    在Web.xml文件中,我们需要定义Shiro的过滤器,以便拦截所有的请求。以下是一个基本的Web.xml配置文件: ```xml &lt;filter-name&gt;shiroFilter &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy...

    springmvc shiro

    - 过滤器链设置:Shiro通过一系列过滤器实现安全控制,如authc(认证过滤器)、perms(权限过滤器)等。需要在web.xml中配置这些过滤器,并将它们指向Spring管理的Bean。 - Controller层集成:在Controller方法上...

    shiroFilter权限验证

    首先我们在web.xml配置的过滤器实际上是配置ShiroFilterFactoryBean,所以在这里需要将ShiroFilterFactoryBean定义为shiroFilter &lt;bean id="shiroFilter" class="org.apache.shiro.spring.web....

    shiro+springmvc整合

    此外,Shiro还支持自定义过滤器和拦截逻辑,以满足更个性化的安全需求。 总之,Apache Shiro与Spring MVC的整合为开发者提供了一种简单有效的方式来实现Web应用的安全管理。通过灵活的配置和扩展,开发者可以快速...

    单机版的shiro

    - **Filter**:Shiro 提供了一系列的安全过滤器,如 `authc`(认证过滤器)、`perms`(权限过滤器)等,它们在 Web 应用的请求处理链中起作用。 **5. 整合流程** - 创建 Realm 类,继承自 `AuthorizingRealm`,...

    shiro认证.docx

    这将创建一个名为 `shiroFilter` 的过滤器,并将所有请求委托给 Spring 管理的过滤器代理。 然后,我们需要创建一个 `shiro.xml` 配置文件,用于定义 Shiro 的具体行为。在这个配置文件中,我们可以自定义 Realm...

    非maven的ssm整合shiro

    2. **配置Shiro**: 在Spring配置文件中,定义Shiro的`SecurityManager`,通常使用`org.apache.shiro.spring.web.ShiroFilterFactoryBean`来创建过滤器。配置包括设置安全管理器、Realm(认证和授权信息的来源)和...

    java shiro实现退出登陆清空缓存

    这个 bean 配置了 Shiro 过滤器链,其中包含了自定义的登出过滤器 `systemLogoutFilter`: ```xml &lt;bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"&gt; &lt;!-- 自定义...

    shiro 与 spring 整合、动态过滤链、以及认证、授权.docx

    Apache Shiro 是一个轻...通过配置 Shiro 过滤器链,我们可以控制对 Web 应用中不同 URL 的访问权限,同时 Shiro 的认证和授权机制使得权限管理变得简单。此外,动态过滤链和缓存处理进一步提高了系统性能和用户体验。

    springmvc shiro使用

    这段配置指定了使用Spring提供的代理过滤器`DelegatingFilterProxy`,并通过`targetFilterLifecycle`参数确保过滤器可以在每个HTTP请求周期内被正确调用。 ##### 3. 配置Spring Security Manager 在Spring的配置...

    Spring框架中整合Shiro

    -- 配置过滤器链 --&gt; &lt;property name="filterChainDefinitions"&gt; /login = authc /logout = logout /** = anon ``` 在Web层,我们可以使用Shiro提供的Filter进行权限控制。例如,通过在web.xml中配置...

    SpringBoot集成 Shiro安全框架【完整源码+数据库】

    5. **配置Filter**:在SpringBoot的主类或者配置类中,通过`@WebFilter`注解配置Shiro Filter,将Shiro的过滤器链绑定到SpringBoot的DispatcherServlet。 6. **编写Controller**:创建处理登录、登出的Controller,...

    springmvc集成shiro登录权限示例代码

    总之,SpringMVC 集成 Shiro 主要涉及到配置过滤器、安全管理器、Realm 以及自定义过滤器等步骤,从而实现对用户登录、权限控制的管理。通过这样的集成,开发者可以更专注于业务逻辑,而将安全性交由 Shiro 这样的...

Global site tag (gtag.js) - Google Analytics