权限校验注解
在开发 Web 应用程序时,权限控制是必不可少的功能。本文将介绍如何在 TioBoot 框架中使用内置的注解 @GatewayCheck
、@RequiresAuthentication
和 @RequiresPermissions
实现权限校验,并演示如何自定义权限校验的切面逻辑。
前言
TioBoot 提供了便捷的权限校验注解,方便开发者在控制器中对请求进行权限控制。需要注意的是,这些注解 仅在使用 Controller 时有效。接下来,我们将通过示例代码,详细说明如何使用和配置这些注解。
控制器的实现
首先,我们创建一个名为 CheckController
的控制器,并在其中使用权限校验注解:
package com.litongjava.tio.web.hello.controller;
import com.litongjava.annotation.RequestPath;
import com.litongjava.annotation.GatewayCheck;
import com.litongjava.annotation.RequiresAuthentication;
import com.litongjava.annotation.RequiresPermissions;
import com.litongjava.model.body.RespBodyVo;
@RequestPath("/check")
public class CheckController {
@GatewayCheck
public RespBodyVo gatewary() {
return RespBodyVo.ok();
}
@RequiresAuthentication
public RespBodyVo requiresAuthentication() {
return RespBodyVo.ok();
}
@RequiresPermissions("test")
public RespBodyVo requiresPermissions() {
return RespBodyVo.ok();
}
}
在上述代码中:
@RequestPath("/check")
:定义了控制器的请求路径前缀。@GatewayCheck
:用于网关校验。@RequiresAuthentication
:表示该方法需要用户已认证。@RequiresPermissions("test")
:表示该方法需要用户具备"test"
权限。
配置自定义切面
为了自定义权限校验的逻辑,我们需要实现对应的切面接口,并在服务器配置中注册这些切面。
服务器配置
package com.litongjava.tio.web.hello.config;
import com.litongjava.annotation.AConfiguration;
import com.litongjava.annotation.Initialization;
import com.litongjava.tio.boot.server.TioBootServer;
import com.litongjava.tio.web.hello.aspect.MyGatewayCheckAspect;
import com.litongjava.tio.web.hello.aspect.MyRequiresAuthenticationAspect;
import com.litongjava.tio.web.hello.aspect.MyRequiresPermissionsAspect;
@AConfiguration
public class TioBootServerConfig {
@Initialization
public void config() {
TioBootServer server = TioBootServer.me();
server.setGateWayCheckAspect(new MyGatewayCheckAspect());
server.setRequiresAuthenticationAspect(new MyRequiresAuthenticationAspect());
server.setRequiresPermissionsAspect(new MyRequiresPermissionsAspect());
}
}
在上述配置中,我们通过 server.setXXXAspect()
方法注册了自定义的切面。
自定义 GatewayCheck
切面
package com.litongjava.tio.web.hello.aspect;
import java.lang.reflect.Method;
import com.litongjava.annotation.GatewayCheck;
import com.litongjava.tio.boot.aspect.IGateWayCheckAspect;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MyGatewayCheckAspect implements IGateWayCheckAspect {
@Override
public HttpResponse check(HttpRequest request, Object targetController, Method actionMethod, GatewayCheck annotation) {
log.info("GatewayCheck Annotation Details: {}", annotation);
// 在此处添加具体的网关校验逻辑
return null;
}
}
自定义 RequiresAuthentication
切面
package com.litongjava.tio.web.hello.aspect;
import java.lang.reflect.Method;
import com.litongjava.tio.boot.aspect.IRequiresAuthenticationAspect;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MyRequiresAuthenticationAspect implements IRequiresAuthenticationAspect {
@Override
public HttpResponse check(HttpRequest request, Object targetController, Method actionMethod) {
log.info("Method Requiring Authentication: {}", actionMethod);
// 在此处添加具体的认证校验逻辑
return null;
}
}
自定义 RequiresPermissions
切面
package com.litongjava.tio.web.hello.aspect;
import java.lang.reflect.Method;
import java.util.Arrays;
import com.litongjava.annotation.RequiresPermissions;
import com.litongjava.tio.boot.aspect.IRequiresPermissionsAspect;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.tio.http.common.HttpResponse;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class MyRequiresPermissionsAspect implements IRequiresPermissionsAspect {
@Override
public HttpResponse check(HttpRequest request, Object targetController, Method actionMethod, RequiresPermissions annotation) {
log.info("Permissions Required: {}", Arrays.toString(annotation.value()));
// 在此处添加具体的权限校验逻辑
return null;
}
}
测试与日志输出
测试接口
我们将通过以下 URL 测试控制器的方法:
- 网关校验测试:
http://localhost/check/gatewary
- 认证校验测试:
http://localhost/check/requiresAuthentication
- 权限校验测试:
http://localhost/check/requiresPermissions
日志输出
访问上述接口后,日志将输出以下信息:
[Thread-15] INFO c.l.t.w.h.a.MyGatewayCheckAspect.check:17 - GatewayCheck Annotation Details: @com.litongjava.aop.GatewayCheck(checkUserToken=false, checkPowerToken=false, checkAdminToken=false)
[Thread-15] INFO c.l.t.w.h.a.MyRequiresAuthenticationAspect.check:16 - Method Requiring Authentication: public com.litongjava.model.body.RespBodyVo com.litongjava.tio.web.hello.controller.CheckController.requiresAuthentication()
[Thread-15] INFO c.l.t.w.h.a.MyRequiresPermissionsAspect.check:18 - Permissions Required: [test]
从日志中可以看到:
MyGatewayCheckAspect
切面成功捕获了@GatewayCheck
注解的信息。MyRequiresAuthenticationAspect
切面成功获取了需要认证的方法信息。MyRequiresPermissionsAspect
切面成功获取了所需的权限信息。
总结
通过上述示例,我们了解了如何在 TioBoot 中使用内置的权限校验注解,并自定义对应的切面逻辑。实际应用中,开发者可以在切面中添加具体的权限验证逻辑,例如校验用户的身份、权限等,以满足业务需求。
参考
- TioBoot 官方文档
- Lombok 项目用于简化 Java 代码的注解工具
提示:上述代码仅为示例,实际项目中请根据具体需求完善权限校验逻辑。