SpringSecurity怎樣使用注解控制權(quán)限
一般的系統(tǒng)在權(quán)限設(shè)計(jì)上,都會(huì)分為角色、權(quán)限(RDBC),復(fù)雜一點(diǎn)的可能會(huì)有用戶(hù)組、組織之類(lèi)的概念。
用戶(hù)的權(quán)限是寫(xiě)死的,對(duì)應(yīng)于后臺(tái)的接口或者資源,是沒(méi)辦法改變的,一般不對(duì)用戶(hù)開(kāi)放修改權(quán)限。
管理員用戶(hù)可以通過(guò)給角色分配權(quán)限的方式,來(lái)實(shí)現(xiàn)訪(fǎng)問(wèn)控制。
所以當(dāng)我們寫(xiě)過(guò)濾器,或者用一些安全框架時(shí)(比如Shiro,Spring Security),也需要將可變的“角色”,轉(zhuǎn)化為不可變的“權(quán)限”,注入到框架中。
具體的可以看我之前寫(xiě)的一篇(Spring Security整合jwt完整版)
注入當(dāng)前用戶(hù)的權(quán)限后,就需要進(jìn)行訪(fǎng)問(wèn)控制了。
常見(jiàn)的做法有
1、路徑比對(duì)
之前有個(gè)項(xiàng)目用過(guò)一次,定義一個(gè)過(guò)濾器,添加到security的過(guò)濾鏈中,在這個(gè)過(guò)濾器中做這么一件事:
分析當(dāng)前訪(fǎng)問(wèn)路徑所需要的權(quán)限,檢查當(dāng)前用戶(hù)是否具有該權(quán)限,做一個(gè)對(duì)比,根據(jù)對(duì)比結(jié)果來(lái)決定當(dāng)前用戶(hù)是否可以訪(fǎng)問(wèn)該資源。
這種做法的好處是代碼的入侵性不高,不需要再每個(gè)接口上加注解。但相對(duì)來(lái)說(shuō),顯得不那么直觀,可讀性比較差,所以這次換個(gè)方法。
2、使用注解的方式
SpringSecurity使用注解來(lái)控制訪(fǎng)問(wèn)時(shí),需要提前開(kāi)啟這個(gè)功能。
在配置類(lèi)上加上注解
@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter {
在接口中如此使用
/** * 條件查詢(xún) */ @PreAuthorize("hasAuthority('IMPORT:SELECT')") @ApiOperation(value = "查詢(xún)") @GetMapping("/list") public R<Page<Task>> list(TaskDto taskDto){ //測(cè)試階段,隨機(jī)生成任務(wù) Random random = new Random(); //todo int i = random.nextInt(4); if(i == 2) { metroServerAdapterService.reloadShelveTask(); } Page<Task> list = taskService.list(taskDto); return R.ok(list); }
hasAuthority可以替換成hasRole,雖然可以達(dá)到相同的目的,但是在使用的方法上還是有些不同的。
hasRole要求內(nèi)容必須以"ROLE_"開(kāi)頭,也是官方推薦的命名方式,否則沒(méi)有效果。但是hasAuthority沒(méi)有限制,數(shù)據(jù)庫(kù)中怎樣寫(xiě)的,代碼里就怎么寫(xiě)。
同樣的功能的注解為什么要有兩個(gè)名字呢。或許這么做可能在語(yǔ)義上比較清晰明確一點(diǎn),將角色與權(quán)限這兩個(gè)概念稍加區(qū)分。
仔細(xì)想一下,確實(shí)有些小型的系統(tǒng)或許壓根就不需要權(quán)限,只有給用戶(hù)分配角色,沒(méi)有給角色分配權(quán)限這一過(guò)程。這樣的話(huà),角色也是不可變的,就可以根據(jù)角色來(lái)做訪(fǎng)問(wèn)控制了。
但考慮通用性,個(gè)人覺(jué)得用hasAuthority就可以了。
SpringSecurity_權(quán)限注解@PreAuthorize、@PostAuthorize
spring是如何實(shí)現(xiàn)對(duì)HTTP請(qǐng)求進(jìn)行安全檢查和資源使用授權(quán)的?
實(shí)現(xiàn)過(guò)程由類(lèi)AbstractSecurityInterceptor在beforeInvocation方法中完成,在beforeInvocation的實(shí)現(xiàn)中,
首先,需要讀取IoC容器中Bean的配置,在這些屬性配置中配置了對(duì)HTTP請(qǐng)求資源的安全需求,
比如,哪個(gè)角色的用戶(hù)可以接入哪些URL請(qǐng)求資源,具體實(shí)現(xiàn)邏輯見(jiàn):
#與Web環(huán)境的接口FilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter
@PreAuthorize、@PostAuthorize注解實(shí)現(xiàn)邏輯
繼承根節(jié)點(diǎn):SecurityMetaSource
可以通過(guò)Spring注解聲明,需要依賴(lài)類(lèi)注入,實(shí)現(xiàn)權(quán)限靈活配置如:
@Component("securityMetadataSource") public class MySecurityMetadataSource implements FilterInvocationSecurityMetadataSource PrePostAnnotationSecurityMetadataSource類(lèi)繼承關(guān)系 AbstractMethodSecurityMetadataSource類(lèi)繼承關(guān)系
package org.springframework.security.access.prepost; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.security.access.ConfigAttribute; import org.springframework.security.access.method.AbstractMethodSecurityMetadataSource; import org.springframework.util.ClassUtils; public class PrePostAnnotationSecurityMetadataSource extends AbstractMethodSecurityMetadataSource { private final PrePostInvocationAttributeFactory attributeFactory; public PrePostAnnotationSecurityMetadataSource(PrePostInvocationAttributeFactory attributeFactory) { this.attributeFactory = attributeFactory; } public Collection<ConfigAttribute> getAttributes(Method method, Class<?> targetClass) { if (method.getDeclaringClass() == Object.class) { return Collections.emptyList(); } else { this.logger.trace("Looking for Pre/Post annotations for method '" + method.getName() + "' on target class '" + targetClass + "'"); PreFilter preFilter = (PreFilter)this.findAnnotation(method, targetClass, PreFilter.class); PreAuthorize preAuthorize = (PreAuthorize)this.findAnnotation(method, targetClass, PreAuthorize.class); PostFilter postFilter = (PostFilter)this.findAnnotation(method, targetClass, PostFilter.class); PostAuthorize postAuthorize = (PostAuthorize)this.findAnnotation(method, targetClass, PostAuthorize.class); if (preFilter == null && preAuthorize == null && postFilter == null && postAuthorize == null) { this.logger.trace("No expression annotations found"); return Collections.emptyList(); } else { String preFilterAttribute = preFilter == null ? null : preFilter.value(); String filterObject = preFilter == null ? null : preFilter.filterTarget(); String preAuthorizeAttribute = preAuthorize == null ? null : preAuthorize.value(); String postFilterAttribute = postFilter == null ? null : postFilter.value(); String postAuthorizeAttribute = postAuthorize == null ? null : postAuthorize.value(); ArrayList<ConfigAttribute> attrs = new ArrayList(2); PreInvocationAttribute pre = this.attributeFactory.createPreInvocationAttribute(preFilterAttribute, filterObject, preAuthorizeAttribute); if (pre != null) { attrs.add(pre); } PostInvocationAttribute post = this.attributeFactory.createPostInvocationAttribute(postFilterAttribute, postAuthorizeAttribute); if (post != null) { attrs.add(post); } attrs.trimToSize(); return attrs; } } } public Collection<ConfigAttribute> getAllConfigAttributes() { return null; } private <A extends Annotation> A findAnnotation(Method method, Class<?> targetClass, Class<A> annotationClass) { Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass); A annotation = AnnotationUtils.findAnnotation(specificMethod, annotationClass); if (annotation != null) { this.logger.debug(annotation + " found on specific method: " + specificMethod); return annotation; } else { if (specificMethod != method) { annotation = AnnotationUtils.findAnnotation(method, annotationClass); if (annotation != null) { this.logger.debug(annotation + " found on: " + method); return annotation; } } annotation = AnnotationUtils.findAnnotation(specificMethod.getDeclaringClass(), annotationClass); if (annotation != null) { this.logger.debug(annotation + " found on: " + specificMethod.getDeclaringClass().getName()); return annotation; } else { return null; } } } }
//注解開(kāi)啟權(quán)限 @EnableResourceServer @EnableGlobalMethodSecurity SecurityContextHolder作為全局緩存,從上下文獲取授權(quán)信息 Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
上面權(quán)限列表初始化由具體實(shí)現(xiàn)類(lèi)實(shí)現(xiàn):
public class User implements UserDetails, CredentialsContainer { ... private final Set<GrantedAuthority> authorities; ... //authorities權(quán)限列表 public User(String username, String password, Collection<? extends GrantedAuthority> authorities) { this(username, password, true, true, true, true, authorities); }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 詳解Spring Security中權(quán)限注解的使用
- 詳解如何在Spring?Security中自定義權(quán)限表達(dá)式
- SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)動(dòng)態(tài)權(quán)限問(wèn)題
- Spring?Security權(quán)限管理實(shí)現(xiàn)接口動(dòng)態(tài)權(quán)限控制
- Spring Security動(dòng)態(tài)權(quán)限的實(shí)現(xiàn)方法詳解
- Spring?Security權(quán)限注解啟動(dòng)及邏輯處理使用示例
相關(guān)文章
Spring依賴(lài)注入的兩種方式(根據(jù)實(shí)例詳解)
這篇文章主要介紹了Spring依賴(lài)注入的兩種方式(根據(jù)實(shí)例詳解),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05Java線(xiàn)程實(shí)現(xiàn)的兩種方式解析
這篇文章主要介紹了Java線(xiàn)程實(shí)現(xiàn)的兩種方式解析,注意在構(gòu)造器中啟動(dòng)這個(gè)線(xiàn)程的話(huà),很容易造成this逃逸的問(wèn)題,這是要注意的,這是通過(guò)直接集成thread來(lái)成為線(xiàn)程,同時(shí)在這種情況下,你可以通過(guò)調(diào)用合適的方法來(lái),需要的朋友可以參考下2024-01-01JAVA下單接口優(yōu)化實(shí)戰(zhàn)TPS性能提高10倍
今天小編就為大家分享一篇關(guān)于JAVA下單接口優(yōu)化實(shí)戰(zhàn)TPS性能提高10倍,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Java中的IP地址和InetAddress類(lèi)使用詳解
這篇文章主要介紹了Java中的IP地址和InetAddress類(lèi)使用詳解,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10