SpringCloud 微服務實現數據權限控制

前章講了如何進行用戶權限驗證《SpringCloud下的用戶鑑權方案》,它是微服務下統一資源訪問權限的控制,就像一道牆保護着SpringCloud集羣下的各個業務應用服務。而本章要講的是權限控制的另一個層面數據權限,意思是控制可訪問數據資源的數量。

舉個例子:

有一批業務員跟進全國的銷售訂單。他們被按城市進行劃分,一個業務員跟進3個城市的訂單,爲了保護公司的業務數據不能被所有人都掌握,故每個業務員只能看到自己負責城市的訂單數據。所以從系統來講每個業務員都有訪問銷售訂單的功能,然後再需要配置每個業務員負責的城市,以此對訂單數據進行篩選。

要實現此功能有很多方法,如果系統中多個地方都需要類似的需求,那我們就可以將其提出來做成一個通用的功能。這裏我介紹一個相對簡單的解決方案,以供參考。

一、 整體架構

數據權限爲作一個註解的形式掛在每一個需要數據權限控制的Controller上,由於和具體的程序邏輯有關故有一定的入侵性,且需要數據庫配合使用。

二、 實現流程

  1. 瀏覽器傳帶查詢權限範圍參數訪問Controller,如cities
POST http://127.0.0.1:8000/order/query
accept: */*
Content-Type: application/json
token: 1e2b2298-8274-4599-a26f-a799167cc82f

{"cities":["cq","cd","bj"],"userName":"string"}
  1. 通過註解攔截權限範圍參數,並根據預授權範圍比較,回寫在授權範圍內的權限範圍參數

    cities = ["cq","cd"]
    
  2. 通過參數傳遞到DAO層,在SQL語句中拼裝出查詢條件,實現數據的過濾

    select * from order where city in ('cq','cd')
    

三、 實現步驟

1. 註解實現

註解的完整代碼,請詳見源代碼

1)創建註解

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD})
@Documented
public @interface ScopeAuth {

    String token() default "AUTH_TOKEN";
    String scope() default "";
    String[] scopes() default {};
}

此註解爲運行時RetentionPolicy.RUNTIME作用在方法上ElementType.METHOD

token:獲取識別唯一用戶的標識,與用戶數據權限存儲有關

scopescopes:預請求的數據權限範圍

2) AOP實現註解

public class ScopeAuthAdvice {
  
    @Around("@annotation(scopeAuth)")
    public Object before(ProceedingJoinPoint thisJoinPoint, ScopeAuth scopeAuth) throws Throwable {
        // ... 省略過程
        // 獲取token
        String authToken = getToken(args, scopeAuth.token(), methodSignature.getMethod());
     		// 回寫範圍參數
        setScope(scopeAuth.scope(), methodSignature, args, authToken);
        
        return thisJoinPoint.proceed();
    }

    /**
     * 設置範圍
     */
    private void setScope(String scope, MethodSignature methodSignature, Object[] args, String authToken) {
        // 獲取請求範圍
        Set<String> requestScope = getRequestScope(args, scope, methodSignature.getMethod());
        ScopeAuthAdapter adapter = new ScopeAuthAdapter(supplier);
        // 已授權範圍
        Set<String> authorizedScope = adapter.identifyPermissionScope(authToken, requestScope);
        // 回寫新範圍
        setRequestScope(args, scope, authorizedScope, methodSignature.getMethod());
    }

    /**
     * 回寫請求範圍
     */
    private void setRequestScope(Object[] args, String scopeName, Collection<String> scopeValues, Method method) {
        // 解析 SPEL 表達式
        if (scopeName.indexOf(SPEL_FLAG) == 0) {
            ParseSPEL.setMethodValue(scopeName, scopeValues, method, args);
        }
    }
}

此爲演示代碼省略了過程,主要功能爲通過token拿到預先授權的數據範圍,再與本次請求的範圍做交集,最後回寫回原參數。

過程中用到了較多的SPEL表達式,用於計算表達式結果,具體請參考ParseSPEL文件

