一、应用场景
项目中用户使用相关功能时需要判断是否登陆
二、自定义注解
约定切入点是标记了@LoginJudge的方法
@Retention(RetentionPolicy
.RUNTIME
)
@Target(ElementType
.METHOD
)
public @
interface LoginJudge {
}
标注注解在相关业务方法上
@PostMapping("/save")
@LoginJudge
public R
save(@RequestHeader("Authorization") String Authorization
,@RequestBody SkuInfoEntity skuInfo
){
if(skuInfoService
.insertOneSkuInfo(skuInfo
))
return R
.ok();
else
return R
.error();
}
三、编写切面
@Aspect
@Component
public class LoginJudge {
@Pointcut("@annotation(com.zhangxueliang.njithome.product.annotation.LoginJudge)")
public void pointcut()
{
}
@Around("pointcut()")
public R
isLogin(ProceedingJoinPoint joinPoint
) throws Throwable
{
Object
[] args
= joinPoint
.getArgs();
String Authorization
= args
[1].toString();
if (!Authorization
.equals("123"))
{
return R
.error("未登录");
}
else {
Object object
= joinPoint
.proceed();
return (R
)object
;
}
}
}
四、测试
1.设置请求头的token(图片中是Authorization)是1 未通过验证
转载请注明原文地址:https://blackberry.8miu.com/read-33614.html