Spring Boot 实现不同用户不同访问权限

前提

近期在使用 Spring Boot,用户角色被分为管理者和普通用户;角色不同,权限也就存在不同。

在 Spring Boot 里实现不同用户拥有不同访问权限,可借助 Spring Security 框架达成。

实现

1. 添加必要依赖

首先要在 pom.xml 里添加 Spring Security 和 JPA 的依赖。

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency>
</dependencies>

2. 数据库表设计

创建三张表,分别是用户表、角色表以及用户角色关联表:

CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT,username VARCHAR(50) NOT NULL UNIQUE,password VARCHAR(100) NOT NULL,enabled BOOLEAN DEFAULT true
);CREATE TABLE roles (id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(50) NOT NULL UNIQUE
);CREATE TABLE user_roles (user_id INT NOT NULL,role_id INT NOT NULL,PRIMARY KEY (user_id, role_id),FOREIGN KEY (user_id) REFERENCES users(id),FOREIGN KEY (role_id) REFERENCES roles(id)
);

3. 实体类设计

创建与数据库表对应的实体类:

// User.java
import javax.persistence.*;
import java.util.Set;@Entity
@Table(name = "users")
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String username;private String password;private boolean enabled;@ManyToMany(fetch = FetchType.EAGER)@JoinTable(name = "user_roles",joinColumns = @JoinColumn(name = "user_id"),inverseJoinColumns = @JoinColumn(name = "role_id"))private Set<Role> roles;// getters and setters
}// Role.java
import javax.persistence.*;@Entity
@Table(name = "roles")
public class Role {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;private String name;// getters and setters
}

4. 创建 Repository 接口

为 User 和 Role 分别创建 Repository 接口,用于数据访问:

// UserRepository.java
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {User findByUsername(String username);
}// RoleRepository.java
import org.springframework.data.jpa.repository.JpaRepository;public interface RoleRepository extends JpaRepository<Role, Long> {Role findByName(String name);
}

5. 实现 UserDetailsService

实现 Spring Security 的 UserDetailsService 接口,从数据库加载用户信息:

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.List;
import java.util.Set;@Service
public class CustomUserDetailsService implements UserDetailsService {private final UserRepository userRepository;public CustomUserDetailsService(UserRepository userRepository) {this.userRepository = userRepository;}@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {User user = userRepository.findByUsername(username);if (user == null) {throw new UsernameNotFoundException("User not found with username: " + username);}return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),user.isEnabled(),true,true,true,getAuthorities(user.getRoles()));}private List<GrantedAuthority> getAuthorities(Set<Role> roles) {List<GrantedAuthority> authorities = new ArrayList<>();for (Role role : roles) {authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));}return authorities;}
}

6. 配置 Spring Security

