温州哪里做网站设计,wordpress免签约支付,现在网站给源码,营销型网站设计注意AOP到底是啥 前言面向切面编程到底是啥意思那么要怎么实现面向切面编程呢#xff1f;成果 前言
回忆起来#xff0c;第一次听到这三字母是博主在上大二的时候#xff0c;那时候看的一脸懵逼#xff0c;现在马上研二了才想起来回顾下。
只记得当时面向对象编程还没整明白成果 前言
回忆起来第一次听到这三字母是博主在上大二的时候那时候看的一脸懵逼现在马上研二了才想起来回顾下。
只记得当时面向对象编程还没整明白这一下子又来个面向切面编程这直接给整懵了。。。。 面向对象相信大家都很了解了那咱们接下来看看什么是面向切面编程。
面向切面编程到底是啥意思
咱不整哪些官方话术了整点通俗的。
想象以下这样的一个场景你接手了一个人员管理系统这个项目并没有做任何的权限管理并且大部分功能已经完成这时候甲方突然来了个新需求我现在希望你加上对人员管理的权限即部门经理只能管理其部门的人员不能管理其他部门的人员。
这时候你可能想到要不加个安全框架但很快你就放弃了因为这种需要给权限的接口实在是太多了总不能一个个根据用户权限限制管理权限这么去改吧。
这时候aop的作用就出现了 那可以怎么做呢
我们通常会将包含接口的Controller都放在一个package中这时候我们就在想我们可不可以在职工id传递给接口之前对传递的职工id操作一下呢
即前端传递过来之后我们在对这些id再进行一次审核将没有权限管理的职工id删掉可以不
很幸运可以
我们可以认为客户端到调用接口这之间存在一个面在这个面上我们可以处理将要传递给接口的id参数进行处理将那些没有权限操作的id直接删除。这个面就可以认为是切面面向切面编程就是这样。
这里只是一个例子切面不仅存在这种情况中对象和对象之间方法和方法之间等等之间都可以认为是一个切面。
那么要怎么实现面向切面编程呢
这里我们用注解的方式实现
首先就是我们怎么监视这个面呢 先看下接口
package com.xiaow.springsecuriydemo.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaow.springsecuriydemo.entity.Person;
import com.xiaow.springsecuriydemo.service.PersonService;
import com.xiaow.springsecuriydemo.vo.Result;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Collection;
import java.util.List;/*** p* 前端控制器* /p** author xiaow* since 2023-09-01*/
RestController
RequestMapping(/person)
public class PersonController {AutowiredPersonService personService;GetMapping(getPersons)public Result getPersons(int adminid, int[] personids) {LambdaQueryWrapperPerson personLambdaQueryWrapper new LambdaQueryWrapper();List list1 Collections.arrayToList(personids);personLambdaQueryWrapper.in(Person::getId, list1);ListPerson list personService.list(personLambdaQueryWrapper);return Result.succ(list);}}
这里的mplus操作就交给大家去写了。
来了解下怎么写Aspect
Before(value execution(* com.xiaow.springsecuriydemo.controller.*.*(..)))Before也就是在接口执行前我们对其进行操作com.xiaow.springsecuriydemo.controller就是我们存放controller的包后面的第一个*代表任意一个Controller第二个* 就是任意的方法
其意义就是在com.xiaow.springsecuriydemo.controller下的所有接口执行前做一步处理那么处理什么呢
当然是处理传递的员工id就是把那些无权限处理的id替代掉这里我们使用-1来代替因为id为-1不会指向任何员工信息。
看下完整的代码
package com.xiaow.springsecuriydemo.aop.advice;import com.xiaow.springsecuriydemo.aop.annotation.ArgsAnnotation;
import com.xiaow.springsecuriydemo.aop.annotation.ArraysAnnotation;
import com.xiaow.springsecuriydemo.entity.Admin;
import com.xiaow.springsecuriydemo.entity.Person;
import com.xiaow.springsecuriydemo.service.AdminService;
import com.xiaow.springsecuriydemo.service.PersonService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;Aspect
Component
public class PersonAdvice {AutowiredAdminService adminService;AutowiredPersonService personService;// 在进入接口之前执行Before(value execution(* com.xiaow.springsecuriydemo.controller.*.*(..)))public void before(JoinPoint joinPoint) throws IllegalAccessException, NoSuchFieldException {final Signature signature joinPoint.getSignature();MethodSignature methodSignature (MethodSignature) signature;// 获取参数的名字final String[] names methodSignature.getParameterNames();// 获取参数的值final Object[] args joinPoint.getArgs();int admin_index -1;int personid_index -1;// 获取到adminid和personids的下标位置for (int i 0; i names.length; i) {if (names[i].equals(adminid)) {admin_index i;}if (names[i].equals(personids)) {personid_index i;}}Integer adminid (Integer) args[admin_index];int [] personid (int[]) args[personid_index];Admin byId adminService.getById(adminid);if (byId ! null) {Integer deptid byId.getDeptid();ListPerson byDeptIdAndPersonids personService.getByDeptIdAndPersonids(deptid, personid);int[] personids (int[]) args[personid_index];for (Integer i 0; i personids.length; i) {boolean flag false;for (Person byDeptIdAndPersonid : byDeptIdAndPersonids) {if (personids[i] byDeptIdAndPersonid.getId())flag true;}
// 若不在有权限的id中则直接设置为-1if (!flag)Array.setInt(personid, i, -1);}}}// 在接口执行完毕后执行After(value execution(* com.xiaow.springsecuriydemo.controller.*.*(..)))public void after() {System.out.println(after);}}成果
就这样我们在不修改原有的接口的情况下实现了权限控制如下图