通过自定义注解+AOP的处理方式实现权限控制

embedded/2024/10/18 14:25:05/

通过自定义注解+AOP切面的方式,可以实现日志记录、权限控制、性能监控、事务管理、缓存管理、异常处理等功能,以下以权限控制为例,介绍一下自定义注解+AOP处理方式的使用。

步骤1:定义注解

用于标记需要权限校验的方法

java">package com.example.security.annotation;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD) // 该注解应用于方法
@Retention(RetentionPolicy.RUNTIME) // 运行时保留,以便AOP切面可以访问
public @interface RequirePermission {String value(); // 定义一个属性,用于指定需要的权限
}

步骤2:编写切面

用于实现权限校验逻辑。

java">package com.example.security.aspect;import com.example.security.annotation.RequirePermission;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;@Aspect
@Component
public class PermissionCheckAspect {@Autowiredprivate UserService userService; // 假设UserService用于获取用户信息和权限@Around("@annotation(permissionAnnotation)")public Object checkPermission(ProceedingJoinPoint joinPoint, RequirePermission permissionAnnotation) throws Throwable {// 获取当前认证信息Authentication authentication = SecurityContextHolder.getContext().getAuthentication();if (authentication == null || !authentication.isAuthenticated()) {throw new UnauthorizedException("用户未认证");}// 获取用户权限,这里简化处理,实际情况可能更复杂String requiredPermission = permissionAnnotation.value();if (!userService.hasPermission(authentication.getName(), requiredPermission)) {throw new UnauthorizedException("没有足够的权限访问:" + requiredPermission);}// 权限校验通过,执行原方法return joinPoint.proceed();}
}

步骤3:使用注解

在需要权限控制的服务方法上使用@RequirePermission注解,并指定所需的权限。

java">package com.example.service;import com.example.security.annotation.RequirePermission;
import org.springframework.stereotype.Service;@Service
public class SomeService {@RequirePermission("ADMIN_ACCESS")public void performAdminAction() {// 需要管理员权限的方法逻辑}


http://www.ppmy.cn/embedded/52572.html

相关文章

6.20作业

1.已知网址www.hqyj.com截取出网址的每一个部分(要求,该网址不能存入文件中) echo www.hqyj.com | cut -d "." -f "1,2,3" 2.整理思维导图 3.将配置桥接网络的过程整理成文档,发csdn i)保证虚拟机提供了桥接模式 菜单…

【Docker】容器

目录 1. 容器启动 2. 容器启动/重启/停止 3. 进入容器 4. 容器查询 5. docker 镜像的构建 方式一:docker 容器 commit 方式二:Dockerfile 定制镜像 1. 容器启动 docker run –it/-d –p/P –name imageID/name 2. 容器启动/重启/停止 docker sta…

canvas如何让单行文本用...省略

let strWidth ctx.measureText(this.data.name).width; const ellipsis "..." const ellipsisWidth ctx.measureText(ellipsis).width; if(strWidth<120 || 120<ellipsisWidth) {ctx.fillText("测试:"this.data.name, 190*dpr,590*dpr); }else {va…

规则引擎-Aviator 表达式校验是否成立

目录 介绍特性使用更多文献支持 介绍 Aviator是一个轻量级、高性能的Java表达式执行引擎&#xff0c;它动态地将表达式编译成字节码并运行。 特性 支持绝大多数运算操作符&#xff0c;包括算术操作符、关系运算符、逻辑操作符、位运算符、正则匹配操作符(~)、三元表达式(?:…

Spring Boot基础入门

引言 Spring Boot是一个开源的Java框架&#xff0c;旨在简化Spring应用程序的创建和部署过程。它提供了一种快速和简便的方式来创建独立的、生产级别的基于Spring的应用程序。本文将介绍Spring Boot的基础知识&#xff0c;包括其核心特性、如何开始使用Spring Boot以及构建你的…

JDK 23:Loom改进版发布

1.新版 Loom EA 改进虚拟线程中的监视器&#xff08;同步方法&#xff09; Project Loom 发布了新的抢先体验版本(23-loom4-102 - 2024/5/31)。改进了对象监视器实现&#xff0c;可以防止虚拟线程在以下情况下固定其载体线程&#xff1a; 当进入同步方法/语句时发生阻塞&…

昇思25天学习打卡营第2天|linchenfengxue

传统的计算机视觉方法通常包括图像预处理、特征提取、特征筛选、图像识别等几个步骤。 对于给定的数字图像&#xff0c;计算机在处理时要先执行二次采样、平滑去噪、对比度提升和尺度调整等预处理操作&#xff0c;再对图像中的线条、边缘等全局特征和边角、斑点等局部特征&…

【前端】HTML+CSS复习记录【1】

文章目录 前言一、p、br&#xff08;段落、换行&#xff09;二、短语标签&#xff08;用来呈现为被强调的文本&#xff09;三、sub、sup&#xff08;下标、上标&#xff09;四、b&#xff08;加粗文本&#xff09;五、块级元素与行内元素六、元素嵌套七、html注释系列文章目录 …