3)權限範圍交集計算

public class ScopeAuthAdapter {

    private final AuthQuerySupplier supplier;

    public ScopeAuthAdapter(AuthQuerySupplier supplier) {
        this.supplier = supplier;
    }

    /**
     * 驗證權限範圍
     * @param token
     * @param requestScope
     * @return
     */
    public Set<String> identifyPermissionScope(String token, Set<String> requestScope) {
        Set<String> authorizeScope = supplier.queryScope(token);

        String ALL_SCOPE = "AUTH_ALL";
        String USER_ALL = "USER_ALL";

        if (authorizeScope == null) {
            return null;
        }

        if (authorizeScope.contains(ALL_SCOPE)) {
            // 如果是全開放則返回請求範圍
            return requestScope;
        }

        if (requestScope == null) {
            return null;
        }

        if (requestScope.contains(USER_ALL)){
            // 所有授權的範圍
            return authorizeScope;
        }

        // 移除不同的元素
        requestScope.retainAll(authorizeScope);

        return requestScope;
    }
}

此處爲了方便設置,有兩個關鍵字範圍

  • AUTH_ALL:預設所有範圍,全開放的意思,爲數據庫預先設置值,請求傳什麼值都通過
  • USER_ALL:請求所有授權的範圍,請求時傳此值則會以數據庫預設值爲準

4) spring.factories自動導入類配置

org.springframework.boot.autoconfigure.AutoConfigurationImportSelector=\
  fun.barryhome.cloud.annotation.ScopeAuthAdvice

如果註解功能是單獨項目存在,在使用時有可能會存在找不到引入文件的問題,可通過此配置文件自動載入需要初始化的類

2. 註解使用

@ScopeAuth(scopes = {"#orderDTO.cities"}, token = "#request.getHeader(\"X-User-Name\")")
@PostMapping(value = "/query")
public String query(@RequestBody OrderDTO orderDTO, HttpServletRequest request) {
	return Arrays.toString(orderDTO.getCities());
}

在需要使用數據權限的controller方法上增加@ScopeAuth註解

scopes = {"#orderDTO.cities"}:表示取輸入參數orderDTOcities值,這裏是表達式必須加**#**

實際開發過程中,需要將**orderDTO.getCities()**帶入後續邏輯中,在DAO層將此拼裝在SQL中,以實現數據過濾功能

3. 實現AuthStoreSupplier

AuthStoreSupplier接口爲數據權限的存儲接口,與AuthQuerySupplier配合使用,可按實際情況實現

此接口爲非必要接口,可由數據庫或Redis存儲(推薦),一般在登錄的同時保存在Redis

4. 實現AuthQuerySupplier

AuthQuerySupplier接口爲數據權限查詢接口,可按存儲方法進行查詢,推薦使用Redis

@Component
public class RedisAuthQuerySupplier implements AuthQuerySupplier {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 查詢範圍
     */
    @Override
    public Set<String> queryScope(String key) {
        String AUTH_USER_KEY = "auth:logic:user:%s";
        String redisKey = String.format(AUTH_USER_KEY, key);

        List<String> range = redisTemplate.opsForList().range(redisKey, 0, -1);

        if (range != null) {
            return new HashSet<>(range);
        } else {
            return null;
        }
    }
}

在分佈式結構裏,也可將此實現提出到權限模塊,採用遠程調用方式,進一步解耦

5. 開啓數據權限

@EnableScopeAuth
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

四、 綜述

至此數據權限功能就實現了。在微服務器架構中爲了實現功能的複用,將註解的創建AuthQuerySupplier的實現提取到公共模塊中,那麼在具體的使用模塊就簡單得多了。只需增加@ScopeAuth註解,配置好查詢方法就可以使用。

五、源代碼

文中代碼由於篇幅原因有一定省略並不是完整邏輯,如有興趣請Fork源代碼 https://gitee.com/hypier/barry-cloud/tree/master/cloud-auth-logic

六、請關注我的公衆號

請關注我的公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章