1. 程式人生 > >spring security 資源判斷時不執行自定義AccessDecisionManager

spring security 資源判斷時不執行自定義AccessDecisionManager

        弄這個框架的時候,因為這個問題困擾了很久,一直顯示無法進入AccessDecisionManager內,導致資源請求無法進行有效攔截,資料找了很久也並未找到合理解決方案,表示當時就腦子不夠用,及看不懂原始碼,又不懂原理,搭建都是靠人家的教程,一開始也看到了一篇類似的博文,但沒太在意,事實證明也正是因為那個原因導致,

博文內容如下:

     

隨後,根據此提示修改了相應程式碼,

@Override
	public Collection<ConfigAttribute> getAttributes(Object obj)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		//獲取請求的url地址
		String url = ((FilterInvocation)obj).getRequestUrl();
		System.out.println("FilterInvocationSecurityMetadataSourceImpl:getAttributes()---------------請求地址為:"+url);
		
		Collection<ConfigAttribute> collection = new LinkedList<>();
		
		//查詢資料庫中是否存在此連結
		List<ModuleApRole> moduleApRoleList = moduleMapper.selectByUri(url);
		for (ModuleApRole moduleApRole : moduleApRoleList) {
			if (moduleApRole.getRole_id() != null) {
				ConfigAttribute configAttribute = new SecurityConfig(moduleApRole.getRole_id()+"");
				collection.add(configAttribute);
			}
		}
		
		//防止資料庫中沒有資料,不能進行許可權攔截
		if(collection.size()<1){
			ConfigAttribute configAttribute = new SecurityConfig("ROLE_NO_USER");
			collection.add(configAttribute);
		}
		return collection;
	}

  這樣框架在解析的時候就會判斷此請求有效,然後再進入AccessDecisionManager的decide()進行資源許可權的驗證,實測是可以的,還有一種方法聽說是重寫AbstractSecurityInterceptor的方法實現。至於為什麼collection在返回的時候不能為空,那不管,那是框架定的規則。

還有一種,是資源攔截不準確,可能出現的情況有如下兩種情況:

  • 1.使用了Struts2+spring框架,在web.xml檔案,<filter>標籤時 Struts2配置到了spring security上面,由於web.xml配置檔案中執行順序是從上至下,所以可能會導致資源請求時被Struts2先攔截,
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>adsystem</display-name>
  <welcome-file-list>
  
  <welcome-file>wrt/manage/main.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- Spring 配置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml,classpath:applicationContext-Security.xml,classpath:applicationContext-mybatis.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
	<!-- Spring 重新整理Introspector防止記憶體洩露 -->
	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>
	
	<!--  防止多人登陸 ,控制一個使用者只能登入一次,不能在其他地方重新登入-->
	<listener>
		<listener-class>
			org.springframework.security.web.session.HttpSessionEventPublisher 
		</listener-class>
	</listener>
	
	<!-- session超時定義,單位為分鐘 -->
	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>
	
 
  <!-- Struts2配置  使用預設-->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  
  
  <!-- Spring Security 安全管理機制 -->
  <filter>
       <filter-name>springSecurityFilterChain</filter-name>
       <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>springSecurityFilterChain</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>


  

    <!-- 配置log4j配置檔案的路徑,可以是xml或 properties(此引數必須配)--> 
	<!-- 下面使用了classpath 引數指定log4j.properties檔案的位置,這樣log4j的配置檔案就不用非要放到src的下面 -->
	<context-param>
	   <param-name>log4jConfigLocation</param-name>
	 <param-value>classpath:config/log4j.properties</param-value>
	 
	</context-param> 
	<!-- 使用spring的監聽器,當應用啟動時來讀取log4j的配置檔案 -->
	<listener>
	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
    
