最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot?AspectJ切面配合自定義注解實(shí)現(xiàn)權(quán)限校驗(yàn)的示例詳解

 更新時(shí)間:2025年09月15日 17:29:11   作者:GFIRE1999  
本文章介紹了如何通過(guò)創(chuàng)建自定義的權(quán)限校驗(yàn)注解,配合AspectJ切面攔截注解實(shí)現(xiàn)權(quán)限校驗(yàn),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

本文章介紹了如何通過(guò)創(chuàng)建自定義的權(quán)限校驗(yàn)注解,配合AspectJ切面攔截注解實(shí)現(xiàn)權(quán)限校驗(yàn)。

1. 創(chuàng)建權(quán)限校驗(yàn)注解

創(chuàng)建權(quán)限校驗(yàn)注解,可用在方法和類上,authPoint屬性表示所需的權(quán)限點(diǎn)。代碼如下:

package com.guo.demo.examples.permissioncheck;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface PermissionCheck {
    /**
     * 所需的權(quán)限點(diǎn)
     *
     * @return 所需的權(quán)限點(diǎn)
     */
    String authPoint();
}

2. 創(chuàng)建AspectJ切面攔截注解校驗(yàn)權(quán)限

創(chuàng)建AspectJ切面,攔截帶有@PermissionCheck注解的方法或類,獲取注解上的權(quán)限點(diǎn)進(jìn)行校驗(yàn)。代碼如下:

package com.guo.demo.examples.permissioncheck;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Slf4j
@Aspect
@Component
public class PermissionCheckAspect {
    // 權(quán)限校驗(yàn)服務(wù)
    @Resource
    private PermissionService permissionService;
    // 定義切入點(diǎn)表達(dá)式,匹配帶有PermissionCheck注解的方法或類
    @Pointcut("@annotation(com.guo.demo.examples.permissioncheck.PermissionCheck) || @within(com.guo.demo.examples.permissioncheck.PermissionCheck)")
    public void pointCut() {
    }
    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 1.獲取目標(biāo)類上的目標(biāo)注解
        PermissionCheck annotationInClass = AnnotationUtils.findAnnotation(signature.getDeclaringType(), PermissionCheck.class);
        // 2.獲取目標(biāo)方法上的目標(biāo)注解
        PermissionCheck annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), PermissionCheck.class);
        // 優(yōu)先取方法上的注解,若方法上無(wú)注解,則取類上的注解
        PermissionCheck annotation = annotationInMethod != null ? annotationInMethod : annotationInClass;
        if (annotation == null) {
            log.error("PermissionCheck annotation is null, {}", signature.toLongString());
            throw new RuntimeException("PermissionCheck annotation is null");
        }
        String authPoint = annotation.authPoint();
        if (permissionService.hasAuthPoint(authPoint)) { // 進(jìn)行權(quán)限校驗(yàn)
            return joinPoint.proceed();
        } else {
            log.warn("user [{}] no permission, authPoint: {}", SessionUtils.getCurrentUser().getFullName(), authPoint);
            throw new RuntimeException("no permission: [" + authPoint + "]");
        }
    }
}

PermissionService的hasAuthPoint方法用于判斷當(dāng)前用戶是否擁有所需權(quán)限點(diǎn)。

例如,將用戶的擁有的權(quán)限點(diǎn)集合存儲(chǔ)到Session中,校驗(yàn)時(shí)通過(guò)Session拿到用戶的權(quán)限點(diǎn)集合進(jìn)行判斷

3. 用法示例

package com.guo.demo.examples.permissioncheck;
import com.guo.demo.pojo.vo.Response;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
@RestController
@RequestMapping("employee")
// 如果在類上使用注解,則對(duì)該類下所有public方法生效
// @PermissionCheck(authPoint = AuthPointConstant.EMPLOYEE_MANAGE)
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @PermissionCheck(authPoint = AuthPointConstant.ADD_EMPLOYEE) // 在方法上使用校驗(yàn)注解
    @PostMapping("add")
    public Response<?> add(@RequestBody @Valid AddEmployeeRequest request) {
        employeeService.add(request);
        return Response.success();
    }
}

AuthPointConstant是一個(gè)用于存放權(quán)限點(diǎn)常量的類,方便統(tǒng)一集中管理權(quán)限點(diǎn)。比如:

