0


后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)

前言

在这里插入图片描述
「作者主页」:雪碧有白泡泡
「个人网站」:雪碧的个人网站
「推荐专栏」:

★java一站式服务 ★
★前端炫酷代码分享
★★ uniapp-从构建到提升★
★ 从0到英雄,vue成神之路★
★ 解决算法,一个专栏就够了★
★ 架构咱们从0说★
★ 数据流通的精妙之道★

★后端进阶之路★

请添加图片描述

在这里插入图片描述

文章目录

引言

继上篇后端进阶之路——深入理解Spring Security配置(二)
在这里插入图片描述

1. 定义用户

使用内存方式定义用户

在内存中定义用户是一种简单的方法,适用于开发和测试环境。我们可以在Spring Security的配置类中使用

InMemoryUserDetailsManager

来定义用户。下面是一个示例:

@Configuration@EnableWebSecuritypublicclassSecurityConfigextendsWebSecurityConfigurerAdapter{@Overrideprotectedvoidconfigure(AuthenticationManagerBuilder auth)throwsException{
        auth.inMemoryAuthentication().withUser("user").password("{noop}password")// 使用 "{noop}" 前缀表示密码不加密.roles("USER");}@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{
        http.authorizeRequests().anyRequest().authenticated().and().formLogin();}}

上述示例中,使用

withUser

方法定义了一个用户名为"user",密码为"password",角色为"USER"的用户。注意,密码的前缀"{noop}"表示密码不加密,这只适用于开发和测试环境。

使用数据库方式定义用户

在实际生产环境中,通常会将用户信息存储在数据库中。使用数据库方式定义用户需要进行以下步骤:

  1. 创建数据库表格来存储用户信息,例如表格名为"users",包含列"username"、“password"和"role”。
  2. 配置Spring Security以连接到数据库,并使用数据库中的用户信息进行认证和授权。

下面是一个使用数据库方式定义用户的示例:

@Configuration@EnableWebSecuritypublicclassSecurityConfigextendsWebSecurityConfigurerAdapter{@AutowiredprivateDataSource dataSource;@Overrideprotectedvoidconfigure(AuthenticationManagerBuilder auth)throwsException{
        auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?").authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");}@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{
        http.authorizeRequests().anyRequest().authenticated().and().formLogin();}}

上述示例中,通过

dataSource

注入了数据源,然后使用

jdbcAuthentication

配置了基于数据库的认证和授权。

.usersByUsernameQuery()

方法指定了查询用户名、密码和启用状态的SQL语句,

.authoritiesByUsernameQuery()

方法指定了查询用户名和角色的SQL语句。

2. 定义角色

在Spring Security中,可以使用角色来组织和控制权限。角色是一组权限的集合,可以通过将角色与用户关联来授予用户相应的权限。

以下是在Spring Security中定义角色并将其与用户关联的示例代码:

创建角色并将其与用户关联