</web-app>
更正為:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>adsystem</display-name>
  <welcome-file-list>
  
  <welcome-file>wrt/manage/main.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- Spring 配置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml,classpath:applicationContext-Security.xml,classpath:applicationContext-mybatis.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
	<!-- Spring 重新整理Introspector防止記憶體洩露 -->
	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>
	
	<!--  防止多人登陸 ,控制一個使用者只能登入一次,不能在其他地方重新登入-->
	<listener>
		<listener-class>
			org.springframework.security.web.session.HttpSessionEventPublisher 
		</listener-class>
	</listener>
	
	<!-- session超時定義,單位為分鐘 -->
	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>
  
  
  <!-- Spring Security 安全管理機制 -->
  <filter>
       <filter-name>springSecurityFilterChain</filter-name>
       <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   </filter>
   <filter-mapping>
       <filter-name>springSecurityFilterChain</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>


  <!-- Struts2配置  使用預設-->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  

    <!-- 配置log4j配置檔案的路徑,可以是xml或 properties(此引數必須配)--> 
	<!-- 下面使用了classpath 引數指定log4j.properties檔案的位置,這樣log4j的配置檔案就不用非要放到src的下面 -->
	<context-param>
	   <param-name>log4jConfigLocation</param-name>
	 <param-value>classpath:config/log4j.properties</param-value>
	 
	</context-param> 
	<!-- 使用spring的監聽器,當應用啟動時來讀取log4j的配置檔案 -->
	<listener>
	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
    
</web-app>
  • 資源不能被攔截,配置屬於失效情況,可能是在applicationContext-Security.xml配置檔案中,忘了將安全訪問策略配置上去,<security:custom-filter ref="myFilterSecurityInterceptor" before="FILTER_SECURITY_INTERCEPTOR"/>    
更改後,


相關推薦

spring security 資源判斷執行定義AccessDecisionManager

        弄這個框架的時候,因為這個問題困擾了很久,一直顯示無法進入AccessDecisionManager內,導致資源請求無法進行有效攔截,資料找了很久也並未找到合理解決方案,表示當時就腦子不夠用,及看不懂原始碼,又不懂原理,搭建都是靠人家的教程,一開始也看到了一

spring-security 個性化用戶認證流程——定義登錄頁面(可配置)

