|
|
@@ -1,14 +1,48 @@
|
|
|
package cn.iaun.it.interceptor;
|
|
|
|
|
|
+import cn.iaun.it.util.RedisUtil;
|
|
|
+import cn.iaun.it.util.ResultCode;
|
|
|
+import cn.iaun.it.util.WebException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.method.HandlerMethod;
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.*;
|
|
|
|
|
|
public class PermissionInterceptor implements HandlerInterceptor {
|
|
|
+ private Map<String, Long> getSys() {
|
|
|
+ Map<String, Long> map = new HashMap<>();
|
|
|
+ map.put("resource", 3L);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
- System.out.println("hello world");
|
|
|
- return HandlerInterceptor.super.preHandle(request, response, handler);
|
|
|
+ if (!(handler instanceof HandlerMethod)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ String[] urls = request.getRequestURI().split("/");
|
|
|
+ if (urls.length < 2) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Map<String, Long> sys = getSys();
|
|
|
+ if (!sys.containsKey(urls[1])) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ Long sysId = sys.get(urls[1]);
|
|
|
+
|
|
|
+ Object userPermissionObj = request.getAttribute("userPermission");
|
|
|
+ List<Long> userPermission = new ArrayList<>();
|
|
|
+ if (userPermissionObj instanceof ArrayList<?>) {
|
|
|
+ for (Object o : (List<?>) userPermissionObj) {
|
|
|
+ userPermission.add((Long) o);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!userPermission.contains(sysId)) {
|
|
|
+ throw new WebException(ResultCode.PERM_NO_PERMISSION);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
}
|
|
|
}
|