aop日志(部分一)

部分一:
/**
 * aop日志:
 *       目标:记录用户的操作
 *       数据:用户传递的内容,我们返回给用户的结果等,是否还有其他的标记 备注等,比如我们正在执行的是login操作,我们是否加上别名,比如备注为登陆
 *       异常:我们需要处理异常
 */
使用步骤:

1、编写自定义注解 LogAnno

package com.qf.p2p.anno;


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)
public @interface LogAnno {

    String oprationtype();
    String oprationname();


}

2、将注解打在方法的上面

    @LogAnno(oprationtype = "/login",oprationname = "管理员登陆了")
    @RequestMapping("/login")
    public String login(String username, String password) {
        Subject subject = SecurityUtils.getSubject();//获取当前使用的用户
        if (!subject.isAuthenticated()) {//没有登录 执行登录
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
            try {
                subject.login(usernamePasswordToken);
                return "success";
            }catch (Exception e){
                e.printStackTrace();
                throw e;
            }
        }
        return "false";
    }

3、添加依赖jar包

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>

4、编写增强类

package com.qf.p2p.utils;

import com.qf.p2p.anno.LogAnno;
import com.qf.p2p.sysadmin.pojo.SysAdmin;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;


@Aspect//定义为增强类
@Component//在服务器启动的时候创建对象
public class AspectLogUtils {

//定义切入点
    @Pointcut("execution(* com..controller.*.*(..))")
    public void pointcut(){

    }

//前置增强
//    @Before("pointcut()")
//    public void before(JoinPoint joinPoint) throws ClassNotFoundException {
//
//        String name = joinPoint.getTarget().getClass().getName();//返回类的全限定名称
//        Signature name2 = joinPoint.getSignature();//返回方法的名称,把getname去掉就是获取全限定名称
//        Object[] args = joinPoint.getArgs();//获得传递过来的参数
//        System.out.println(name + "-------->" + name2 + "-------->" + Arrays.toString(args));
//         //获取类的对象
//        Class<?> aClass = Class.forName(name);
//
//        Method[] methods = aClass.getMethods();
//        for (Method method : methods) {
            System.out.println(method.getName());//返回方法的名称  如  login 等
//            if(method.getName().equalsIgnoreCase(name2.getName())){
					//获取方法上面的注解
//                LogAnno annotation = method.getAnnotation(LogAnno.class);
//                if(annotation!=null){
//                    String oprationtype = annotation.oprationtype();
//                    String oprationname = annotation.oprationname();
//                    SysAdmin user = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
//                    System.out.println(oprationname + "=========>" + oprationtype+"参数是:========>"+Arrays.toString(args)+"    用户是;======>"+(user==null?null:user.getName()));
//                }
//            }
//
//        }
//    }

    /**
     * 后置通知
     * @param joinPoint
     * @param o
     */
//    @AfterReturning(value = "pointcut()",returning = "o")
//    public void after(JoinPoint joinPoint,Object o) throws Exception {
//
//        String s = o.toString();//获取方法的返回值
//        System.out.println(s);
//        System.out.println("-----------------华丽的分割线-------------------");
//
//        String name = joinPoint.getTarget().getClass().getName();//返回类的全限定名称
//        Signature name2 = joinPoint.getSignature();//返回方法的名称,把getname去掉就是获取全限定名称
//        Object[] args = joinPoint.getArgs();//获得传递过来的参数
//        System.out.println(name + "-------->" + name2 + "-------->" + Arrays.toString(args));
//
//        Class<?> aClass = Class.forName(name);
//
//        Method[] methods = aClass.getMethods();
//        for (Method method : methods) {
            System.out.println(method.getName());//返回方法的名称  如  login 等
//            if(method.getName().equalsIgnoreCase(name2.getName())){
//                LogAnno annotation = method.getAnnotation(LogAnno.class);
//                if(annotation!=null){
//                    String oprationtype = annotation.oprationtype();
//                    String oprationname = annotation.oprationname();
//                    SysAdmin user = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
//                    System.out.println(oprationname + "=========>" + oprationtype+"参数是:========>"+Arrays.toString(args)+"    用户是;======>"+(user==null?null:user.getName()));
//                }
//            }
//
//        }
//    }

    /**
     * 异常的捕捉
     * @param joinPoint
     * @param e
     * @throws ClassNotFoundException
     */

    @AfterThrowing(value = "pointcut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Throwable e) throws ClassNotFoundException {


        String name = joinPoint.getTarget().getClass().getName();//返回类的全限定名称
        Signature name2 = joinPoint.getSignature();//返回方法的名称,把getname去掉就是获取全限定名称
        Object[] args = joinPoint.getArgs();//获得传递过来的参数
        System.out.println(name + "-------->" + name2 + "-------->" + Arrays.toString(args));

        Class<?> aClass = Class.forName(name);

        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
//            System.out.println(method.getName());//返回方法的名称  如  login 等
            if(method.getName().equalsIgnoreCase(name2.getName())){
                LogAnno annotation = method.getAnnotation(LogAnno.class);
                if(annotation!=null){
                    String oprationtype = annotation.oprationtype();
                    String oprationname = annotation.oprationname();
                    SysAdmin user = (SysAdmin) SecurityUtils.getSubject().getSession().getAttribute("user");
                    System.out.println(oprationname + "=========>" + oprationtype+"参数是:========>"+Arrays.toString(args)+"    用户:"+(user==null?null:user.getName())+"失败了......");
                }
            }

        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值