浏览代码

优化权限提示

zhou-hao 4 年之前
父节点
当前提交
d389d05f56

+ 11 - 0
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/define/AuthorizeDefinition.java

@@ -1,6 +1,8 @@
 package org.hswebframework.web.authorization.define;
 
 
+import java.util.StringJoiner;
+
 /**
  * 权限控制定义,定义权限控制的方式
  *
@@ -18,4 +20,13 @@ public interface AuthorizeDefinition {
     Phased getPhased();
 
     boolean isEmpty();
+
+    default String getDescription() {
+        ResourcesDefinition res = getResources();
+        StringJoiner joiner = new StringJoiner(";");
+        for (ResourceDefinition resource : res.getResources()) {
+            joiner.add(resource.getId() + ":" + String.join(",", resource.getActionIds()));
+        }
+        return joiner.toString();
+    }
 }

+ 4 - 1
hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/exception/AccessDenyException.java

@@ -26,8 +26,11 @@ public class AccessDenyException extends RuntimeException {
         super(message);
     }
 
+    public AccessDenyException(String message, String code) {
+        this(message, code, null);
+    }
     public AccessDenyException(String message, Throwable cause) {
-        this(message,"access_denied", cause);
+        this(message, "access_denied", cause);
     }
 
     public AccessDenyException(String message, String code, Throwable cause) {

+ 1 - 1
hsweb-authorization/hsweb-authorization-basic/src/main/java/org/hswebframework/web/authorization/basic/handler/DefaultAuthorizingHandler.java

@@ -106,7 +106,7 @@ public class DefaultAuthorizingHandler implements AuthorizingHandler {
         ResourcesDefinition resources = definition.getResources();
 
         if (!resources.hasPermission(authentication.getPermissions())) {
-            throw new AccessDenyException();
+            throw new AccessDenyException(definition.getMessage(),definition.getDescription());
         }
     }
 }