|
@@ -0,0 +1,81 @@
|
|
|
|
|
+package org.zhongzheng.common.secure.annotation;
|
|
|
|
|
+
|
|
|
|
|
+//
|
|
|
|
|
+// Source code recreated from a .class file by IntelliJ IDEA
|
|
|
|
|
+// (powered by FernFlower decompiler)
|
|
|
|
|
+//
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+import org.zhongzheng.common.secure.annotation.PreAuth;
|
|
|
|
|
+import org.zhongzheng.common.secure.annotation.AuthFun;
|
|
|
|
|
+import org.zhongzheng.common.secure.SecureException;
|
|
|
|
|
+import org.zhongzheng.common.utils.impl.ResultCode;
|
|
|
|
|
+import org.zhongzheng.common.utils.ClassUtil;
|
|
|
|
|
+import org.zhongzheng.common.utils.StringUtil;
|
|
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
|
|
+import org.springframework.context.expression.BeanFactoryResolver;
|
|
|
|
|
+import org.springframework.core.MethodParameter;
|
|
|
|
|
+import org.springframework.expression.Expression;
|
|
|
|
|
+import org.springframework.expression.ExpressionParser;
|
|
|
|
|
+import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|
|
|
|
+import org.springframework.expression.spel.support.StandardEvaluationContext;
|
|
|
|
|
+
|
|
|
|
|
+@Aspect
|
|
|
|
|
+@Component
|
|
|
|
|
+public class AuthAspect implements ApplicationContextAware {
|
|
|
|
|
+ private static final ExpressionParser SPEL_PARSER = new SpelExpressionParser();
|
|
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
|
|
+
|
|
|
|
|
+ public AuthAspect() {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Around("@annotation(org.zhongzheng.common.secure.annotation.PreAuth) || @within(org.zhongzheng.common.secure.annotation.PreAuth)")
|
|
|
|
|
+ public Object preAuth(ProceedingJoinPoint point) throws Throwable {
|
|
|
|
|
+ System.out.println(" ---------- preAuth around Aspect");
|
|
|
|
|
+ if (this.handleAuth(point)) {
|
|
|
|
|
+ return point.proceed();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new SecureException(ResultCode.UN_AUTHORIZED);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean handleAuth(ProceedingJoinPoint point) {
|
|
|
|
|
+ MethodSignature ms = (MethodSignature)point.getSignature();
|
|
|
|
|
+ Method method = ms.getMethod();
|
|
|
|
|
+ PreAuth preAuth = (PreAuth)ClassUtil.getAnnotation(method, PreAuth.class);
|
|
|
|
|
+ String condition = preAuth.value();
|
|
|
|
|
+ if (StringUtil.isNotBlank(condition)) {
|
|
|
|
|
+ Expression expression = SPEL_PARSER.parseExpression(condition);
|
|
|
|
|
+ Object[] args = point.getArgs();
|
|
|
|
|
+ StandardEvaluationContext context = this.getEvaluationContext(method, args);
|
|
|
|
|
+ return (Boolean)expression.getValue(context, Boolean.class);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private StandardEvaluationContext getEvaluationContext(Method method, Object[] args) {
|
|
|
|
|
+ StandardEvaluationContext context = new StandardEvaluationContext(new AuthFun());
|
|
|
|
|
+ context.setBeanResolver(new BeanFactoryResolver(this.applicationContext));
|
|
|
|
|
+
|
|
|
|
|
+ for(int i = 0; i < args.length; ++i) {
|
|
|
|
|
+ MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
|
|
|
|
|
+ context.setVariable(methodParam.getParameterName(), args[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return context;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
|
|
|
+ this.applicationContext = applicationContext;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|