spring boot 使用注解和aop实现鉴权功能
一、自定义注解
自定义一个注解,实现以下几个要求:
1、注解使用使用在方法上;
2、注解保留到运行时;
3、注解可以传入单个参数、多个参数或者不传参数
@documented
@target({elementtype.method}) // 用在方法上
@retention(retentionpolicy.runtime) //注解保留到运行时
public @interface roletype {
string[] value() default {};
}二、用户信息上下文
定义了一个名为 usercontext 的类,用于管理用户上下文信息。它使用 threadlocal 变量来存储每个线程独立的用户数据,包括用户名、角色列表和登录状态。
public class usercontext {
private static final threadlocal<list<string>> role = new threadlocal<>();
private static final threadlocal<string> username = new threadlocal<>();
private static final threadlocal<boolean> loginstatus = new threadlocal<>();
public static boolean loginstatus() {
return loginstatus.get();
}
public static void login() {
usercontext.loginstatus.set(true);
}
public static void logout() {
usercontext.loginstatus.set(false);
}
public static string getusername() {
return username.get();
}
public static void setusername(string username) {
usercontext.username.set(username);
}
public static void clearusername() {
username.remove();
}
public static list<string> getrole() {
return role.get();
}
public static void setrole(list<string> role) {
usercontext.role.set(role);
}
public static void setrole(string role) {
usercontext.role.set(list.of(role));
}
public static void clearrole() {
role.remove();
}
}三、设置用于拦截识别用户信息的过滤器
@component
public class authfilter extends onceperrequestfilter {
@override
protected void dofilterinternal(httpservletrequest request,
httpservletresponse response,
filterchain filterchain ) throws servletexception, ioexception {
//将用户信息写入上下文,可以从session或redis或者从关系型数据库获取用户信息及角色信息
usercontext.setusername(username);
usercontext.setrole(roles);
usercontext.login();
filterchain.dofilter(request,response);
}
}四、使用aop实现权限校验
使用spring aop(面向切面编程)实现的权限控制切面。使用注解的方法检查用户是否具有访问该方法所需的权限。
@aspect
@component
public class authaspect extends httpservlet {
@pointcut("@annotation(roletype)")
public void annotatedmethod() {
}
//注解存在时,需要在登陆情况下v爱可以访问接口
@around("annotatedmethod()")
public object aroundannotatedmethod(proceedingjoinpoint joinpoint) throws throwable {
//访问接口时需要的权限标识集合
list<string> apirole = arrays.aslist(annotationutils.findannotation(((methodsignature) joinpoint.getsignature()).getmethod(), roletype.class).value());
//用户拥有的权限标识集合
list<string> userrole = usercontext.getrole();
//注解存在,并且登陆情况下可以访问接口
if (usercontext.loginstatus()){
//如果任意接口标识中元素在用户权限标识中存在,则有权访问该接口
if (apirole.isempty() || apirole.stream().anymatch(userrole::contains)) {
return joinpoint.proceed();
} else {
throw new mallexception(403, "无权限访问!");
}
}else {
throw new mallexception(500,"请先登陆再访问!");
}
}
//程序运行结束后清楚上下文
@after("annotatedmethod()")
public void afterannotatedmethod(joinpoint joinpoint) {
usercontext.clearrole();
usercontext.clearusername();
usercontext.clearrole();
}
}六、注解的使用
1、无参数
使用注解情况下,必须登录情况下才可以访问
@roletype
public string test(){
return usercontext.getrole();
}2、一个参数
用户有role权限标识才可以访问该方法
@roletype("role")
public string test(){
return usercontext.getrole();
}3、多个参数
用户有role或test等任意一个权限标识才可以访问该方法
@roletype({"role","test",...})
public string test(){
return usercontext.getrole();
}到此这篇关于springboot使用注解实现鉴权功能的文章就介绍到这了,更多相关springbbot注解实现鉴权内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论