自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

三個注解,優(yōu)雅的實現(xiàn)微服務(wù)鑒權(quán)

開發(fā) 架構(gòu)
本文主要介紹了微服務(wù)中如何將鑒權(quán)下放到微服務(wù)中,也是為了解決讀者的疑惑,實際生產(chǎn)中除非業(yè)務(wù)需要,陳某還是建議將鑒權(quán)統(tǒng)一放到網(wǎng)關(guān)中。

實現(xiàn)思路

前面的幾篇文章陳某都是將鑒權(quán)和認證統(tǒng)一的放在了網(wǎng)關(guān)層面,架構(gòu)如下:

圖片

微服務(wù)中的鑒權(quán)還有另外一種思路:將鑒權(quán)交給下游的各個微服務(wù),網(wǎng)關(guān)層面只做路由轉(zhuǎn)發(fā)。

這種思路其實實現(xiàn)起來也是很簡單,下面針對網(wǎng)關(guān)層面鑒權(quán)的代碼改造一下即可完成:實戰(zhàn)干貨!Spring Cloud Gateway 整合 OAuth2.0 實現(xiàn)分布式統(tǒng)一認證授權(quán)!

1. 干掉鑒權(quán)管理器

在網(wǎng)關(guān)統(tǒng)一鑒權(quán)實際是依賴的鑒權(quán)管理器ReactiveAuthorizationManager,所有的請求都需要經(jīng)過鑒權(quán)管理器的去對登錄用戶的權(quán)限進行鑒權(quán)。

這個鑒權(quán)管理器在網(wǎng)關(guān)鑒權(quán)的文章中也有介紹,在陳某的《Spring Cloud Alibaba 實戰(zhàn)》中配置攔截也很簡單,如下:

圖片

除了配置的白名單,其他的請求一律都要被網(wǎng)關(guān)的鑒權(quán)管理器攔截鑒權(quán),只有鑒權(quán)通過才能放行路由轉(zhuǎn)發(fā)給下游服務(wù)。

看到這里思路是不是很清楚了,想要將鑒權(quán)交給下游服務(wù),只需要在網(wǎng)關(guān)層面直接放行,不走鑒權(quán)管理器,代碼如下:

http
....
//白名單直接放行
.pathMatchers(ArrayUtil.toArray(whiteUrls.getUrls(), String.class)).permitAll()
//其他的任何請求直接放行
.anyExchange().permitAll()
.....

2. 定義三個注解

經(jīng)過第①步,鑒權(quán)已經(jīng)下放給下游服務(wù)了,那么下游服務(wù)如何進行攔截鑒權(quán)呢?

其實Spring Security 提供了3個注解用于控制權(quán)限,如下:

  • @Secured
  • @PreAuthorize
  • @PostAuthorize

關(guān)于這三個注解就不再詳細介紹了,有興趣的可以去查閱官方文檔。

陳某這里并不打算使用的內(nèi)置的三個注解實現(xiàn),而是自定義了三個注解,如下:

1)@RequiresLogin

見名知意,只有用戶登錄才能放行,代碼如下:

/**
* @author 公眾號:碼猿技術(shù)專欄
* @url: www.java-family.cn
* @description 登錄認證的注解,標注在controller方法上,一定要是登錄才能的訪問的接口
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RequiresLogin {
}

2)@RequiresPermissions

見名知意,只有擁有指定權(quán)限才能放行,代碼如下:

/**
* @author 公眾號:碼猿技術(shù)專欄
* @url: www.java-family.cn
* @description 標注在controller方法上,確保擁有指定權(quán)限才能訪問該接口
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RequiresPermissions {
/**
* 需要校驗的權(quán)限碼
*/
String[] value() default {};

/**
* 驗證模式:AND | OR,默認AND
*/
Logical logical() default Logical.AND;
}

3.@RequiresRoles

見名知意,只有擁有指定角色才能放行,代碼如下:

