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

spring?boot權(quán)限管理的幾種常見方式

 更新時間:2023年08月08日 10:12:20   作者:orton777  
這篇文章主要給大家介紹了關(guān)于spring?boot權(quán)限管理的幾種常見方式,在Web應(yīng)用程序中,用戶權(quán)限管理是至關(guān)重要的,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下

Spring Boot 提供了多種權(quán)限管理方式,以下是幾種常見的方法,以及相應(yīng)的示例:

1、基于角色的訪問控制(Role-Based Access Control,RBAC)

在基于角色的訪問控制中,權(quán)限分配給角色,然后將角色分配給用戶。這種方法簡化了權(quán)限管理,因為您只需要管理角色和用戶之間的關(guān)系。

示例:使用 Spring Security 實現(xiàn) RBAC

1.1. 添加 Spring Security 依賴項到 pom.xml :

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

1.2. 創(chuàng)建一個 SecurityConfig 類,繼承 WebSecurityConfigurerAdapter ,并配置角色和權(quán)限:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/user/**").hasAnyRole("ADMIN", "USER")
            .antMatchers("/").permitAll()
            .and().formLogin();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("admin").password(passwordEncoder().encode("admin123")).roles("ADMIN")
            .and()
            .withUser("user").password(passwordEncoder().encode("user123")).roles("USER");
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

2、基于屬性的訪問控制(Attribute-Based Access Control,ABAC)

在基于屬性的訪問控制中,權(quán)限是基于用戶、資源和環(huán)境屬性的。這種方法提供了更細(xì)粒度的權(quán)限控制,但可能更難管理。

示例:使用 Spring Security 的 @PreAuthorize 實現(xiàn) ABAC

2.1. 在 SecurityConfig  類中啟用方法安全性:

@EnableGlobalMethodSecurity(prePostEnabled = true)

2.2. 在需要保護(hù)的方法上添加 @PreAuthorize  注解:

@RestController
@RequestMapping("/api")
public class ApiController {
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin")
    public String admin() {
        return "Admin area";
    }
    @PreAuthorize("hasRole('USER')")
    @GetMapping("/user")
    public String user() {
        return "User area";
    }
}

3、基于訪問控制列表(Access Control List,ACL)

在基于訪問控制列表的權(quán)限管理中,為每個資源定義一個訪問控制列表,指定哪些用戶或角色可以訪問該資源。這種方法適用于需要對每個資源進(jìn)行細(xì)粒度控制的場景。

示例:使用 Spring Security 的 ACL 模塊實現(xiàn) ACL

3.1. 添加 Spring Security ACL 依賴項到 pom.xml :

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-acl</artifactId>
    <version>5.6.1</version>
</dependency>

3.2. 配置 ACL 數(shù)據(jù)源、服務(wù)和權(quán)限評估器:

@Configuration
public class AclConfig {
    @Autowired
    private DataSource dataSource;
    @Bean
    public JdbcMutableAclService aclService() {
        return new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache());
    }
    @Bean
    public AclAuthorizationStrategy aclAuthorizationStrategy() {
        return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_ADMIN"));
    }
    @Bean
    public PermissionGrantingStrategy permissionGrantingStrategy() {
        return new DefaultPermissionGrantingStrategy(new ConsoleAuditLogger());
    }
    @Bean
    public EhCacheBasedAclCache aclCache() {
        return new EhCacheBasedAclCache(ehCacheFactoryBean().getObject(), permissionGrantingStrategy(), aclAuthorizationStrategy());
    }
    @Bean
    public EhCacheFactoryBean ehCacheFactoryBean() {
        EhCacheFactoryBean factoryBean = new EhCacheFactoryBean();
        factoryBean.setCacheManager(cacheManager().getObject());
        factoryBean.setCacheName("aclCache");
        return factoryBean;
    }
    @Bean
    public EhCacheManagerFactoryBean cacheManager() {
        return new EhCacheManagerFactoryBean();
    }
    @Bean
    public LookupStrategy lookupStrategy() {
        return new BasicLookupStrategy(dataSource, aclCache(), aclAuthorizationStrategy(), new ConsoleAuditLogger());
    }
    @Bean
    public AclPermissionEvaluator permissionEvaluator() {
        return new AclPermissionEvaluator(aclService());
    }
}

3.3. 在需要保護(hù)的方法上添加 @PreAuthorize  注解,使用 hasPermission  表達(dá)式:

@PreAuthorize("hasPermission(#resourceId, 'com.example.Resource', 'read')")
@GetMapping("/resource/{resourceId}")
public String getResource(@PathVariable Long resourceId) {
    return "Resource " + resourceId;
}

這些示例僅用于演示 Spring Boot 中權(quán)限管理的幾種方式。實際應(yīng)用中,您可能需要根據(jù)項目需求進(jìn)行更詳細(xì)的配置和實現(xiàn)。

總結(jié)

到此這篇關(guān)于spring boot權(quán)限管理的幾種常見方式的文章就介紹到這了,更多相關(guān)spring boot權(quán)限管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Base64加解密的實現(xiàn)方式實例詳解

    Base64加解密的實現(xiàn)方式實例詳解

    這篇文章主要介紹了Base64加解密的實現(xiàn)方式實例詳解的相關(guān)資料,這里提供了實現(xiàn)實例,幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • 實例解析如何正確使用Java數(shù)組

    實例解析如何正確使用Java數(shù)組

    同一種類型數(shù)據(jù)的集合。其實數(shù)組就是一個容器。運算的時候有很多數(shù)據(jù)參與運算,那么首先需要做的是什么下面我們就一起來看看。
    2016-07-07
  • Java線程同步、同步方法實例詳解

    Java線程同步、同步方法實例詳解

    本篇文章主要通過實例介紹了Java線程:線程的同步-同步方法,需要的朋友可以參考下
    2017-04-04
  • SpringBoot如何實現(xiàn)一個Redis限流注解

    SpringBoot如何實現(xiàn)一個Redis限流注解

    這篇文章主要介紹了利用SpringBoot實現(xiàn)一個Redis限流注解方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • mybatis的mapper對應(yīng)的xml寫法及配置詳解

    mybatis的mapper對應(yīng)的xml寫法及配置詳解

    這篇文章給大家介紹mybatis的mapper對應(yīng)的xml寫法及配置詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-05-05
  • SpringBoot整合MongoDB的完整操作指南

    SpringBoot整合MongoDB的完整操作指南

    在實際項目開發(fā)中,合理封裝MongoDB的操作工具類,可以大幅提升代碼復(fù)用性和維護(hù)性,本文將帶你從零開始實現(xiàn)一個功能完善的MongoDB工具類,涵蓋基礎(chǔ)CRUD和高級查詢功能,需要的朋友可以參考下
    2026-02-02
  • java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能

    java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能

    本文主要介紹了java實現(xiàn)把對象數(shù)組通過excel方式導(dǎo)出的功能的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • 詳解使用spring validation完成數(shù)據(jù)后端校驗

    詳解使用spring validation完成數(shù)據(jù)后端校驗

    這篇文章主要介紹了詳解使用spring validation完成數(shù)據(jù)后端校驗,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 一文教你如何使用AES對接口參數(shù)進(jìn)行加密

    一文教你如何使用AES對接口參數(shù)進(jìn)行加密

    這篇文章主要是想為大家介紹一下如何使用AES實現(xiàn)對接口參數(shù)進(jìn)行加密,文中的示例代碼簡潔易懂,具有一定的借鑒價值,需要的小伙伴可以了解一下
    2023-08-08
  • Java中5種方式實現(xiàn)String反轉(zhuǎn)

    Java中5種方式實現(xiàn)String反轉(zhuǎn)

    下面小編就為大家?guī)硪黄狫ava中5種方式實現(xiàn)String反轉(zhuǎn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。
    2016-06-06

最新評論

富顺县| 平泉县| 闸北区| 邯郸市| 易门县| 沧州市| 卢氏县| 张家界市| 三江| 潢川县| 九龙城区| 五寨县| 鞍山市| 阿拉善盟| 无锡市| 朝阳市| 方山县| 历史| 通州市| 南城县| 宁津县| 兴山县| 峨眉山市| 肃北| 垣曲县| 泌阳县| 武清区| 巴东县| 藁城市| 金坛市| 丹江口市| 桃园市| 长宁区| 宁明县| 荣成市| 永清县| 新蔡县| 徐闻县| 虹口区| 阜南县| 大厂|