ssm开发切面类

package com.itheima.ssm.controller;

import com.itheima.ssm.domain.SysLog;
import com.itheima.ssm.service.SysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect  //表示当前类是一个切面类
public class SysLogAop {

    //注入request
    @Autowired
    private HttpServletRequest request;

    //注入service
    @Autowired
    private SysLogService sysLogService;

    private Date visitTime;  //访问时间
    private Class clazz;  //访问的类
    private Method method; //访问的方法


    //配置切入点
    @Pointcut("execution(* com.itheima.ssm.controller.*.*(..))&&!execution(* com.itheima.ssm.controller.SysLogController.*(..))&&!execution(* com.itheima.ssm.controller.UserController.getVCode(..))&&!execution(* com.itheima.ssm.controller.UserController.register(..))")
    public void pt() {

    }

    //前置通知
    @Before("pt()")
    public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException {
        visitTime = new Date ();  //获取访问时间
        clazz = joinPoint.getTarget ().getClass ();  //获取访问的类
        String methodName = joinPoint.getSignature ().getName ();  //获取访问的方法的名称
        //获取方法参数
        Object[] args = joinPoint.getArgs ();
        //通过反射获取Method
        if (args == null || args.length == 0) {
            method = clazz.getMethod (methodName); //只能获取无参的方法
        } else {
            Class[] classes = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                //Model 类型的参数注入的是BindingAwareModelMap类,这个类是Model接口实现类的ExtendedModelMap类的子类
                if(args[i] instanceof BindingAwareModelMap){
                    //将子类BindingAwareModelMap强转为父类ExtendedModelMap,这个类是Model接口的实现类  clazz.getMethod (methodName, classes); 就不会出现类型转换异常
                    classes[i] = Model.class;
                    continue;
                }
                classes[i] = args[i].getClass ();
            }
            method = clazz.getMethod (methodName, classes);
        }
    }

    //后置通知   主要获取日志中其它信息,时长、ip、url...
    @AfterReturning("pt()")
    public void doAfter() {
        //获取url -->  /orders/findAll.do
        String url = "";
        if (clazz != null && method != null && clazz != SysLogAop.class) {
            //1.获取类上 @RequestMapping("/product") 注解中的value值
            RequestMapping clazzAnnotation = (RequestMapping) clazz.getAnnotation (RequestMapping.class);
            if (clazzAnnotation != null) {
                String[] classValue = clazzAnnotation.value ();
                //2.获取方法上 @RequestMapping("/findAll.do") 注解中的value值
                RequestMapping methodAnnotation = method.getAnnotation (RequestMapping.class);
                if (methodAnnotation != null) {
                    String[] methodValue = methodAnnotation.value ();
                    //3.获取URL
                    if (classValue[0] != null && methodValue[0] != null) {
                        url = classValue[0] + methodValue[0];
                    }

//                    String uri = request.getRequestURI ();

                    //获取执行时间
                    Long executionTime = new Date ().getTime () - visitTime.getTime ();

                    //获取ip  需要request获取 --》 获取request需要在web.xml中配置Listener  RequestContextListener类 在注入HttpServletRequest
                    String ip = request.getRemoteAddr ();

                    //获取操作者用户名(两种方式)  1.从上下文中获取登录的对象   2.也可以从request.getSession中获取
                    SecurityContext context = SecurityContextHolder.getContext ();
//                    SecurityContext context = (SecurityContext) request.getSession ().getAttribute ("SPRING_SECURITY_CONTEXT");
                    Object principal = context.getAuthentication ().getPrincipal ();
                    String username = "";
                    if(principal instanceof User){
                        User user = (User) context.getAuthentication ().getPrincipal ();
                        username = user.getUsername ();
                    }else {
                        username = "还没登录";
                    }


                    //封装日志类 SysLog
                    SysLog sysLog = new SysLog ();
                    sysLog.setVisitTime (visitTime);
                    sysLog.setExecutionTime (executionTime);
                    sysLog.setIp (ip);
                    sysLog.setUrl (url);
                    sysLog.setUsername (username);
                    sysLog.setMethod ("[类名]" + clazz.getName() + "[方法名]" + method.getName());

                    //调用service
                    sysLogService.save(sysLog);
                }

            }

        }
    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值