/**
* @author 公眾號:碼猿技術(shù)專欄
* @url: www.java-family.cn
* @description 標注在controller方法上,確保擁有指定的角色才能訪問該接口
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface RequiresRoles {
/**
* 需要校驗的角色標識,默認超管和管理員
*/
String[] value() default {OAuthConstant.ROLE_ROOT_CODE,OAuthConstant.ROLE_ADMIN_CODE};

/**
* 驗證邏輯:AND | OR,默認AND
*/
Logical logical() default Logical.AND;
}

以上三個注解的含義想必都很好理解,這里就不再解釋了....

3. 注解切面定義

注解有了,那么如何去攔截呢?這里陳某定義了一個切面進行攔截,關(guān)鍵代碼如下:

/**
* @author 公眾號:碼猿技術(shù)專欄
* @url: www.java-family.cn
* @description @RequiresLogin,@RequiresPermissions,@RequiresRoles 注解的切面
*/
@Aspect
@Component
public class PreAuthorizeAspect {
/**
* 構(gòu)建
*/
public PreAuthorizeAspect() {
}

/**
* 定義AOP簽名 (切入所有使用鑒權(quán)注解的方法)
*/
public static final String POINTCUT_SIGN = " @annotation(com.mugu.blog.common.annotation.RequiresLogin) || "
+ "@annotation(com.mugu.blog.common.annotation.RequiresPermissions) || "
+ "@annotation(com.mugu.blog.common.annotation.RequiresRoles)";

/**
* 聲明AOP簽名
*/
@Pointcut(POINTCUT_SIGN)
public void pointcut() {
}

/**
* 環(huán)繞切入
*
* @param joinPoint 切面對象
* @return 底層方法執(zhí)行后的返回值
* @throws Throwable 底層方法拋出的異常
*/
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// 注解鑒權(quán)
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
checkMethodAnnotation(signature.getMethod());
try {
// 執(zhí)行原有邏輯
Object obj = joinPoint.proceed();
return obj;
} catch (Throwable e) {
throw e;
}
}

/**
* 對一個Method對象進行注解檢查
*/
public void checkMethodAnnotation(Method method) {
// 校驗 @RequiresLogin 注解
RequiresLogin requiresLogin = method.getAnnotation(RequiresLogin.class);
if (requiresLogin != null) {
doCheckLogin();
}

// 校驗 @RequiresRoles 注解
RequiresRoles requiresRoles = method.getAnnotation(RequiresRoles.class);
if (requiresRoles != null) {
doCheckRole(requiresRoles);
}

// 校驗 @RequiresPermissions 注解
RequiresPermissions requiresPermissions = method.getAnnotation(RequiresPermissions.class);
if (requiresPermissions != null) {
doCheckPermissions(requiresPermissions);
}
}


/**
* 校驗有無登錄
*/
private void doCheckLogin() {
LoginVal loginVal = SecurityContextHolder.get();
if (Objects.isNull(loginVal))
throw new ServiceException(ResultCode.INVALID_TOKEN.getCode(), ResultCode.INVALID_TOKEN.getMsg());
}

/**
* 校驗有無對應(yīng)的角色
*/
private void doCheckRole(RequiresRoles requiresRoles){
String[] roles = requiresRoles.value();
LoginVal loginVal = OauthUtils.getCurrentUser();

//該登錄用戶對應(yīng)的角色
String[] authorities = loginVal.getAuthorities();
boolean match=false;

//and 邏輯
if (requiresRoles.logical()==Logical.AND){
match = Arrays.stream(authorities).filter(StrUtil::isNotBlank).allMatch(item -> CollectionUtil.contains(Arrays.asList(roles), item));
}else{ //OR 邏輯
match = Arrays.stream(authorities).filter(StrUtil::isNotBlank).anyMatch(item -> CollectionUtil.contains(Arrays.asList(roles), item));
}

if (!match)
throw new ServiceException(ResultCode.NO_PERMISSION.getCode(), ResultCode.NO_PERMISSION.getMsg());
}

