1. 程式人生 > >遇到問題----shrio------shiro自定義filters無效

遇到問題----shrio------shiro自定義filters無效

我們在上面一篇文章中已經知道了如何自定義filters來重寫角色驗證,authc授權驗證等。

但是出現了filters無效的情況。

shiro  自定義filters無效

這種情況主要分成兩個原因

一是shiro本身的配置有問題,就是shiro未生效。

這種情況建議重新檢查一遍shiro的搭建。先不自定義filers。

參考連結:

二是攔截順序設定的有問題

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定義filters無效

我們在上面一篇文章中已經知道了如何自定義filters來重寫角色驗證,authc授權驗證等。但是出現了filters無效的情況。shiro  自定義filters無效這種情況主要分成兩個原因一是shiro本身的配置有問題,就是shiro未生效。這種情況建議重新檢查一遍shir

shiro定義realm(五)

cm4 dsm cbc vdh fab strong vts gn3 aqi 上一節介紹了realm的作用: realm:需要根據token中的身份信息去查詢數據庫(入門程序使用ini配置文件),如果查到用戶返回認證信息,如果查詢不到返回null。token就相當於是對用戶

shiro定義realm支持MD5算法(六)

svi vfs ltm vold rcc TTT sym fio owa 1.1 散列算法 通常需要對密碼 進行散列,常用的有md5、sha, 對md5密碼,如果知道散列後的值可以通過窮舉算法,得到md5密碼對應的明文。 建議對md5進行散列時加salt(鹽),

Shiro定義密碼匹配認證

() global throws equals urn authent assert cred color 項目集成shiro的時候,有寫某個自定義類然後繼承自AuthorizingRealm 並且重寫實現了他的2個方法: 1、其中一個:認證回調 驗證賬戶密碼的 doGet

SSM+Apache shiro--定義realm

ati 定義 ppi auto word sta active 如果 ive 自定義reaml需繼承AuthorizingRealm,並重寫doGetAuthorizationInfo(用戶獲取授權信息)和doGetAuthenticationInfo(用戶獲取認證信息)兩

(3)shiro定義realm

上面一章說到shiro的認證和授權最底層就是呼叫realm的getAuthorizationInfo(獲取使用者的角色和資源)和getAuthenticationInfo(校驗賬號密碼是否正確)兩個方法。 如果我們要從資料庫中查詢使用者和他的許可權資訊,我們可以使用shiro提供給我們的JdbcRealm

android textiew定義ClickableSpan無效問題

pre col pri andro method [] 一個數 buffer selection 我們有個需求,需要將一段文本中的url跳轉,替換成跳轉我們app的某個頁面。然後就開始搞,先自定義clickspan,在設置LinkMovementMethoid, 但是不管

shiro定義過濾器

就是自定義過濾規則 一些特殊的請求路徑 需要進過自定義的過濾器 在自定義的過濾器中 認證規則是自己定的 spring檔案: <!-- 引入資料來源 --> <context:property-placeholder lo

SwitchResX 定義解析度無效& Not install 的解決方案

這部分是前置內容,如果解決了可以略掉 關閉SIP的方法 https://my.oschina.net/u/3182047/blog/1503217 SwitchResX下載地址 連結: https://pan.baidu.com/s/1o88zgb4 密碼: r36c 以下文章轉載自

Shiro安全框架第四篇| Shiro定義Realm進行認證授權

  SHiro自定義Realm 首先在resource下新建user.ini 1[users] 2jiuyue=12345,admin 3[roles] 4admin=user:delete,user:update 然後測試類下新建IniRealmTest測試類,跟前面不

springmvc+shiro定義過濾器

實現需求: 1.使用者未登入,跳轉到登入頁,登入完成後會跳到初始訪問頁。 2.使用者自定義處理(如需要啟用),跳轉到啟用頁面,啟用完成後會跳到初始訪問頁。 使用到的框架 springmvc 的攔截器 shiro 自定義過濾器 實現: 1.編寫攔截器通過sessi

Shiro(三)——Shiro定義Realm

Realm: 域。Shiro 從從 Realm 獲取安全資料(如使用者、角色、許可權),就是說 SecurityManager 要驗證使用者身份,那麼它需要從 Realm 獲取相應的使用者進行比較以確定使用者身份是否合法;也需要從 Realm 得到使用者相應的角色 / 許可權進行驗證使用者是否能進行

shiro 定義FormAuthenticationFilter,記住我

驗證碼 思路 shiro使用FormAuthenticationFilter進行表單認證,驗證校驗的功能應該加在FormAuthenticationFilter中,在認證之前進行驗證碼校驗。 需要寫FormAuthenticationFilter的子類,繼承FormAuth

spring boot配置shiro定義shiro filter匹配異常

原文地址:http://www.hillfly.com/2017/179.html最近忙著研究在 Springboot 上使用 Shiro 的問題。剛好就遇到個詭異事,百度 Google 也沒找到啥有價值的資訊,幾番周折自己解決了,這裡稍微記錄下。自定義 FilterTOCS

shiro定義登陸和推出的過濾器,實現登陸之後根據條件跳轉不同頁面,推出登陸根據條件跳轉不同頁面

shiro中我們可以通過自定義過濾器的方式來實現自己想要的結果,比如想要登陸之後跳轉不同頁面 @Bean(name = "shiroFilter") public ShiroFilterFactoryBean getShiroFilterFacto

Springboot 整合Shiro定義Filter

網上自定義Filter的實現很多,這裡我提供一種Springboot在程式碼中的實現。 Shiro提供的Filter我這裡不一一介紹了,一般基於web會話的都是使用authc(這是FormAuthenticationFilter)。根據我無狀態的登陸需求,選擇

原始碼分析Dialog定義大小無效

最近在專案中用到了自定義Dialog,以前也是經常用,只不過要麼是用自帶的dialog樣式,要麼也是很簡單的佈局,所以並沒有重視修改dialog大小的坑。直到這次專案中產(keng)品(die)經(wan)理(yi)死了都說dialog大小別扭要求改,然後突然發

### shiro定義filter【解決某個請求 需要同時擁有多種許可權】

1. shiro的filter介紹 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">         <property name="s

SpringBoot整合Shiro定義Redis存儲

min locked 訪問控制 ride ora efault protect contex lan Shiro   Shiro 主要分為 安全認證 和 接口授權 兩個部分,其中的核心組件為 Subject、 SecurityManager、 Realms,公共部分 Sh

shiro授權及定義realm授權(七)

qbc mtp jbd red es6 sil llb wmi sin 1.授權流程 2. 三種授權方法          Shiro 支持三種方式的授權: 編程式:通過寫if/else 授權代碼塊完成: Subject subject = SecurityU