欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot使用注解實現(xiàn)鑒權(quán)功能

 更新時間:2024年12月23日 11:01:57   作者:Gms89  
這篇文章主要介紹了springboot使用注解實現(xiàn)鑒權(quán)功能,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧

Spring Boot 使用注解和AOP實現(xiàn)鑒權(quán)功能

一、自定義注解

自定義一個注解,實現(xiàn)以下幾個要求:

1、注解使用使用在方法上;

2、注解保留到運行時;

3、注解可以傳入單個參數(shù)、多個參數(shù)或者不傳參數(shù)

@Documented
@Target({ElementType.METHOD})  // 用在方法上
@Retention(RetentionPolicy.RUNTIME)  //注解保留到運行時
public @interface RoleType {
    String[] value() default {};
}

二、用戶信息上下文

定義了一個名為 UserContext 的類,用于管理用戶上下文信息。它使用 ThreadLocal 變量來存儲每個線程獨立的用戶數(shù)據(jù),包括用戶名、角色列表和登錄狀態(tài)。

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();
    }
}

三、設(shè)置用于攔截識別用戶信息的過濾器

@Component
public class AuthFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain ) throws ServletException, IOException {
        //將用戶信息寫入上下文,可以從session或redis或者從關(guān)系型數(shù)據(jù)庫獲取用戶信息及角色信息
        UserContext.setUsername(username);
        UserContext.setRole(roles);
        UserContext.login();
        filterChain.doFilter(request,response);
    }
}

四、使用AOP實現(xiàn)權(quán)限校驗

使用Spring AOP(面向切面編程)實現(xiàn)的權(quán)限控制切面。使用注解的方法檢查用戶是否具有訪問該方法所需的權(quán)限。

@Aspect
@Component
public class AuthAspect extends HttpServlet {
    @Pointcut("@annotation(RoleType)")
    public void annotatedMethod() {
    }
    //注解存在時,需要在登陸情況下v愛可以訪問接口
    @Around("annotatedMethod()")
    public Object aroundAnnotatedMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        //訪問接口時需要的權(quán)限標識集合
        List<String> apiRole = Arrays.asList(AnnotationUtils.findAnnotation(((MethodSignature) joinPoint.getSignature()).getMethod(), RoleType.class).value());
        //用戶擁有的權(quán)限標識集合
        List<String> userRole = UserContext.getRole();
        //注解存在,并且登陸情況下可以訪問接口
        if (UserContext.loginStatus()){
            //如果任意接口標識中元素在用戶權(quán)限標識中存在,則有權(quán)訪問該接口
            if (apiRole.isEmpty() || apiRole.stream().anyMatch(userRole::contains)) {
                return joinPoint.proceed();
            } else {
                throw new MallException(403, "無權(quán)限訪問!");
            }
        }else {
            throw new MallException(500,"請先登陸再訪問!");
        }
    }
    //程序運行結(jié)束后清楚上下文
    @After("annotatedMethod()")
    public void afterAnnotatedMethod(JoinPoint joinPoint) {
        UserContext.clearRole();
        UserContext.clearUsername();
        UserContext.clearRole();
    }
}

六、注解的使用

1、無參數(shù)

使用注解情況下,必須登錄情況下才可以訪問

    @RoleType
    public String test(){
        return UserContext.getRole();
    }

2、一個參數(shù)

用戶有role權(quán)限標識才可以訪問該方法

    @RoleType("role")
    public String test(){
        return UserContext.getRole();
    }

3、多個參數(shù)

用戶有role或test等任意一個權(quán)限標識才可以訪問該方法

    @RoleType({"role","test",...})
    public String test(){
        return UserContext.getRole();
    }

到此這篇關(guān)于springboot使用注解實現(xiàn)鑒權(quán)功能的文章就介紹到這了,更多相關(guān)springbbot注解實現(xiàn)鑒權(quán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java讀取文件方法匯總

    Java讀取文件方法匯總

    這篇文章主要為大家詳細介紹了Java讀取文件方法,按字節(jié)讀取文件內(nèi)容、按字符讀取文件內(nèi)容、隨機讀取文件內(nèi)容等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Java+MySQL實現(xiàn)學生信息管理系統(tǒng)源碼

    Java+MySQL實現(xiàn)學生信息管理系統(tǒng)源碼

    這篇文章主要為大家詳細介紹了Java+MySQL實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Spring Boot實戰(zhàn)之逐行釋義Hello World程序

    Spring Boot實戰(zhàn)之逐行釋義Hello World程序

    spring boot 是基于Spring的一個框架,Spring boot幫我們集成很多常用的功能,使得整個配置更加簡單。這篇文章主要介紹了Spring Boot實戰(zhàn)之逐行釋義Hello World,需要的朋友可以參考下
    2017-12-12
  • Java RabbitMQ的三種Exchange模式

    Java RabbitMQ的三種Exchange模式

    這篇文章主要介紹了Java RabbitMQ的三種Exchange模式,分別為Direct模式、Fanout模式、Topic模式,Rabbit的Direct Exchange模式是指消息發(fā)送導RouteKey中指定的Queue,Direct模式可以使用Rabbit自帶的Exchange
    2022-08-08
  • 關(guān)于Java中的 JSP 詳解

    關(guān)于Java中的 JSP 詳解

    JSP 代表 Java 服務(wù)器頁面。它是一種在應(yīng)用服務(wù)器端使用的編程工具。JSP 基本上用于支持平臺–獨立和動態(tài)的方法來構(gòu)建 Web 依賴的應(yīng)用程序。JSP 頁面類似于 ASP 頁面,因為它們是在服務(wù)器上編譯的,而不是在用戶的 Web 瀏覽器上進行編譯。下面來看看文章的詳細介紹內(nèi)容
    2021-11-11
  • springboot 通過代碼自動生成pid的方法

    springboot 通過代碼自動生成pid的方法

    這篇文章主要介紹了springboot 通過代碼自動生成pid的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • java定時任務(wù)cron表達式每周執(zhí)行一次的坑及解決

    java定時任務(wù)cron表達式每周執(zhí)行一次的坑及解決

    這篇文章主要介紹了java定時任務(wù)cron表達式每周執(zhí)行一次的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Maven3種打包方式中maven-assembly-plugin的使用詳解

    Maven3種打包方式中maven-assembly-plugin的使用詳解

    這篇文章主要介紹了Maven3種打包方式中maven-assembly-plugin的使用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • nacos配置中心持久化相關(guān)配置方式

    nacos配置中心持久化相關(guān)配置方式

    這篇文章主要介紹了nacos配置中心持久化相關(guān)配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • mybatis plus 關(guān)聯(lián)數(shù)據(jù)庫排除不必要字段方式

    mybatis plus 關(guān)聯(lián)數(shù)據(jù)庫排除不必要字段方式

    這篇文章主要介紹了mybatis plus 關(guān)聯(lián)數(shù)據(jù)庫排除不必要字段方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論