使用Spring安全表示式控制系統功能訪問許可權

2022-08-24 23:39:10 字數 3138 閱讀 5412

一、spel表示式許可權控制

從spring security 3.0開始已經可以使用spring expression表示式來控制授權,允許在表示式中使用複雜的布林邏輯來控制訪問的許可權。spring security可用表示式物件的基類是securityexpressionroot。

表示式函式        描述

hasrole([role])        使用者擁有指定的角色時返回true (spring security缺省會帶有role_字首),去除字首參考remove the role_

hasanyrole([role1,role2])        使用者擁有任意乙個指定的角色時返回true

hasauthority([authority])        擁有某資源的訪問許可權時返回true

hasanyauthority([auth1,auth2])        擁有某些資源其中部分資源的訪問許可權時返回true

permitall        永遠返回true

denyall        永遠返回false

anonymous        當前使用者是anonymous時返回true

rememberme        當前使用者是rememberme使用者返回true

authentication        當前登入使用者的authentication物件

fullauthenticated        當前使用者既不是anonymous也不是rememberme使用者時返回true

hasipaddress('192.168.1.0/24'))        請求傳送的ip匹配時返回true

部分朋友可能會對authority和role有些混淆。authority作為資源訪問許可權可大可小,可以是某按鈕的訪問許可權(如資源id:biz1),也可以是某類使用者角色的訪問許可權(如資源id:admin)。當authority作為角色資源許可權時,hasauthority('role_admin')與hasrole('admin')是一樣的效果。

二、spel在全域性配置中的使用

我們可以通過繼承websecurityconfigureradapter,實現相關的配置方法,進行全域性的安全配置(之前的章節已經講過) 。下面就為大家介紹一些如何在全域性配置中使用spel表示式。

2.1.url安全表示式

config.antmatchers("/system/*").access("hasauthority('admin') or hasauthority('user')")

.anyrequest().authenticated();

這裡我們定義了應用/person/*url的範圍,只有擁有admin或者user許可權的使用者才能訪問這些person資源。

2.2.安全表示式中引用bean

這種方式,比較適合有複雜許可權驗證邏輯的情況,當spring security提供的預設表示式方法無法滿足我們的需求的時候。首先我們定義乙個許可權驗證的rbacservice。

@component("rbacservice")

@slf4j

public class rbacservice

public boolean checkuserid(authentication authentication, int id)

}對於"/person/"對應的資源的訪問,呼叫rbacservice的bean的方法checkuserid進行許可權驗證,傳遞引數為authentication物件和person的id。該id為pathvariable,以#開頭表示。

config.antmatchers("/person/").access("@rbacservice.checkuserid(authentication,#id)")

.anyrequest().access("@rbacservice.haspermission(request,authentication)");

三、 method表示式安全控制

如果我們想實現方法級別的安全配置,spring security提供了四種註解,分別是@preauthorize , @prefilter , @postauthorize 和 @postfilter

3.1.開啟方法級別註解的配置

在spring安全配置**中,加上enableglobalmethodsecurity註解,開啟方法級別安全配置功能。

@configuration

@enableglobalmethodsecurity(prepostenabled = true)

public class mysecurityconfig extends websecurityconfigureradapter {

3.2 使用preauthorize註解

@preauthorize 註解適合進入方法前的許可權驗證。只有擁有admin角色才能訪問findall方法。

@preauthorize("hasrole('admin')")

listfindall();

3.3 使用postauthorize註解

@postauthorize 在方法執行後再進行許可權驗證,適合根據返回值結果進行許可權驗證。spring el 提供返回物件能夠在表示式語言中獲取返回的物件returnobject。下文**只有返回值的name等於authentication物件的name才能正確返回,否則丟擲異常。

@postauthorize("returnobject.name == authentication.name")

person findone(integer id);

3.4 使用prefilter註解

prefilter 針對引數進行過濾,下文**表示針對ids引數進行過濾,只有id為偶數才能訪問delete方法。

//當有多個物件是使用filtertarget進行標註

@prefilter(filtertarget="ids", value="filterobject%2==0")

public void delete(listids, listusernames) {

3.5 使用postfilter 註解

postfilter 針對返回結果進行過濾,特別適用於集合類返回值,過濾集合中不符合表示式的物件。

@postfilter("filterobject.name == authentication.name")

listfindall();

Spring 切點表示式

摘要 spring中的aspectj切點表示式函式 切點表示式函式就像我們的gps導航軟體。通過切點表示式函式,再配合萬用字元和邏輯運算子的靈活運用,我們能很好定位到我們需要織入增強的連線點上。經過上面的鋪墊,下面來看看springz中支援的切點表 spring中的aspectj切點表示式函式 切點...

Spring表示式語言

org.springframework spring expression 4.0.5.release org.springframework spring core 4.0.5.release org.springframework spring context 4.0.5.release pub...

Spring 表示式語言 SpEL

spel 字面量 spel支援的運算符號 constructor arg value property name equal value property property name hascap value property spel支援的運算符號 constructor arg value co...