做网站用的什么服务器,重庆装修公司10强,公众号买粉平台,网站建设文件名目录
#x1f37b;前言
#x1f378;一、鉴权#xff08;Authorization#xff09;
#x1f37a;二、功能实现 2.1 环境准备 2.2 代码实现 2.3 测试接口
#x1f379;三、测试功能 3.1 传递 admin 请求
3.2 传递普通 user 请求
#x1f37b;四、章末 前言
一、鉴权Authorization
二、功能实现 2.1 环境准备 2.2 代码实现 2.3 测试接口
三、测试功能 3.1 传递 admin 请求
3.2 传递普通 user 请求
四、章末 前言 小伙伴们大家好最近有一段时间没更了有一部分是工作上的原因还有一部分生活上的小事给耽搁了之前的文章链接如下这次继续来学习下 Spring EL 表达式的另一种方式结合切面类实现鉴权的功能
【Spring EL一✈️ 】SL 表达式的应用-CSDN博客
一、鉴权Authorization 也称为授权是系统安全中的一个重要环节。用于确定已经通过认证的用户是否有权限访问某个资源或执行某个操作。鉴权确保只有具有适当权限的用户可以执行特定的操作从而保护系统的资源和数据。 下面是一些常见的鉴权方式 基于角色的访问控制RBAC 用户被赋予一个或多个角色每个角色拥有一组权限。鉴权时根据用户的角色来决定其是否有权限执行某操作。 基于属性的访问控制ABAC 检查用户属性、环境属性、资源属性等多种因素。使用这些属性和策略规则来进行复杂的鉴权决策。 访问控制列表ACL 每个资源有一个列表列出哪些用户或系统进程对该资源有哪些权限。直接针对资源的授权管理。 二、功能实现 本地实现的是基于角色访问控制的鉴权 2.1 环境准备 创建一个简单的 Springboot 项目确保可以正常启动 引入AOP 的依赖以及打开使用权限 2.2 代码实现 2.2.1 注解定义 简单定义了一个注解指明了该注解的作用域为方法上并且运行时可存在
import java.lang.annotation.*;Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
Documented
public interface CheckPermission {String value();
} 2.2.2 注解切面类实现 这里没有使用切入点PointCut),是因为该切面类中只有一个 Before 操作如果有多个操作适合使用 PointCut 标注就不用每个方法上都加 annotation(com.example.demo.testCode.springel.CheckPermission) 了
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;Aspect
Component
public class PermissionAspect {Before(annotation(com.example.demo.testCode.springel.CheckPermission))public void checkUserPermission(JoinPoint joinPoint) throws Throwable {MethodSignature signature (MethodSignature) joinPoint.getSignature();Method method signature.getMethod();CheckPermission checkPermission method.getAnnotation(CheckPermission.class);String spelExpression checkPermission.value();// 使用Spring EL表达式解析器ExpressionParser parser new SpelExpressionParser();StandardEvaluationContext context new StandardEvaluationContext();// 将方法参数放入上下文中Object[] args joinPoint.getArgs();String[] paramNames signature.getParameterNames();for (int i 0; i args.length; i) {context.setVariable(paramNames[i], args[i]);}// 模拟权限检查逻辑Boolean hasPermission parser.parseExpression(spelExpression).getValue(context, Boolean.class);if (!hasPermission) {throw new SecurityException(User does not have permission to perform this action);}}} 2.3 测试接口 简单的暴露一个接口供测试使用这里使用的 SpringEL 表达式是“identity admin ,就是简单的判断下当前访问该接口的用户的身份是否为 admin,当然这只是一个接口别的接口可以随意改变鉴权方式以及权限要求
GetMapping(/checkPermission)CheckPermission(value #identity admin)public void checkPermission(String identity){// 业务逻辑System.out.println(Reading sensitive data for user: identity);}
三、测试功能 3.1 传递 admin 请求 模拟 admin 用户访问该接口结果如图通过了切面类的 SpringEL 表达式校验 3.2 传递普通 user 请求 模拟普通用户请求访问该接口结果如图所示普通用户请求被拦截 这里只是简单的打印了日志实际应用中可操作空间很大比如不同的用户执行不同的业务逻辑或者不同的用户抛出不同的异常信息再用全局异常处理器实现不同的提示操作等 四、章末 文章到这里就结束了~