对 Spring Security 进行配置,设置不同 URL 的访问权限:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;@Configuration
@EnableWebSecurity
public class SecurityConfig {@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public/**").permitAll().antMatchers("/admin/**").hasRole("ADMIN").antMatchers("/user/**").hasAnyRole("USER", "ADMIN").anyRequest().authenticated().and().formLogin().loginPage("/login").permitAll().and().logout().permitAll();return http.build();}
}

7. 创建控制器

创建不同权限的控制器示例:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/public/hello")public String publicHello() {return "Public Hello!";}@GetMapping("/user/hello")public String userHello() {return "User Hello!";}@GetMapping("/admin/hello")public String adminHello() {return "Admin Hello!";}
}

8. 测试用户数据

创建测试用户数据,以便进行测试:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import java.util.Collections;
import java.util.HashSet;@Component
public class DataInitializer implements CommandLineRunner {@Autowiredprivate UserRepository userRepository;@Autowiredprivate RoleRepository roleRepository;@Autowiredprivate PasswordEncoder passwordEncoder;@Overridepublic void run(String... args) throws Exception {// 创建角色Role adminRole = roleRepository.findByName("ADMIN");if (adminRole == null) {adminRole = new Role();adminRole.setName("ADMIN");roleRepository.save(adminRole);}Role userRole = roleRepository.findByName("USER");if (userRole == null) {userRole = new Role();userRole.setName("USER");roleRepository.save(userRole);}// 创建管理员用户User adminUser = userRepository.findByUsername("admin");if (adminUser == null) {adminUser = new User();adminUser.setUsername("admin");adminUser.setPassword(passwordEncoder.encode("admin123"));adminUser.setEnabled(true);adminUser.setRoles(new HashSet<>(Collections.singletonList(adminRole)));userRepository.save(adminUser);}// 创建普通用户User normalUser = userRepository.findByUsername("user");if (normalUser == null) {normalUser = new User();normalUser.setUsername("user");normalUser.setPassword(passwordEncoder.encode("user123"));normalUser.setEnabled(true);normalUser.setRoles(new HashSet<>(Collections.singletonList(userRole)));userRepository.save(normalUser);}}
}

权限控制说明

  • @PreAuthorize 注解:能在方法级别进行权限控制。例如:
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin/hello")
    public String adminHello() {return "Admin Hello!";
    }
    
  • 角色继承:可以让 ADMIN 角色继承 USER 角色的权限,配置如下:
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public/**").permitAll().antMatchers("/user/**").hasRole("USER").antMatchers("/admin/**").hasRole("ADMIN").anyRequest().authenticated().and().roleHierarchy(roleHierarchy());return http.build();
    }@Bean
    public RoleHierarchy roleHierarchy() {RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");return roleHierarchy;
    }
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/912420.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/912420.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

华沿协作机器人:数字孪生技术赋能焊接领域智能化升级

在工业4.0与智能制造浪潮的推动下&#xff0c;焊接行业正经历从传统工艺向数字化、柔性化转型的关键阶段。作为国内协作机器人领域的创新者&#xff0c;华沿机器人通过融合数字孪生、智能感知与多轴协同技术&#xff0c;在焊接场景中实现了技术突破与应用创新。本文将从技术原理…

Linux中部署Nacos保姆级教程

前置说明&#xff1a; Dokcer部署Nacos官方文档&#xff1a;Nacos Docker 快速开始 | Nacos 官网 一、Nacos版本说明 Nacos 1.x 版本 Nacos 1.1.3 &#xff1a;是一个相对稳定的版本&#xff0c;在一段时期内被广泛使用&#xff0c;但目前该版本已经下线&#xff0c;不再单独维…

战神授权后台报错:Parse error: syntax error, unexpected end of file in解决办法

问题现象分析 当您在战神授权后台遇到"Parse error: syntax error, unexpected end of file"这个错误时&#xff0c;说明PHP解析器在解析脚本文件时遇到了意外结束的情况。这种错误通常发生在PHP代码结构不完整时&#xff0c;比如缺少闭合的大括号、分号或者PHP结束…

HTML<span>元素详解

HTML<span>元素详解 <span> 是 HTML 中最常用的内联(inline)容器元素&#xff0c;用于对文档中的部分文本或内容进行标记和样式化。 一、基本语法 <span>内容</span>二、主要特点 内联元素&#xff1a;不会独占一行&#xff0c;只占据内容所需宽度无…

vscode ssh远程连接到Linux并实现免密码登录

vscode ssh远程连接到Linux并实现免密码登录 文章目录 vscode ssh远程连接到Linux并实现免密码登录一、安装VSCode扩展二、Linux侧工作三、连接四、实现免密登录 一、安装VSCode扩展 扩展一栏搜索remote找到Remote Development插件直接点击Install安装即可 二、Linux侧工作 U…

超级详细 的 Apache Camel 教程

前言 通过本教程学习 Apache Camel 的基础知识并在 Spring Boot 项目上创建您的第一个 Camel。 想开始使用Apache Camel吗&#xff1f;这是我关于这个流行的 Java 集成框架的教程。 我为完整的初学者编写了这个 Apache Camel 教程。它向您介绍了 Camel 的核心概念&#xff0c;并…

使用GithubActions和腾讯CloudBase自动发布静态网页

腾讯 CloudBase 可以用于托管静态网站&#xff0c;服务开通之后&#xff0c;使用 CloudBase CLI 可以将本地静态网站上传到 CloudBase&#xff0c;并生成相应的访问域名。 配置 Workflow 创建 .github/workflows/deploy.yml 文件, 编辑内容如下&#xff1a; name: Deploy to…

《聊一聊ZXDoc》之汽车标定、台架标定、三高标定

ZXDoc支持XCP/CCP标定功能&#xff0c;标定工作贯穿主机厂与Tier1厂商汽车ECU研发、生产、测试的整个流程&#xff0c;是保障ECU性能达标、功能稳定的关键。 什么是XCP/CCP标定&#xff1f; XCP/CCP标定是汽车电子领域用于ECU标定和测量的核心通信协议&#xff0c;由ASAM组织…

【目标检测】评估指标详解:Precision/Recall/F1-Score

&#x1f9d1; 博主简介&#xff1a;曾任某智慧城市类企业算法总监&#xff0c;目前在美国市场的物流公司从事高级算法工程师一职&#xff0c;深耕人工智能领域&#xff0c;精通python数据挖掘、可视化、机器学习等&#xff0c;发表过AI相关的专利并多次在AI类比赛中获奖。CSDN…

【unity游戏开发——网络】网络协议、TCP vs UDP 本质区别

注意&#xff1a;考虑到热更新的内容比较多&#xff0c;我将热更新的内容分开&#xff0c;并全部整合放在【unity游戏开发——网络】专栏里&#xff0c;感兴趣的小伙伴可以前往逐一查看学习。 文章目录 一、网络协议概述二、OSI七层模型三、TCP/IP四层模型四、核心传输协议对比…

Spark Streaming 与 Flink 实时数据处理方案对比与选型指南

Spark Streaming 与 Flink 实时数据处理方案对比与选型指南 实时数据处理在互联网、电商、物流、金融等领域均有大量应用&#xff0c;面对海量流式数据&#xff0c;Spark Streaming 和 Flink 成为两大主流开源引擎。本文基于生产环境需求&#xff0c;从整体架构、编程模型、容…

鸿蒙HarmonyOS 5小游戏实践:记忆翻牌(附:源代码)

记忆翻牌游戏是一款经典的益智游戏&#xff0c;它能有效锻炼玩家的记忆力和观察能力。本文将详细介绍如何使用鸿蒙&#xff08;HarmonyOS&#xff09;的ArkUI框架开发一款完整的记忆翻牌游戏&#xff0c;涵盖游戏设计、核心逻辑实现和界面构建的全过程。 游戏设计概述 记忆翻牌…

【Linux庖丁解牛】— 文件系统!

1 引⼊"块"概念 其实硬盘是典型的“块”设备&#xff0c;操作系统读取硬盘数据的时候&#xff0c;其实是不会⼀个个扇区地读取&#xff0c;这样 效率太低&#xff0c;⽽是⼀次性连续读取多个扇区&#xff0c;即⼀次性读取⼀个”块”&#xff08;block&#xff09;。…

如何通过自动化减少重复性工作

通过自动化减少重复性工作的关键策略包括&#xff1a;1、识别可被规则化操作的任务、2、引入RPA&#xff08;机器人流程自动化&#xff09;工具、3、整合AI与业务流程系统、4、部署脚本与低代码平台、5、持续优化自动化场景与效率。 其中&#xff0c;“引入RPA工具”被广泛认为…

知识变现全链路设计:从IP打造到商业闭环的系统方法论|创客匠人

一、变现低效根源&#xff1a;碎片化努力为何换不来持续增长&#xff1f; 创客匠人服务上千位知识创业者后发现&#xff0c;变现乏力多因缺乏系统设计&#xff1a;某营销专家的课程因定位模糊、表达生硬、渠道单一&#xff0c;低价仍少有人问。文档中提出的“六大超级设计公式…

如何利用人工智能大模型提升流量质量

摘要 流量质量是衡量数字化营销效果的重要指标之一&#xff0c;它反映了用户对网站或应用的兴趣和满意度。流量质量的常用评估方法有点击率、跳出率和用户停留时间等。本文将介绍如何利用人工智能大模型来分析和优化这些指标&#xff0c;提高流量质量&#xff0c;从而提升数字…

从单体架构到微服务:微服务架构演进与实践

一、单体架构的困境与演进 &#xff08;一&#xff09;单体应用的初始优势与演进路径 在系统发展的初期&#xff0c;单体架构凭借其简单性和开发效率成为首选。单体应用将整个系统的所有功能模块整合在一个项目中&#xff0c;以单一进程的方式运行&#xff0c;特别适合小型系…

Elasticsearch 自定义排序:使用 Painless 脚本实现复杂排序逻辑

需求背景&#xff1a; 从es查询数据出来的时候&#xff0c;要求type为CATALOG的数据排在最前面&#xff0c;也就是目录类型的要放在最前面&#xff0c;而且要求按照层级排序&#xff0c;从L1到L5顺序排序 直接上解法&#xff1a; {//查询条件"query": {"bool…

华为云Flexus+DeepSeek征文|华为云数字人 + DeepSeek:智能交互的革命性突破

目录 前言 关于华为云数字人和云服务 1、华为云数字人 &#xff08;1&#xff09;MetaStudio介绍 &#xff08;2&#xff09;应用场景 &#xff08;3&#xff09;功能特性 &#xff08;4&#xff09;使用体验 2、华为云云服务 华为云数字人结合DeepSeek的核心流程 1、…

【GESP】C++四级练习 luogu-P5729 【深基5.例7】工艺品制作

GESP C四级练习&#xff0c;二维/多维数组练习&#xff0c;难度★★☆☆☆。 题目题解详见&#xff1a;【GESP】C四级练习 luogu-P5729 【深基5.例7】工艺品制作 | OneCoder 【GESP】C四级练习 luogu-P5729 【深基5.例7】工艺品制作 | OneCoderGESP C四级练习&#xff0c;二维…