java使用注解 添加权限

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RuleArea {

}
@Aspect
@Component
public class RuleAreaAspect {

    @Autowired
    private PlatFeignClient platFeignClient;

    // 配置织入点
    @Pointcut("@annotation(com.util.ruleAreaAspect.RuleArea)")
    public void PoinCut() {

    }

    // 环绕通知
    @Around(value = "PoinCut()")
    public Object sayAround(ProceedingJoinPoint joinPoint) throws Throwable {

        // 下面两个数组中,参数值和参数名的个数和位置是一一对应的。
        Object[] objs = joinPoint.getArgs();
        String[] argNames = ((MethodSignature) joinPoint.getSignature()).getParameterNames(); // 参数名
        Map<String, Object> paramMap = new HashMap<String, Object>();
        for (int i = 0; i < objs.length; i++) {
            if (!(objs[i] instanceof ExtendedServletRequestDataBinder) && !(objs[i] instanceof HttpServletResponseWrapper)) {
                paramMap.put(argNames[i], objs[i]);
            }
        }

        //此处的request必须和下面调用时的request完全一致
        HttpServletRequest request = (HttpServletRequest) paramMap.get("request");
        //从plat中获取到省和id保存进去
        Integer userId = Utilties.getUserId(request);
        List<SocAreaEntity> list = platFeignClient.queryProvince(userId);
        List<String> prov_id_list = list.stream().map(e -> String.valueOf(e.getA_id())).collect(Collectors.toList());

        //此处的alarm必须和下面调用时的alarm完全一致
        Object alarmRule = paramMap.get("alarm");

        Class<?> clazz = alarmRule.getClass();
        Method setUser_id = clazz.getDeclaredMethod("setUser_id",Integer.class);
        Method setProv_id = clazz.getDeclaredMethod("setProv_id",String.class);
        setUser_id.invoke(alarmRule,userId);
        setProv_id.invoke(alarmRule,String.join(",",prov_id_list));

        Object result = joinPoint.proceed();// 执行方法

        return result;
    }

}

调用时:

	@RuleArea
	@RequestMapping(value = "/add1", method = {RequestMethod.GET, RequestMethod.POST})
	//此处的alarm必须和上面注解中的alarm完全一致
	public String add1(Alarm1 alarm, HttpServletRequest request) {
		return alarmFeignClient.addAlarmRule(alarm);
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值