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

本文涉及的产品
访问控制,不限时长
简介: 后端进阶之路——浅谈Spring Security用户、角色、权限和访问规则(三)



引言

继上篇后端进阶之路——深入理解Spring Security配置(二)

1. 定义用户

使用内存方式定义用户

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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("user")
                .password("{noop}password") // 使用 "{noop}" 前缀表示密码不加密
                .roles("USER");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin();
    }
}


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

使用数据库方式定义用户

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


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

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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private DataSource dataSource;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?")
                .authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin();
    }
}


上述示例中,通过dataSource注入了数据源,然后使用jdbcAuthentication配置了基于数据库的认证和授权。.usersByUsernameQuery()方法指定了查询用户名、密码和启用状态的SQL语句,.authoritiesByUsernameQuery()方法指定了查询用户名和角色的SQL语句。

2. 定义角色

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


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

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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private DataSource dataSource;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?")
                .authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN") // 需要"ADMIN"角色才能访问"/admin/**"路径
                .anyRequest().authenticated()
                .and()
                .formLogin();
    }
}

在上述示例中,.antMatchers("/admin/**").hasRole("ADMIN")指定了只有拥有"ADMIN"角色的用户才能访问"/admin/**"路径。这样,我们可以根据不同的角色来限制用户对某些资源的访问权限。

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


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

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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private DataSource dataSource;
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?")
                .authoritiesByUsernameQuery("SELECT username, role FROM users WHERE username=?");
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        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>

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

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
@Component
public class MyAuthorization {
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public void adminOnly() {
        // 只有具有ROLE_ADMIN角色的用户才能调用此方法
    }
    @PreAuthorize("hasAuthority('WRITE_PERMISSION')")
    public void writeAccess() {
        // 只有具有WRITE_PERMISSION权限的用户才能调用此方法
    }
}

在上述代码中,MyAuthorization类包含了两个方法:adminOnly()和writeAccess()。这两个方法都使用了@PreAuthorize注解来定义权限。


hasRole('ROLE_ADMIN')表示只有具有ROLE_ADMIN角色的用户才能调用adminOnly()方法。


hasAuthority('WRITE_PERMISSION')表示只有具有WRITE_PERMISSION权限的用户才能调用writeAccess()方法。


然后,在你的业务逻辑中,可以通过依赖注入的方式使用MyAuthorization类,并调用相应的方法。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
    private final MyAuthorization myAuthorization;
    @Autowired
    public MyService(MyAuthorization myAuthorization) {
        this.myAuthorization = myAuthorization;
    }
    public void doSomething() {
        myAuthorization.adminOnly(); // 调用需要admin权限的方法
        myAuthorization.writeAccess(); // 调用需要writeAccess权限的方法
    }
}

在上述代码中,MyService类依赖注入了MyAuthorization类,并在doSomething()方法中调用了其中的两个方法。


4. 访问规则

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


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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/user/**").hasAnyRole("USER", "ADMIN")
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .httpBasic();
    }
}


在上述代码中,我们使用authorizeRequests()方法来配置路径的访问规则。.antMatchers()方法用于指定要匹配的路径模式,并使用hasRole()或hasAnyRole()方法指定需要具有的角色。在这个例子中,如果请求的路径以"/admin/"开头,则用户需要具有"ADMIN"角色。


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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .expressionHandler(expressionHandler())
            .antMatchers("/admin/**").access("hasRole('ADMIN') or hasIpAddress('127.0.0.1')")
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .and()
            .httpBasic();
    }
    @Bean
    public DefaultWebSecurityExpressionHandler expressionHandler() {
        DefaultWebSecurityExpressionHandler expressionHandler = new DefaultWebSecurityExpressionHandler();
        expressionHandler.setRoleHierarchy(roleHierarchy());
        return expressionHandler;
    }
    @Bean
    public RoleHierarchy roleHierarchy() {
        RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
        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风格的路径匹配规则或表达式语言编写更复杂的访问规则,以限制用户对特定路径或功能的访问权限。

通过定义这些元素,可以确保系统的安全性和可靠性。

相关实践学习
消息队列+Serverless+Tablestore:实现高弹性的电商订单系统
基于消息队列以及函数计算,快速部署一个高弹性的商品订单系统,能够应对抢购场景下的高并发情况。
云安全基础课 - 访问控制概述
课程大纲 课程目标和内容介绍视频时长 访问控制概述视频时长 身份标识和认证技术视频时长 授权机制视频时长 访问控制的常见攻击视频时长
相关文章
|
3月前
|
安全 Java 数据安全/隐私保护
使用Spring Security实现细粒度的权限控制
使用Spring Security实现细粒度的权限控制
|
3月前
|
安全 Java 数据库
实现基于Spring Security的权限管理系统
实现基于Spring Security的权限管理系统
|
3月前
|
安全 Java 数据安全/隐私保护
解析Spring Security中的权限控制策略
解析Spring Security中的权限控制策略
|
3月前
|
缓存 监控 Java
优化Spring Boot应用的数据库访问性能
优化Spring Boot应用的数据库访问性能
|
2月前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
10天前
|
消息中间件 NoSQL 安全
(转)Spring Boot加载 不同位置的 application.properties配置文件顺序规则
这篇文章介绍了Spring Boot加载配置文件的顺序规则,包括不同位置的application.properties文件的加载优先级,以及如何通过命令行参数或环境变量来指定配置文件的名称和位置。
|
2月前
|
XML Java 数据格式
后端框架学习1-----Spring
这篇文章是Spring学习笔记,涵盖了Spring框架的基本概念、IoC容器、依赖注入(DI)的方式(包括set注入和构造函数注入)、复杂类型依赖注入、Bean的作用域和自动装配,以及Spring注解开发方式,包括@Autowired和@Qualifier注解的使用,以及@Component及其衍生注解的功能。
后端框架学习1-----Spring
|
2月前
|
前端开发 JavaScript Java
【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
【Azure 应用服务】App Service For Windows 中如何设置代理实现前端静态文件和后端Java Spring Boot Jar包
|
2月前
|
Java Spring
Spring Boot实战:静态资源无法访问
Spring Boot实战:静态资源无法访问
55 0
|
3月前
|
缓存 监控 Java
优化Spring Boot应用的数据库访问性能
优化Spring Boot应用的数据库访问性能
下一篇
无影云桌面