Spring Security其它權(quán)限校驗方式&自定義權(quán)限校驗方式
一、其它權(quán)限校驗方法(了解)
我們前面都是使用@PreAuthorize注解,然后在在其中使用的是hasAuthority方法進行校驗。
SpringSecurity還為我們提供了其它方法例如:hasAnyAuthority,hasRole,hasAnyRole等。
這里我們先不急著去介紹這些方法,我們先去理解hasAuthority的原理,然后再去學(xué)習(xí)其他方法你就更容易理解,而不是死記硬背區(qū)別。并且我們也可以選擇定義校驗方法,實現(xiàn)我們自己的校驗邏輯。
hasAuthority方法實際是執(zhí)行到了SecurityExpressionRoot的hasAuthority,大家只要斷點調(diào)試既可知道它內(nèi)部的校驗原理。
它內(nèi)部其實是調(diào)用authentication的getAuthorities方法獲取用戶的權(quán)限列表。然后判斷我們在接口上定義的權(quán)限是否被包含于用戶的權(quán)限列表,如果有則返回true放行,反之false攔截。
hasAnyAuthority方法可以傳入多個權(quán)限,只有用戶有其中任意一個權(quán)限都可以訪問對應(yīng)資源。
@PreAuthorize("hasAnyAuthority('admin','test','system:dept:list')") public String hello(){ return "hello"; }
hasRole要求有對應(yīng)的角色才可以訪問,但是它內(nèi)部會把我們傳入的參數(shù)拼接上 ROLE_ 后再去比較。
所以這種情況下要用用戶對應(yīng)的權(quán)限也要有 ROLE_ 這個前綴才可以。
@PreAuthorize("hasRole('system:dept:list')") public String hello(){ return "hello"; }
hasAnyRole 有任意的角色就可以訪問。它內(nèi)部也會把我們傳入的參數(shù)拼接上 ROLE_ 后再去比較。
所以這種情況下要用用戶對應(yīng)的權(quán)限也要有 ROLE_ 這個前綴才可以。
@PreAuthorize("hasAnyRole('admin','system:dept:list')") public String hello(){ return "hello"; }
二、自定義權(quán)限校驗方法(掌握)
我們也可以定義自己的權(quán)限校驗方法,在@PreAuthorize注解中使用我們的方法。
@Component("ex") public class SGExpressionRoot { public boolean hasAuthority(String authority) { //獲取當前用戶的權(quán)限 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); LoginUser loginUser = (LoginUser) authentication.getPrincipal(); List<String> permissions = loginUser.getPermissions(); //判斷用戶權(quán)限集合中是否存在authority return permissions.contains(authority); } }
在SPEL表達式中使用 @ex相當于獲取容器中bean的名字未ex的對象。然后再調(diào)用這個對象的
hasAuthority方法
@RequestMapping("/hello") @PreAuthorize("@ex.hasAuthority('system:dept:list')") public String hello(){ return "hello"; }
三、基于配置的權(quán)限控制
我們也可以在配置類中使用使用配置的方式對資源進行權(quán)限控制。
eg:
.antMatchers("/testCors").hasAuthority("system:dept:list222")
@Override protected void configure(HttpSecurity http) throws Exception { http //關(guān)閉csrf .csrf().disable() //不通過Session獲取SecurityContext .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .authorizeRequests() // 對于登錄接口 允許匿名訪問 .antMatchers("/user/login").anonymous() .antMatchers("/testCors").hasAuthority("system:dept:list222") // 除上面外的所有請求全部需要鑒權(quán)認證 .anyRequest().authenticated(); //添加過濾器 http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); //配置異常處理器 http.exceptionHandling() //配置認證失敗處理器 .authenticationEntryPoint(authenticationEntryPoint) .accessDeniedHandler(accessDeniedHandler); //允許跨域 http.cors(); }
四、CSRF
CSRF是指跨站請求偽造(Cross-site request forgery),是web常見的攻擊之一。
SpringSecurity去防止CSRF攻擊的方式就是通過csrf_token。后端會生成一個csrf_token,前端發(fā)起請求的時候需要攜帶這個csrf_token,后端會有過濾器進行校驗,如果沒有攜帶或者是偽造的就不允許訪問。
我們可以發(fā)現(xiàn)CSRF攻擊依靠的是cookie中所攜帶的認證信息。但是在前后端分離的項目中我們的認證信息其實是token,而token并不是存儲中cookie中,并且需要前端代碼去把token設(shè)置到請求頭中才可以,所以CSRF攻擊也就不用擔心了。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot參數(shù)校驗Validator框架詳解
Validator框架就是為了解決開發(fā)人員在開發(fā)的時候少寫代碼,提升開發(fā)效率,Validator專門用來進行接口參數(shù)校驗,今天通過本文給大家介紹SpringBoot參數(shù)校驗Validator框架,感興趣的朋友一起看看吧2022-06-06java ThreadPoolExecutor使用方法簡單介紹
這篇文章主要介紹了java ThreadPoolExecutor使用方法簡單介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02SpringBoot實現(xiàn)動態(tài)多線程并發(fā)定時任務(wù)
這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)動態(tài)多線程并發(fā)定時任務(wù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05基于Springboot實現(xiàn)定時發(fā)送郵件功能
這篇文章主要為大家詳細介紹了基于Springboot實現(xiàn)定時發(fā)送郵件功能的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Java基礎(chǔ)之內(nèi)部類與代理知識總結(jié)
今天帶大家復(fù)習(xí)Java的基礎(chǔ)知識,文中有非常詳細的介紹及圖文示例,對正在學(xué)習(xí)Java的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06