SpringBoot使用AOP

本文详细介绍如何在Spring Boot项目中使用AOP(面向切面编程),包括添加依赖、创建切面类进行权限检查和日志记录,以及具体测试代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.pom添加依赖

<!--AOP-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>RELEASE</version>
        </dependency>

二.创建切面类

package com.example.demo.aspect;

import com.example.demo.exception.MyException;
import com.example.demo.utils.ResultUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.logging.Logger;

/* *
 * Created by Ay on 2018/9/20
 */
@Aspect
@Component
public class ControllerAspect {


    @Pointcut("execution(public * com.example.demo.controller.TestController.errorTest(..))")
    public void check() {

    }

    @Before(value = "check()")
    public void checkBefore(JoinPoint joinPoint)
    {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("===============请求内容===============");
        System.out.println("请求地址:"+request.getRequestURL().toString());
        System.out.println("请求方式:"+request.getMethod());
        System.out.println("请求类方法:"+joinPoint.getSignature());
        System.out.println("请求类方法参数:"+ Arrays.toString(joinPoint.getArgs()));
        //检查权限
        HttpSession session = request.getSession();
        if(session.getAttribute("user")==null) {
            throw new MyException(-1,"无权限访问");
        }

    }

    @After(value = "check()")
    public void checkAfter() {
        System.out.println("aop after");
    }

}

@Aspect声明这是一个切面类
@Pointcut切点指明要切入的方法,check是签名
JoinPoint可以获得要请求的方法和请求的参数

三.测试代码

@RequestMapping(value = "error/{id}")
    public Object errorTest(@PathVariable("id") Integer id) {
        if(id == 0){
            throw new MyException(-1,"除0");
        }
        int result = 10 / id;
        return ResultUtil.success(result);
    }

    @RequestMapping(value = "login")
    @ResponseBody
    public Object loginTest(@RequestParam("username") String username, @RequestParam("password") String password,
                            HttpSession session){
        if("admin".equals(username) && "admin".equals(password)){
            session.setAttribute("user",username);
            return ResultUtil.success();
        }
        return ResultUtil.error(-1,"登录失败");
    }

打印内容
在这里插入图片描述
AOP通常可用于权限检查及访问日志

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值