欢迎使用CSDN-markdown编辑器

来源:互联网 发布:金山软件管家下载 编辑:程序博客网 时间:2024/05/29 17:47

java 自定义注解的定义与使用

注解类

package com.yesmywine.jwt.customPerm;import java.lang.annotation.*;/** * Created by by on 2017/8/3. */@Target({ElementType.PARAMETER, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedpublic @interface SecurestValid {    public String value();}

使用切面切入controller

package com.yesmywine.jwt.customPerm;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.yesmywine.db.base.biz.RedisCache;import com.yesmywine.jwt.thread.LogThread;import com.yesmywine.util.basic.Statement;import com.yesmywine.util.basic.ValueUtil;import com.yesmywine.util.error.YesmywineException;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import java.lang.reflect.Method;/** * Created by by on 2017/8/3. */@Aspect@Componentpublic class SecurestAspect {    private  static  final Logger logger = LoggerFactory.getLogger(SecurestAspect. class);    //Controller层切点 包路径 可为多个    @Pointcut("execution (* com.yesmywine.*.controller..*(..))")    public  void controllerAspect() {    }    private String getOperation(String requestPerm,String userInfo) {        String userPerms = ValueUtil.getFromJson(userInfo,"allPerms");        JSONObject jsonObject = JSON.parseObject(userPerms);        return jsonObject.getString(requestPerm);    }    /**     * 前置通知 用于拦截Controller层记录用户的操作     *     * @param joinPoint 切点     */    @Before("controllerAspect()")    public void doBefore(JoinPoint joinPoint) throws ClassNotFoundException, YesmywineException {        System.out.println("==========执行controller前置通知===============");        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();        String perm = request.getHeader("RequestPerm");        String targetName = joinPoint.getTarget().getClass().getName();        String methodName = joinPoint.getSignature().getName();        Object[] arguments = joinPoint.getArgs();        Class targetClass = Class.forName(targetName);        Method[] methods = targetClass.getMethods();        String value = "";        String operationName = "";        for (Method method : methods) {            if (method.getName().equals(methodName)) {                Class[] clazzs = method.getParameterTypes();                if (clazzs.length == arguments.length) {                    if(method.getAnnotation(SecurestValid.class)!=null){                        value = method.getAnnotation(SecurestValid.class).value();                        if(!perm.equals(value)){                            ValueUtil.isError("您无此权限");                        }                    }        //            String userInfo = RedisCache.get(Statement.USER_INFO+username);        //            String operation = getOperation(perm,userInfo);        //        //            if(operation==null){        //                ValueUtil.isError("请求权限 RequestPerm 参数,无效的操作");        //            }        //            LogThread logThread = new LogThread(username,operation);        //            Thread thread = new Thread(logThread);        //            thread.start();                    break;                }            }        }        if(logger.isInfoEnabled()){            logger.info("before " + joinPoint);        }    }    //配置controller环绕通知,使用在方法aspect()上注册的切入点    @Around("controllerAspect()")    public Object around(JoinPoint joinPoint) throws ClassNotFoundException{        System.out.println("==========开始执行controller环绕通知===============");        long start = System.currentTimeMillis();        try {            //obj 为controller的返回值            Object obj = ((ProceedingJoinPoint) joinPoint).proceed();            long end = System.currentTimeMillis();            if(logger.isInfoEnabled()){                logger.info("around " + joinPoint + "\tUse time : " + (end - start) + " ms!");            }            System.out.println("==========结束执行controller环绕通知===============");            return obj;        } catch (YesmywineException e) {            return ValueUtil.toError(e.getCode(),e.getMessage());        } catch (Throwable throwable) {            throwable.printStackTrace();        }        return null;    }    /**     * 后置通知 用于拦截Controller层记录用户的操作     *     * @param joinPoint 切点     */    @After("controllerAspect()")    public  void after(JoinPoint joinPoint) throws ClassNotFoundException, YesmywineException {        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();        HttpSession session = request.getSession();        String perm = request.getHeader("RequestPerm");        //读取session中的用户        // User user = (User) session.getAttribute("user");        //请求的IP        String ip = request.getRemoteAddr();        String targetName = joinPoint.getTarget().getClass().getName();        String methodName = joinPoint.getSignature().getName();        Object[] arguments = joinPoint.getArgs();        Class targetClass = Class.forName(targetName);        Method[] methods = targetClass.getMethods();        String value = "";        String operationName = "";        for (Method method : methods) {            if (method.getName().equals(methodName)) {                Class[] clazzs = method.getParameterTypes();                if (clazzs.length == arguments.length) {//                    System.out.println(method.getAnnotation(SecurestValid.class));//                    value = method.getAnnotation(SecurestValid.class).value();//                        operationName = method.getAnnotation(SecurestAspect.class).operationName();                    break;                }            }        }        //*========控制台输出=========*//        System.out.println("=====controller后置通知开始=====");        System.out.println("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+value);        System.out.println("方法描述:" + operationName);//            System.out.println("请求人:" + user.getName());        System.out.println("请求IP:" + ip);        System.out.println("=====controller后置通知结束=====");    }    //配置后置返回通知,使用在方法aspect()上注册的切入点    @AfterReturning("controllerAspect()")    public void afterReturn(JoinPoint joinPoint) throws YesmywineException {        System.out.println("=====执行controller后置返回通知=====");//        ValueUtil.isError("888888888");        if(logger.isInfoEnabled()){            logger.info("afterReturn " + joinPoint);        }    }    /**     * 异常通知 用于拦截记录异常日志     *     * @param joinPoint     * @param e     *///    @AfterThrowing(pointcut = "controllerAspect()", throwing="e")//    public  String  doAfterThrowing(JoinPoint joinPoint, Throwable e) throws YesmywineException{//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();//        HttpSession session = request.getSession();//        //读取session中的用户//        //获取请求ip//        String ip = request.getRemoteAddr();//        //获取用户请求方法的参数并序列化为JSON格式字符串////        String params = "";//        if (joinPoint.getArgs() !=  null && joinPoint.getArgs().length > 0) {//            for ( int i = 0; i < joinPoint.getArgs().length; i++) {////                params += JsonUtil.getJsonStr(joinPoint.getArgs()[i]) + ";";//                params += joinPoint.getArgs()[i] ;//            }//        }//        try {////            String targetName = joinPoint.getTarget().getClass().getName();//            String methodName = joinPoint.getSignature().getName();//            Object[] arguments = joinPoint.getArgs();//            Class targetClass = Class.forName(targetName);//            Method[] methods = targetClass.getMethods();//            String operationType = "";//            String operationName = "";//            for (Method method : methods) {//                if (method.getName().equals(methodName)) {//                    Class[] clazzs = method.getParameterTypes();//                    if (clazzs.length == arguments.length) {//                        operationType = method.getAnnotation(SecurestValid.class).value();////                        operationName = method.getAnnotation(Log.class).operationName();//                        break;//                    }//                }//            }//             /*========控制台输出=========*///            System.out.println("=====异常通知开始=====");//            System.out.println("异常代码:" + e.getClass().getName());//            System.out.println("异常信息:" + e.getMessage());//            System.out.println("异常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType);//            System.out.println("方法描述:" + operationName);////            System.out.println("请求人:" + user.getName());////            System.out.println("请求IP:" + ip);//            System.out.println("请求参数:" + params);//               /*==========数据库日志=========*///            System.out.println("=====异常通知结束=====");//        }  catch (Exception ex) {//            //记录本地异常日志//            logger.error("==异常通知异常==");//            logger.error("异常信息:{}", ex.getMessage());//        }//         /*==========记录本地异常日志==========*///        logger.error("异常方法:{}异常代码:{}异常信息:{}参数:{}", joinPoint.getTarget().getClass().getName() + joinPoint.getSignature().getName(), e.getClass().getName(), e.getMessage(), params);//        return null;//    }}

controller中的使用

package com.yesmywine.activity.controller;import com.yesmywine.activity.ifttt.entity.Activity;import com.yesmywine.activity.ifttt.service.ActivityService;import com.yesmywine.activity.ifttt.service.IftttService;import com.yesmywine.base.record.bean.PageModel;import com.yesmywine.util.basic.MapUtil;import com.yesmywine.util.basic.ValueUtil;import com.yesmywine.util.error.YesmywineException;import org.apache.http.HttpStatus;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import java.util.Map;/** * Created by wangdiandian on 2017/1/10. */@RestController@RequestMapping("/activity")public class ActivityController {    @Autowired    private ActivityService activityService;    @Autowired    private IftttService iftttService;    @SecurestValid("/test")    @RequestMapping(method = RequestMethod.POST)    public String createActivity(@RequestParam Map<String, String> param, HttpServletRequest request) {//创建活动        try {            return ValueUtil.toJson(HttpStatus.SC_CREATED, activityService.createActivity(param,request));        } catch (YesmywineException e) {            return ValueUtil.toError(e.getCode(), e.getMessage());        }    }}
原创粉丝点击