最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Spring Boot 使用注解和AOP實現(xià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();
    }
}

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

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

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

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

@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、無參數(shù)

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

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

2、一個參數(shù)

用戶有role權限標識才可以訪問該方法

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

3、多個參數(shù)

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

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

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

相關文章

  • MyBatis不同Mapper文件引用resultMap實例代碼

    MyBatis不同Mapper文件引用resultMap實例代碼

    這篇文章主要介紹了mybatis 不同Mapper文件引用resultMap的實例代碼,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2017-07-07
  • SpringBoot實現(xiàn)緩存與數(shù)據(jù)庫雙寫策略的詳細代碼

    SpringBoot實現(xiàn)緩存與數(shù)據(jù)庫雙寫策略的詳細代碼

    在SpringBoot企業(yè)開發(fā)中,為了提升系統(tǒng)性能,我們都會給高頻查詢接口加上緩存,把熱點數(shù)據(jù)緩存起來,減少數(shù)據(jù)庫查詢壓力,因此本文給大家介紹了SpringBoot實現(xiàn)緩存與數(shù)據(jù)庫雙寫策略的詳細方法,需要的朋友可以參考下
    2026-04-04
  • DolphinScheduler容錯Master源碼分析

    DolphinScheduler容錯Master源碼分析

    這篇文章主要為大家介紹了DolphinScheduler容錯Master源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Spring Security OAuth2.0登出的實現(xiàn)

    Spring Security OAuth2.0登出的實現(xiàn)

    本文主要介紹了Spring Security OAuth2.0登出的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2026-03-03
  • Java基礎之命名規(guī)范的詳解

    Java基礎之命名規(guī)范的詳解

    這篇文章主要介紹了Java基礎之命名規(guī)范的詳解,文中有非常詳細的代碼示例,對正在學習Java基礎的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Java如何優(yōu)雅替換if-else語句

    Java如何優(yōu)雅替換if-else語句

    當邏輯分支非常多的時候,if-else套了一層又一層,那么如何干掉過多的if-else,本文就詳細的介紹一下,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java數(shù)據(jù)結構之二叉搜索樹詳解

    Java數(shù)據(jù)結構之二叉搜索樹詳解

    二叉搜索樹作為一個經(jīng)典的數(shù)據(jù)結構,具有鏈表的快速插入與刪除的特點,同時查詢效率也很優(yōu)秀,所以應用十分廣泛。本文將詳細講講二叉搜索樹的原理與實現(xiàn),需要的可以參考一下
    2022-06-06
  • java報錯:javax.xml.bind.JAXBException:?JAXB解決辦法

    java報錯:javax.xml.bind.JAXBException:?JAXB解決辦法

    這篇文章主要介紹了java報錯:javax.xml.bind.JAXBException:?JAXB的解決辦法,文中通過示例提出多種解決方案,幫助開發(fā)者快速定位并解決問題,需要的朋友可以參考下
    2025-05-05
  • java web項目實現(xiàn)文件下載實例代碼

    java web項目實現(xiàn)文件下載實例代碼

    現(xiàn)在項目里面有個需求,需要把系統(tǒng)產(chǎn)生的日志文件給下載到本地 先獲取所有的日志文件列表,顯示到界面,選擇一個日志文件,把文件名傳到后臺
    2013-09-09
  • EntityWrapper如何在and條件中嵌套or語句

    EntityWrapper如何在and條件中嵌套or語句

    這篇文章主要介紹了EntityWrapper如何在and條件中嵌套or語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論

高安市| 郑州市| 饶平县| 墨竹工卡县| 龙里县| 江华| 微山县| 钟山县| 达州市| 荥经县| 高雄县| 手机| 延庆县| 琼中| 台湾省| 平顺县| 疏附县| 鄯善县| 云安县| 鸡泽县| 桑日县| 钟山县| 赤城县| 万盛区| 仪征市| 祁连县| 井研县| 松溪县| 称多县| 徐州市| 蒙自县| 都江堰市| 寿宁县| 工布江达县| 大埔区| 扎兰屯市| 东城区| 胶州市| 武功县| 舟山市| 墨脱县|