/**
* TODO 自己實現(xiàn),由于并未集成前端的菜單權(quán)限,根據(jù)業(yè)務(wù)需求自己實現(xiàn)
*/
private void doCheckPermissions(RequiresPermissions requiresPermissions){

}
}

其實這中間的邏輯非常簡單,就是解析的Token中的權(quán)限、角色然后和注解中的指定的進行比對。

@RequiresPermissions這個注解的邏輯陳某并未實現(xiàn),自己根據(jù)業(yè)務(wù)模仿著完成,算是一道思考題了....

4. 注解使用

比如《Spring Cloud Alibaba 實戰(zhàn)》項目中有一個添加文章的接口,只有超管和管理員的角色才能添加,那么可以使用@RequiresRoles注解進行標注,如下:

@RequiresRoles
@AvoidRepeatableCommit
@ApiOperation("添加文章")
@PostMapping("/add")
public ResultMsg<Void> add(@RequestBody @Valid ArticleAddReq req){
.......
}

效果這里就不演示了,實際的效果:非超管和管理員角色用戶登錄訪問,將會直接被攔截,返回?zé)o權(quán)限。

注意:這里僅僅解決了下游服務(wù)鑒權(quán)的問題,那么feign調(diào)用是否也適用?

當然適用,這里使用的是切面方式,feign內(nèi)部其實使用的是http方式調(diào)用,對于接口來說一樣適用。

比如《Spring Cloud Alibaba 實戰(zhàn)》項目中獲取文章列表的接口,其中會通過feign的方式調(diào)用評論服務(wù)中的接口獲取文章評論總數(shù),這里一旦加上了@RequiresRoles,那么調(diào)用將會失敗,代碼如下:

@RequiresRoles
@ApiOperation(value = "批量獲取文章總數(shù)")
@PostMapping(value = "/list/total")
public ResultMsg<List<TotalVo>> listTotal(@RequestBody @Valid List<CommentListReq> param){
....
}

總結(jié)

本文主要介紹了微服務(wù)中如何將鑒權(quán)下放到微服務(wù)中,也是為了解決讀者的疑惑,實際生產(chǎn)中除非業(yè)務(wù)需要,陳某還是建議將鑒權(quán)統(tǒng)一放到網(wǎng)關(guān)中。

責(zé)任編輯:武曉燕 來源: 碼猿技術(shù)專欄
相關(guān)推薦

2023-04-17 08:56:29

微服務(wù)鑒權(quán)業(yè)務(wù)

2018-01-10 14:22:05

2022-12-02 16:28:47

2022-05-13 14:01:46

微服務(wù)架構(gòu)安全微服務(wù)

2022-04-08 09:00:00

微服務(wù)架構(gòu)安全防火墻

2024-01-10 09:00:00

云計算架構(gòu)

2023-12-20 16:26:43

微服務(wù)軟件開發(fā)

2024-11-08 15:56:36

2022-06-21 14:44:38

接口數(shù)據(jù)脫敏

2024-11-07 10:55:26

2025-02-23 08:00:00

冪等性Java開發(fā)

2023-08-07 09:12:51

權(quán)限SpringSecurity

2012-02-21 10:30:35

業(yè)務(wù)靈活性IT架構(gòu)

2019-09-25 08:57:24

單體式架構(gòu)微服務(wù)

2023-04-26 11:14:11

IT領(lǐng)導(dǎo)者遠程工作

2022-10-13 14:15:35

商業(yè)智能大數(shù)據(jù)工具

2021-07-07 07:44:20

微服務(wù)Nacos緩存

2010-09-01 09:08:31

VMwareIT即服務(wù)

2020-07-28 00:42:20

數(shù)據(jù)安全SAAS服務(wù)網(wǎng)絡(luò)安全

2010-06-28 13:40:22

SNMP協(xié)議服務(wù)
點贊
收藏

51CTO技術(shù)棧公眾號