@Configuration@EnableWebSecuritypublicclassSecurityConfigextendsWebSecurityConfigurerAdapter{@AutowiredprivateDataSource dataSource;@Overrideprotectedvoidconfigure(AuthenticationManagerBuilder auth)throwsException{
        auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?").authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");}@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{
        http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN")// 需要"ADMIN"角色才能访问"/admin/**"路径.anyRequest().authenticated().and().formLogin();}}

在上述示例中,

.antMatchers("/admin/**").hasRole("ADMIN")

指定了只有拥有"ADMIN"角色的用户才能访问"/admin/**"路径。这样,我们可以根据不同的角色来限制用户对某些资源的访问权限。

解释如何使用角色来组织和控制权限

在Spring Security中,可以使用角色来定义访问控制规则,并使用这些规则来保护应用程序的不同部分。通过为用户分配不同的角色,可以实现对不同用户的权限控制。

以下是一个使用角色进行权限控制的示例代码:

@Configuration@EnableWebSecuritypublicclassSecurityConfigextendsWebSecurityConfigurerAdapter{@AutowiredprivateDataSource dataSource;@Overrideprotectedvoidconfigure(AuthenticationManagerBuilder auth)throwsException{
        auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?").authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");}@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{
        http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN")// 需要"ADMIN"角色才能访问"/admin/**"路径.antMatchers("/user/**").hasRole("USER")// 需要"USER"角色才能访问"/user/**"路径.anyRequest().authenticated().and().formLogin();}}

上述示例中,使用

.antMatchers("/admin/**").hasRole("ADMIN")

.antMatchers("/user/**").hasRole("USER")

定义了不同路径需要不同角色的访问权限。只有拥有相应角色的用户才能访问对应的路径。

3. 定义权限

权限是指在系统中对特定资源或操作进行访问控制的能力。它是用于确保只有经过授权的用户或角色才能执行某些敏感操作或访问某些受限资源的机制。权限的定义和管理对于确保系统的安全性和保护重要数据非常重要。

在Spring Security中,我们可以使用

@PreAuthorize

@PostAuthorize

@Secured

等注解来定义权限。这些注解可以放置在方法上,用于限制只有具有特定权限的用户才能调用该方法。

下面是一个示例,演示了如何在Spring Security中定义权限:

首先,确保你已经添加了Spring Security的依赖到你的项目中。

<!-- pom.xml --><dependencies>
    ...
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
    ...
</dependencies>

接下来,创建一个自定义的权限验证类。

importorg.springframework.security.access.prepost.PreAuthorize;importorg.springframework.stereotype.Component;@ComponentpublicclassMyAuthorization{@PreAuthorize("hasRole('ROLE_ADMIN')")publicvoidadminOnly(){// 只有具有ROLE_ADMIN角色的用户才能调用此方法}@PreAuthorize("hasAuthority('WRITE_PERMISSION')")publicvoidwriteAccess(){// 只有具有WRITE_PERMISSION权限的用户才能调用此方法}}

在上述代码中,

MyAuthorization

类包含了两个方法:

adminOnly()

writeAccess()

。这两个方法都使用了

@PreAuthorize

注解来定义权限。

hasRole('ROLE_ADMIN')

表示只有具有

ROLE_ADMIN

角色的用户才能调用

adminOnly()

方法。

hasAuthority('WRITE_PERMISSION')

表示只有具有

WRITE_PERMISSION

权限的用户才能调用

writeAccess()

方法。

然后,在你的业务逻辑中,可以通过依赖注入的方式使用

MyAuthorization

类,并调用相应的方法。

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;@ServicepublicclassMyService{privatefinalMyAuthorization myAuthorization;@AutowiredpublicMyService(MyAuthorization myAuthorization){this.myAuthorization = myAuthorization;}publicvoiddoSomething(){
        myAuthorization.adminOnly();// 调用需要admin权限的方法
        myAuthorization.writeAccess();// 调用需要writeAccess权限的方法}}

在上述代码中,

MyService

类依赖注入了

MyAuthorization

类,并在

doSomething()

方法中调用了其中的两个方法。

4. 访问规则

当使用Spring Security进行访问控制时,可以通过Ant风格的路径匹配规则和表达式语言来定义更复杂的访问规则。下面是一些示例代码片段,用于说明如何使用这些规则:

使用Ant风格的路径匹配规则

@Configuration@EnableWebSecuritypublicclassSecurityConfigextendsWebSecurityConfigurerAdapter{@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{
        http.authorizeRequests().antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/user/**").hasAnyRole("USER","ADMIN").anyRequest().authenticated().and().formLogin().and().httpBasic();}}

在上述代码中,我们使用

authorizeRequests()

方法来配置路径的访问规则。

.antMatchers()

方法用于指定要匹配的路径模式,并使用

hasRole()

hasAnyRole()

方法指定需要具有的角色。在这个例子中,如果请求的路径以"/admin/"开头,则用户需要具有"ADMIN"角色。

使用表达式语言进行更复杂的访问规则定义

@Configuration@EnableWebSecuritypublicclassSecurityConfigextendsWebSecurityConfigurerAdapter{@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{
        http.authorizeRequests().expressionHandler(expressionHandler()).antMatchers("/admin/**").access("hasRole('ADMIN') or hasIpAddress('127.0.0.1')").anyRequest().authenticated().and().formLogin().and().httpBasic();}@BeanpublicDefaultWebSecurityExpressionHandlerexpressionHandler(){DefaultWebSecurityExpressionHandler expressionHandler =newDefaultWebSecurityExpressionHandler();
        expressionHandler.setRoleHierarchy(roleHierarchy());return expressionHandler;}@BeanpublicRoleHierarchyroleHierarchy(){RoleHierarchyImpl roleHierarchy =newRoleHierarchyImpl();
        roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");return roleHierarchy;}}

在上述代码中,我们使用

.access()

方法来定义更复杂的访问规则。这里我们使用表达式语言,可以通过

hasRole()

hasIpAddress()

等方法来判断用户是否有权限访问路径。在这个例子中,如果请求的路径以"/admin/“开头,并且用户具有"ADMIN"角色或者IP地址为"127.0.0.1”,则允许访问。

另外,我们还使用

DefaultWebSecurityExpressionHandler

RoleHierarchyImpl

来配置角色层次关系。在这个例子中,"ROLE_ADMIN"角色被认为是"ROLE_USER"角色的父角色,因此具有"ROLE_ADMIN"角色的用户也将被授予"ROLE_USER"角色的权限。

小结

通过定义用户、角色、权限和访问规则
可以在SpringSecurity中实现灵活的访问控制和权限管理。可以使用内存方式或数据库方式定义用户,设置用户名、密码和角色等信息。角色可以用来组织和控制权限,可以将一组权限赋予特定角色,并将角色分配给用户。权限是指用户可以执行的特定操作或访问的资源,可以细化到每个功能或数据级别。可以使用Ant风格的路径匹配规则或表达式语言编写更复杂的访问规则,以限制用户对特定路径或功能的访问权限。
通过定义这些元素,可以确保系统的安全性和可靠性。

在这里插入图片描述

标签: spring java 后端

本文转载自: https://blog.csdn.net/Why_does_it_work/article/details/132109259
版权归原作者 雪碧有白泡泡 所有, 如有侵权,请联系我们删除。

“后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)”的评论:

还没有评论