当前位置: 代码网 > it编程>编程语言>Java > springboot使用注解实现鉴权功能

springboot使用注解实现鉴权功能

2024年12月24日 Java 我要评论
spring boot 使用注解和aop实现鉴权功能一、自定义注解自定义一个注解,实现以下几个要求:1、注解使用使用在方法上;2、注解保留到运行时;3、注解可以传入单个参数、多个参数或者不传参数@do

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注解实现鉴权内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com