操作权限控制
1. 实现示例
Leap 本身没有提供内置的操作权限控制,我们可以通过路由拦截器来拦截所有请求,并在拦截器中实现您的权限控制规则。
下面是路由拦截器的示例代码:
package hello.interceptors;
import leap.core.annotation.Inject;
import leap.core.validation.Validation;
import leap.lang.Strings;
import leap.lang.intercepting.State;
import leap.web.action.ActionContext;
import leap.web.action.ActionInterceptor;
import leap.web.api.mvc.ApiErrorHandler;
public class PermissionInterceptor implements ActionInterceptor {
protected @Inject ApiErrorHandler errorHandler;
@Override
public State preExecuteAction(ActionContext context, Validation validation) throws Throwable {
String path = context.getPath();
if(path.equals("/greeting/perm")) {
if(Strings.isEmpty(context.getRequest().getParameter("secret"))) {
errorHandler.forbidden(context.getResponse(), "No permission");
return State.INTERCEPTED;
}
}
return State.CONTINUE;
}
}
配置 src/main/resources/beans.xml
让该拦截器生效:
<beans xmlns="http://www.leapframework.org/schema/beans">
<bean type="leap.web.action.ActionInterceptor" class="hello.interceptors.PermissionInterceptor"/>
</beans>
2. 验证测试
发起不带 secret
参数的请求,将会访问失败:
**[terminal]
curl http://localhost:8080/greeting/perm
返回失败信息:
{"code":"FORBIDDEN","message":"No permission"}
再次发起带上 secret
参数的请求,将会访问成功:
**[terminal]
curl http://localhost:8080/greeting/perm?secret=1
返回成功信息:
"OK"