遇到问题----shrio------shiro自定义filters无效
2017-03-08 20:09
375 查看
我们在上面一篇文章中已经知道了如何自定义filters来重写角色验证,authc授权验证等。
shiro角色( roles)自定义Filter----同一个URL配置多个角色的或关系
但是出现了filters无效的情况。
shiro 自定义filters无效
这种情况主要分成两个原因
一是shiro本身的配置有问题,就是shiro未生效。
这种情况建议重新检查一遍shiro的搭建。先不自定义filers。
参考链接:
springMVC与shiro集成
二是拦截顺序设置的有问题
shiro每个URL配置,表示匹配该URL的应用程序请求将由对应的过滤器进行验证。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表达式说明
1、URL目录是基于HttpServletRequest.getContextPath()此目录设置
2、URL可使用通配符,**代表任意子目录
3、Shiro验证URL时,URL匹配成功便不再继续匹配查找。所以要注意配置文件中的URL顺序,尤其在使用通配符时。
Filter Chain定义说明
1、一个URL可以配置多个Filter,使用逗号分隔
2、当设置多个过滤器时,全部验证通过,才视为通过
3、部分过滤器可指定参数,如perms,roles
大家注意到
Shiro验证URL时,URL匹配成功便不再继续匹配查找。
如果我们把/**放在其他/student/**前面,则只会进入/**的拦截,不会再进去/student/**的拦截了。
所以需要注意filterChainDefinitions中的顺序,越仔细的路径应该放在越前面。
/**=athuc尤其不能放在最前,否则就不会进roles的拦截了。
错误的顺序
<property name="filterChainDefinitions">
<value>
/**/*.* = anon
/login = anon
/** = authc
/student/** =roles["admin,normal,assistant"]
/teacher/** =roles["admin,normal,assistant"]
/class/** =roles["admin,normal,assistant"]
/grade/** =roles["admin,normal"]
</value>
</property>
正确的拦截顺序
<property name="filterChainDefinitions">
<value>
/**/*.* = anon
/login = anon
/student/** =roles["admin,normal,assistant"]
/teacher/** =roles["admin,normal,assistant"]
/class/** =roles["admin,normal,assistant"]
/grade/** =roles["admin,normal"]
/** = authc
</value>
</property>
完整shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more
contributor license agreements. See the NOTICE file ~ distributed with this
work for additional information ~ regarding copyright ownership. The ASF
licenses this file ~ to you under the Apache License, Version 2.0 (the ~
"License"); you may not use this file except in compliance ~ with the License.
You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, ~ software
distributed under the License is distributed on an ~ "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY ~ KIND, either express or implied. See the
License for the ~ specific language governing permissions and limitations
~ under the License. -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mongoRealm" class="com.test.web.support.shiro.MyShiro">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.SimpleCredentialsMatcher"></bean>
</property>
<property name="mongoTemplate" ref="mongoTemplate" />
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- <property name="cacheManager" ref="cacheManager" /> -->
<!-- <property name="sessionManager" ref="sessionManager" /> -->
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="rememberMeManager">
<bean class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cookie">
<bean class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="sid" />
<!--设置Cookie名字,默认为JSESSIONID -->
<property name="name" value="WEBSID" />
</bean>
</property>
</bean>
</property>
<property name="realm" ref="mongoRealm" />
</bean>
<!-- shiroFilter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="unauthorizedUrl" value="/403" />
<property name="filters">
<map>
<entry key="authc">
<bean
class="com.test.web.support.shiro.AjaxCompatibleAuthenticationFilter"></bean>
</entry>
<entry key="roles">
<bean
class="com.test.web.support.shiro.CustomRolesAuthorizationFilter" />
</entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
/**/*.* = anon
/login = anon
/student/** =roles["admin,normal,assistant"]
/teacher/** =roles["admin,normal,assistant"]
/class/** =roles["admin,normal,assistant"]
/grade/** =roles["admin,normal"]
/** = authc
</value>
</property>
</bean>
</beans>
shiro角色( roles)自定义Filter----同一个URL配置多个角色的或关系
但是出现了filters无效的情况。
shiro 自定义filters无效
这种情况主要分成两个原因
一是shiro本身的配置有问题,就是shiro未生效。
这种情况建议重新检查一遍shiro的搭建。先不自定义filers。
参考链接:
springMVC与shiro集成
二是拦截顺序设置的有问题
shiro每个URL配置,表示匹配该URL的应用程序请求将由对应的过滤器进行验证。
例如:
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest
/remoting/rpc/** = authc, perms["remote:invoke"]
URL表达式说明
1、URL目录是基于HttpServletRequest.getContextPath()此目录设置
2、URL可使用通配符,**代表任意子目录
3、Shiro验证URL时,URL匹配成功便不再继续匹配查找。所以要注意配置文件中的URL顺序,尤其在使用通配符时。
Filter Chain定义说明
1、一个URL可以配置多个Filter,使用逗号分隔
2、当设置多个过滤器时,全部验证通过,才视为通过
3、部分过滤器可指定参数,如perms,roles
大家注意到
Shiro验证URL时,URL匹配成功便不再继续匹配查找。
如果我们把/**放在其他/student/**前面,则只会进入/**的拦截,不会再进去/student/**的拦截了。
所以需要注意filterChainDefinitions中的顺序,越仔细的路径应该放在越前面。
/**=athuc尤其不能放在最前,否则就不会进roles的拦截了。
错误的顺序
<property name="filterChainDefinitions">
<value>
/**/*.* = anon
/login = anon
/** = authc
/student/** =roles["admin,normal,assistant"]
/teacher/** =roles["admin,normal,assistant"]
/class/** =roles["admin,normal,assistant"]
/grade/** =roles["admin,normal"]
</value>
</property>
正确的拦截顺序
<property name="filterChainDefinitions">
<value>
/**/*.* = anon
/login = anon
/student/** =roles["admin,normal,assistant"]
/teacher/** =roles["admin,normal,assistant"]
/class/** =roles["admin,normal,assistant"]
/grade/** =roles["admin,normal"]
/** = authc
</value>
</property>
完整shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more
contributor license agreements. See the NOTICE file ~ distributed with this
work for additional information ~ regarding copyright ownership. The ASF
licenses this file ~ to you under the Apache License, Version 2.0 (the ~
"License"); you may not use this file except in compliance ~ with the License.
You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, ~ software
distributed under the License is distributed on an ~ "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY ~ KIND, either express or implied. See the
License for the ~ specific language governing permissions and limitations
~ under the License. -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="mongoRealm" class="com.test.web.support.shiro.MyShiro">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.SimpleCredentialsMatcher"></bean>
</property>
<property name="mongoTemplate" ref="mongoTemplate" />
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- securityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- <property name="cacheManager" ref="cacheManager" /> -->
<!-- <property name="sessionManager" ref="sessionManager" /> -->
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="rememberMeManager">
<bean class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cookie">
<bean class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="sid" />
<!--设置Cookie名字,默认为JSESSIONID -->
<property name="name" value="WEBSID" />
</bean>
</property>
</bean>
</property>
<property name="realm" ref="mongoRealm" />
</bean>
<!-- shiroFilter -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="unauthorizedUrl" value="/403" />
<property name="filters">
<map>
<entry key="authc">
<bean
class="com.test.web.support.shiro.AjaxCompatibleAuthenticationFilter"></bean>
</entry>
<entry key="roles">
<bean
class="com.test.web.support.shiro.CustomRolesAuthorizationFilter" />
</entry>
</map>
</property>
<property name="filterChainDefinitions">
<value>
/**/*.* = anon
/login = anon
/student/** =roles["admin,normal,assistant"]
/teacher/** =roles["admin,normal,assistant"]
/class/** =roles["admin,normal,assistant"]
/grade/** =roles["admin,normal"]
/** = authc
</value>
</property>
</bean>
</beans>
相关文章推荐
- 遇到问题----shrio------shiro登录,多个项目session被覆盖问题---两个web项目会导致shiro的session污染
- 遇到"无效的默认脚本语言..."问题的解决办法
- 加密解密时遇到的"不正确的数据"以及"要解密的数据长度无效"问题解决方案
- 在自定义鼠标指针的时候遇到的一些问题
- 今天遇到的问题一————此页的状态信息无效,可能已损坏
- moss2007自定义工作流开发中运行install.bat文件遇到的问题及解决方法
- 加密解密时遇到的"不正确的数据"以及"要解密的数据长度无效"问题解决方案
- WPF自定义集合控件概述与遇到的问题
- 工作线程调用sendmessage()向主对话框发送自定义消息遇到的问题以及解决
- WIN7下.Net开发遇到的又一问题:HTTP 错误 500.19 - Internal Server Error,无法访问请求的页面,因为该页的相关配置数据无效。
- 自定义ListView里面条目有CheckBox 监听ListView遇到的问题
- ADO.NET中使用SqlParameter添加参数时遇到的参数无效问题
- WIN7下.Net开发遇到的又一问题:HTTP 错误 500.19 - Internal Server Error,无法访问请求的页面,因为该页的相关配置数据无效。
- eVC编程遇到的各种问题之——自定义消息的方法
- ASP.NET mvc 自定义XSLT视图引擎 遇到的问题【高手请进有疑难杂症】
- 为Reporting Service部署自定义程序集可能遇到的问题
- SharePoint : 自定义权限设置中可能遇到的问题
- [项目过程中所遇到的各种问题记录]编辑器篇——FCKeditor自定义上传路径配置
- 继承QToolButon实现Qt自定义按钮遇到的事件问题
- {"在头值中找到无效的字符。"} 发email的时候 遇到这个问题 老师解决