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

SpringBoot?AspectJ切面配合自定義注解實現權限校驗的示例詳解

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

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

1. 創(chuàng)建權限校驗注解

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

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 {
    /**
     * 所需的權限點
     *
     * @return 所需的權限點
     */
    String authPoint();
}

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

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

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 {
    // 權限校驗服務
    @Resource
    private PermissionService permissionService;
    // 定義切入點表達式,匹配帶有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.獲取目標類上的目標注解
        PermissionCheck annotationInClass = AnnotationUtils.findAnnotation(signature.getDeclaringType(), PermissionCheck.class);
        // 2.獲取目標方法上的目標注解
        PermissionCheck annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), PermissionCheck.class);
        // 優(yōu)先取方法上的注解,若方法上無注解,則取類上的注解
        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)) { // 進行權限校驗
            return joinPoint.proceed();
        } else {
            log.warn("user [{}] no permission, authPoint: {}", SessionUtils.getCurrentUser().getFullName(), authPoint);
            throw new RuntimeException("no permission: [" + authPoint + "]");
        }
    }
}

PermissionService的hasAuthPoint方法用于判斷當前用戶是否擁有所需權限點。

例如,將用戶的擁有的權限點集合存儲到Session中,校驗時通過Session拿到用戶的權限點集合進行判斷

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")
// 如果在類上使用注解,則對該類下所有public方法生效
// @PermissionCheck(authPoint = AuthPointConstant.EMPLOYEE_MANAGE)
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;
    @PermissionCheck(authPoint = AuthPointConstant.ADD_EMPLOYEE) // 在方法上使用校驗注解
    @PostMapping("add")
    public Response<?> add(@RequestBody @Valid AddEmployeeRequest request) {
        employeeService.add(request);
        return Response.success();
    }
}

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

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

A. 參考文章

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

相關文章

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

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

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

    java&javascript自定義加密數據傳輸代碼示例

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

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

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

    關于在IDEA熱部署插件JRebel使用問題詳解

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

    java_String和StringBuffer區(qū)別分析

    JAVA平臺提供了兩個類:String和StringBuffer,它們可以儲存和操作字符串,即包含多個字符的字符數據。這個String類提供了數值不可改變的字符串。
    2013-04-04
  • spring boot 2整合swagger-ui過程解析

    spring boot 2整合swagger-ui過程解析

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

    Spring Boot Actuator未授權訪問漏洞的問題解決

    Spring Boot Actuator 端點的未授權訪問漏洞是一個安全性問題,可能會導致未經授權的用戶訪問敏感的應用程序信息,本文就來介紹一下解決方法,感興趣的可以了解一下
    2023-09-09
  • java數組實現隊列及環(huán)形隊列實現過程解析

    java數組實現隊列及環(huán)形隊列實現過程解析

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

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

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

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

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

最新評論

唐海县| 淳化县| 北安市| 西林县| 洛阳市| 汉中市| 仪征市| 伊川县| 惠水县| 和政县| 遵义市| 阳泉市| 友谊县| 通榆县| 普定县| 芦山县| 连城县| 翼城县| 长子县| 太仆寺旗| 谢通门县| 洛阳市| 绥化市| 本溪市| 泽普县| 积石山| 赤城县| 酒泉市| 桑植县| 榆中县| 河北区| 东乡族自治县| 高平市| 德格县| 桐梓县| 博爱县| 罗山县| 尼玛县| 新郑市| 襄垣县| 信丰县|