|
@@ -0,0 +1,108 @@
|
|
|
+package com.xy.satoken;
|
|
|
+
|
|
|
+import cn.dev33.satoken.exception.NotLoginException;
|
|
|
+import cn.dev33.satoken.exception.NotPermissionException;
|
|
|
+import cn.dev33.satoken.exception.NotRoleException;
|
|
|
+import cn.dev33.satoken.stp.StpInterface;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import com.xy.utils.R;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 权限校验
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@RestControllerAdvice
|
|
|
+public class SaTokenAuthorize implements StpInterface {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验权限
|
|
|
+ *
|
|
|
+ * @param satoken token值
|
|
|
+ * @param permission 菜单权限标识
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean check(String satoken, String permission) {
|
|
|
+ //校验登录
|
|
|
+ if (StringUtils.isEmpty(satoken)) {
|
|
|
+ throw new NotLoginException(null, null, null);
|
|
|
+ }
|
|
|
+ Object byId = StpUtil.getLoginIdByToken(satoken);
|
|
|
+ if (byId == null) {
|
|
|
+ throw new NotLoginException(null, null, null);
|
|
|
+ }
|
|
|
+ //菜单权限
|
|
|
+ List<String> permissionList = StpUtil.getPermissionList(byId);
|
|
|
+ if (!permissionList.contains(permission.substring(1).replaceAll("/", "."))) {
|
|
|
+ throw new NotPermissionException(null);
|
|
|
+ }
|
|
|
+ //角色权限
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给予用户菜单权限
|
|
|
+ *
|
|
|
+ * @param loginId
|
|
|
+ * @param loginType
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<String> getPermissionList(Object loginId, String loginType) {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 给予用户角色权限
|
|
|
+ *
|
|
|
+ * @param loginId
|
|
|
+ * @param loginType
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<String> getRoleList(Object loginId, String loginType) {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无token异常
|
|
|
+ *
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ExceptionHandler(NotLoginException.class)
|
|
|
+ public R notLoginException(NotLoginException e) {
|
|
|
+ return R.fail(501, "未登录");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无菜单权限异常
|
|
|
+ *
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ExceptionHandler(NotPermissionException.class)
|
|
|
+ public R notPermissionException(NotPermissionException e) {
|
|
|
+ return R.fail(502, "无权限");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无角色权限异常
|
|
|
+ *
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ExceptionHandler(NotRoleException.class)
|
|
|
+ public R notRoleException(NotRoleException e) {
|
|
|
+ return R.fail(502, "无权限");
|
|
|
+ }
|
|
|
+}
|