Преглед изворни кода

增加内部接口校验注解和切面

dengsixing пре 2 година
родитељ
комит
bac006e80b

+ 14 - 0
4dkankan-common-web/src/main/java/com/fdkankan/web/annotation/CheckInnerApiPermit.java

@@ -0,0 +1,14 @@
+package com.fdkankan.web.annotation;
+
+import java.lang.annotation.Documented;
+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)
+@Documented
+public @interface CheckInnerApiPermit {
+    String description() default "";
+}

+ 48 - 0
4dkankan-common-web/src/main/java/com/fdkankan/web/interceptor/CheckInnerApiPermitAspect.java

@@ -0,0 +1,48 @@
+package com.fdkankan.web.interceptor;
+
+import cn.hutool.core.util.StrUtil;
+import com.fdkankan.common.constant.ErrorCode;
+import com.fdkankan.common.exception.BusinessException;
+import java.io.IOException;
+import javax.servlet.http.HttpServletRequest;
+import lombok.extern.log4j.Log4j2;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+@Log4j2
+@Aspect
+@Component
+@Order(101)
+public class CheckInnerApiPermitAspect {
+
+	@Value("${inner.customToken}")
+	private String customToken;
+
+	@Pointcut("@annotation(com.fdkankan.ucenter.annotation.CheckInnerApiPermit)")
+	public void checkCooperationPermit() {
+	}
+
+	/**
+	 * 前置通知 用于判断用户协作场景是否有协作权限
+	 *
+	 * @param joinPoint
+	 *            切点
+	 * @throws IOException
+	 */
+	@Before("checkCooperationPermit()")
+	public void doBefore(JoinPoint joinPoint) throws Exception {
+		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
+		String customToken = request.getHeader("custom-token");
+		if(StrUtil.isEmpty(customToken) || !customToken.equals(this.customToken)){
+			throw new BusinessException(ErrorCode.HAVE_NO_RIGHT);
+		}
+	}
+
+}