ron 進行 狀態 row 錯誤 this 力度 override all 1.定義自己的登錄頁面我們需要根據自己的業務系統構建自己的登錄頁面以及登錄成功、失敗處理在spring security提供給我的登錄頁面中,只有用戶名、密碼框,而自帶的登錄成功頁面是空白頁面(可以

spring-security 個性化用戶認證流程——定義登錄成功/失敗的處理

http servle utf author ddr als ble 沒有 ont 1.自定義登錄成功處理什麽需要自定義登錄成功處理,因為登錄行為不止只有一種,有可能是ajax請求,而默認的則是form提交跳轉的行為,這個時候就不是我們想要的一種結果。 如果自定義登錄成功之

Spring Boot專案啟動可以根據定義配置決定初始化哪些Bean

讓Spring Boot專案啟動時可以根據自定義配置決定初始化哪些Bean 問題描述 實現思路 思路一 [不符合要求] 思路二[滿足要求] 思路三[未試驗] 問題描述 目前我工作環境下,後端主要的框架是

[Spring Security] 表單登入通過配置定義登入頁面,以及定義認證成功/失敗處理機制

1.目錄結構2.配置自定義登入頁通過配置SecurityProperties,MyProperties,SecurityCoreConfig來讀取resources資料夾下application.properties中相關配置項。SecurityProperties:pack

Spring Security 入門(四):定義-Filter

前文導讀本文解決問題將自定義的 Filter 加入到 Spring Security 中的 Fi

spring-security認證過程的分析及定義登入

首先spring-security配置認證過濾器,它是spring-security處理業務的入口。使用者如果不重寫過濾器,使用預設的過濾器UsernamePasswordAuthenticationFilter。它繼承了抽象類AbstractAuthentic

Spring Cloud實戰 | 第九篇:Spring Cloud整合Spring Security OAuth2認證伺服器統一認證定義異常處理

[本文完整程式碼下載點選](https://github.com/hxrui/youlai-mall.git) # 一. 前言 相信瞭解過我或者看過我之前的系列文章應該多少知道點我寫這些文章包括建立 [有來商城youlai-mall](https://github.com/hxrui/youlai

spring boot-執行Async任務,使用定義執行緒池

一、增加配置屬性類 package com.chhliu.springboot.async.configuration; import org.springframework.boot.context.properties.ConfigurationPropertie

spring-boot configuration processor 讓配置檔案有提示(包括定義的)

IDEA新建spring-boot時  勾選該項   勾選該項後pom.xml就會出現 <dependency> <groupId>org.springframework.boot</groupId> <

spring boot 掃描定義的Controller。。。

    springboot專案的特色,這裡就不多說了,框架搭建是非常簡單的;小編在搭建成功之後遇到了如下問題:     掃描不到自定義的controller。。。     文章介紹兩個可能的問題方案: &nbs

spring在專案啟動執行特定方法

如何在專案啟動時就執行特定方法 1. 方法上加註解@PostConstruct @Compant public class InitDemo{ @PostConstruct

Android之——使用Android studio建立的AIDL編譯定義類的解決辦法

使用AS建立ADIL檔案時AS會在main資料夾下給我們生成一個aidl資料夾和一個相同包名的包,通常我們會把所有和ADIL相關的類或檔案放在這個包下,但是如果存在自定義的類時,程式編譯時無法通過,提示找不到自定義的包。解決辦法如下,在啟動Module的build.gra

spring-boot configuration processor 讓配置檔案有提示(包括定義的)

IDEA新建spring-boot時  勾選該項 勾選該項後pom.xml就會出現 <dependency> <groupId>org.springframework.boot</groupId> <art

使用thymeleaf+spring security處理csrf遇到Cannot create a session after the response has been committed

文章目錄 被這個問題折磨了幾個小時,期間懷疑了各種程式碼,但最終還是讓我發現了根本性的原因 spring security中預設csrf是懶載入的,只有在第一次使用_csrf時才會建立session 而thymeleaf頁面的緩衝區滿後,response會在模板

SpringBoot使用@RunWith(Parameterized.class)進行引數化測試同時支援依賴注入,以及CommandLineRunner在單元測試執行主程序

問題提出 在使用SpringBoot進行單元測試的時候,我發現了兩個問題 使用引數化測試的時候,必須使用 @RunWith(Parameterized.class),而對Spring進行單元測試時,如果想使用依賴注入,即使用 @Autowired 註解,需要使

關於js中連續click執行訪問後臺請求,當點選停止2s之後,立即發起訪問後臺的請求的解決方案

防止使用者重複向後臺發起大量請求,下面我們通過一個例子實現: 當連續點選修改溫度的值時,頁面上顯示的值每次都根據具體情況做出修改,但是將這個值儲存的資料庫的操作則是在點選操作結束後2s發起的(其中若連續多次點選改變溫度的值,則只是在使用者最後一次修改完成後2s執行儲存到資料庫的操作)

spring security + thymeleaf 判斷登入使用者的許可權

spring security的UserDetailService是我自己定義的。 @Component public class MyUserDetailsService implements UserDetailsService {@Autowired private

shiro和Spring整合使用註解沒有執行realm的doGetAuthorizationInfo回撥方法的解決

在使用Shiro框架進行專案整合時,使用註解在使用Shiro框架進行專案整合時,使用註解在使用Shiro框架進行專案整合時,使用註解@RequiresPermissions為方法提供是需要的許可權,但是根本沒有進行驗證,後面發現在自己的Realm中只執行了doGetAuth

django定義model,直接執行定義SQL

如果不想定義model,直接執行自定義SQL,可如下操作: 1. 通過 connections獲取db連線,如果是多個數據庫,connections['dbName'] 來選擇 2. 獲取遊標 cursor 3. 執行sql: cursor.execute(sql) 4.獲取返回結果:fet