public class AuthPointConstant {
    public static final String EMPLOYEE_MANAGE = "employee:manage"; // 管理員工
    public static final String ADD_EMPLOYEE = "employee:add"; // 添加員工
}

A. 參考文章

到此這篇關(guān)于SpringBoot AspectJ切面配合自定義注解實(shí)現(xiàn)權(quán)限校驗(yàn)的示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot AspectJ權(quán)限校驗(yàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis-Plus中使用@DS注解動(dòng)態(tài)選擇數(shù)據(jù)源的源碼解讀

    Mybatis-Plus中使用@DS注解動(dòng)態(tài)選擇數(shù)據(jù)源的源碼解讀

    這篇文章主要介紹了Mybatis-Plus中使用@DS注解動(dòng)態(tài)選擇數(shù)據(jù)源的源碼解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • java&javascript自定義加密數(shù)據(jù)傳輸代碼示例

    java&javascript自定義加密數(shù)據(jù)傳輸代碼示例

    這篇文章主要介紹了java&javascript自定義加密數(shù)據(jù)傳輸代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Hibernate之環(huán)境搭建及demo分享

    Hibernate之環(huán)境搭建及demo分享

    下面小編就為大家分享一篇Hibernate之環(huán)境搭建及demo,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-11-11
  • 關(guān)于在IDEA熱部署插件JRebel使用問(wèn)題詳解

    關(guān)于在IDEA熱部署插件JRebel使用問(wèn)題詳解

    這篇文章主要介紹了關(guān)于在IDEA熱部署插件JRebel使用問(wèn)題詳解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java_String和StringBuffer區(qū)別分析

    java_String和StringBuffer區(qū)別分析

    JAVA平臺(tái)提供了兩個(gè)類:String和StringBuffer,它們可以儲(chǔ)存和操作字符串,即包含多個(gè)字符的字符數(shù)據(jù)。這個(gè)String類提供了數(shù)值不可改變的字符串。
    2013-04-04
  • spring boot 2整合swagger-ui過(guò)程解析

    spring boot 2整合swagger-ui過(guò)程解析

    這篇文章主要介紹了spring boot 2整合swagger-ui過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Spring Boot Actuator未授權(quán)訪問(wèn)漏洞的問(wèn)題解決

    Spring Boot Actuator未授權(quán)訪問(wèn)漏洞的問(wèn)題解決

    Spring Boot Actuator 端點(diǎn)的未授權(quán)訪問(wèn)漏洞是一個(gè)安全性問(wèn)題,可能會(huì)導(dǎo)致未經(jīng)授權(quán)的用戶訪問(wèn)敏感的應(yīng)用程序信息,本文就來(lái)介紹一下解決方法,感興趣的可以了解一下
    2023-09-09
  • java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過(guò)程解析

    java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了java數(shù)組實(shí)現(xiàn)隊(duì)列及環(huán)形隊(duì)列實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 詳解Http協(xié)議以及post與get區(qū)別

    詳解Http協(xié)議以及post與get區(qū)別

    這篇文章主要介紹了詳解Http協(xié)議以及post與get區(qū)別,通過(guò)分別說(shuō)明Http協(xié)議以及get與post各自的概念,再到兩者作比較有著詳細(xì)的說(shuō)明,希望對(duì)你有所幫助
    2021-06-06
  • Mybatis核心配置文件加載流程詳解

    Mybatis核心配置文件加載流程詳解

    本文將介紹MyBatis在配置文件加載的過(guò)程中,如何加載核心配置文件、如何解析映射文件中的SQL語(yǔ)句以及每條SQL語(yǔ)句如何與映射接口的方法進(jìn)行關(guān)聯(lián),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12

最新評(píng)論

秦安县| 枣庄市| 朝阳市| 信阳市| 海原县| 元朗区| 娱乐| 凉城县| 南涧| 西宁市| 贵阳市| 孟连| 淳安县| 汝州市| 璧山县| 无为县| 望城县| 视频| 岳普湖县| 许昌市| 永吉县| 同仁县| 曲水县| 天等县| 鄄城县| 邹平县| 密山市| 厦门市| 蓝山县| 双流县| 开原市| 三原县| 康平县| 泾川县| 庐江县| 额敏县| 仁寿县| 安宁市| 普陀区| 中西区| 永州市|