spring?boot權(quán)限管理的幾種常見方式
Spring Boot 提供了多種權(quán)限管理方式,以下是幾種常見的方法,以及相應(yīng)的示例:
1、基于角色的訪問控制(Role-Based Access Control,RBAC)
在基于角色的訪問控制中,權(quán)限分配給角色,然后將角色分配給用戶。這種方法簡(jiǎn)化了權(quán)限管理,因?yàn)槟恍枰芾斫巧陀脩糁g的關(guān)系。
示例:使用 Spring Security 實(shí)現(xiàn) RBAC
1.1. 添加 Spring Security 依賴項(xiàng)到 pom.xml :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
1.2. 創(chuàng)建一個(gè) 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
實(shí)現(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)限管理中,為每個(gè)資源定義一個(gè)訪問控制列表,指定哪些用戶或角色可以訪問該資源。這種方法適用于需要對(duì)每個(gè)資源進(jìn)行細(xì)粒度控制的場(chǎng)景。
示例:使用 Spring Security 的 ACL 模塊實(shí)現(xiàn) ACL
3.1. 添加 Spring Security ACL 依賴項(xiàng)到 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)限評(píng)估器:
@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)限管理的幾種方式。實(shí)際應(yīng)用中,您可能需要根據(jù)項(xiàng)目需求進(jìn)行更詳細(xì)的配置和實(shí)現(xiàn)。
總結(jié)
到此這篇關(guān)于spring boot權(quán)限管理的幾種常見方式的文章就介紹到這了,更多相關(guān)spring boot權(quán)限管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 使用 logback、logstash、ELK 記錄日志文件的方法
這篇文章主要介紹了Spring Boot 使用 logback、logstash、ELK 記錄日志文件的思路詳解,文中給大家提到了logback 取代 log4j的理由,需要的朋友可以參考下2017-12-12JavaWeb Session 會(huì)話管理實(shí)例詳解
這篇文章主要介紹了JavaWeb Session 會(huì)話管理的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來)
這篇文章主要介紹了Spring Boot 2.X整合Spring-cache(讓你的網(wǎng)站速度飛起來),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09java實(shí)現(xiàn)人民幣大小寫轉(zhuǎn)換方法分享
本文介紹java人民幣數(shù)字大小寫轉(zhuǎn)換方法,代碼中有注釋,大家直接看代碼吧2014-01-01前置++和后置++ 運(yùn)算的詳解及實(shí)例代碼
這篇文章主要介紹了前置++和后置++ 的相關(guān)資料,并附示例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-09-09