AI 生成的一个基于springboot后端权限框架

news/2024/12/14 20:47:41/

构建一个完整的基于Spring Boot的权限管理系统是一个庞大的任务,涉及多个模块和复杂的业务逻辑。由于代码量较大,我将提供一个简化版本的框架结构,并详细解释每个部分的关键实现。你可以根据需要进一步扩展和完善这个系统。

项目结构

src
├── main
│   ├── java
│   │   └── com
│   │       └── example
│   │           └── demo
│   │               ├── controller
│   │               │   ├── UserController.java
│   │               │   ├── RoleController.java
│   │               │   ├── OrganizationController.java
│   │               │   ├── MenuController.java
│   │               │   ├── AuthController.java
│   │               │   └── PermissionController.java
│   │               ├── service
│   │               │   ├── UserService.java
│   │               │   ├── RoleService.java
│   │               │   ├── OrganizationService.java
│   │               │   ├── MenuService.java
│   │               │   ├── PermissionService.java
│   │               │   ├── AuthService.java
│   │               │   └── impl
│   │               │       ├── UserServiceImpl.java
│   │               │       ├── RoleServiceImpl.java
│   │               │       ├── OrganizationServiceImpl.java
│   │               │       ├── MenuServiceImpl.java
│   │               │       ├── PermissionServiceImpl.java
│   │               │       └── AuthServiceImpl.java
│   │               ├── mapper
│   │               │   ├── UserMapper.java
│   │               │   ├── RoleMapper.java
│   │               │   ├── OrganizationMapper.java
│   │               │   ├── MenuMapper.java
│   │               │   ├── PermissionMapper.java
│   │               │   └── AuthMapper.java
│   │               ├── config
│   │               │   ├── JwtConfig.java
│   │               │   └── SecurityConfig.java
│   │               └── DemoApplication.java
│   └── resources
│       ├── application.properties
│       ├── mapper
│       │   ├── UserMapper.xml
│       │   ├── RoleMapper.xml
│       │   ├── OrganizationMapper.xml
│       │   ├── MenuMapper.xml
│       │   ├── PermissionMapper.xml
│       │   └── AuthMapper.xml
│       └── static
└── test└── java└── com└── example└── demo└── DemoApplicationTests.java

核心依赖 (pom.xml)

确保你的pom.xml文件中包含以下核心依赖:

<dependencies><!-- Spring Boot Starter Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- MyBatis --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.0</version></dependency><!-- MySQL Connector --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- JWT --><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-api</artifactId><version>0.11.2</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-impl</artifactId><version>0.11.2</version><scope>runtime</scope></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt-jackson</artifactId><version>0.11.2</version><scope>runtime</scope></dependency><!-- Spring Security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- Lombok for less boilerplate code --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
</dependencies>

配置文件 (application.properties)

# MySQL Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# MyBatis Configuration
mybatis.mapper-locations=classpath:mapper/*.xml# JWT Configuration
jwt.secret=yourSecretKey
jwt.expiration=86400 # seconds

java_138">JWT配置 (JwtConfig.java)

java">package com.example.demo.config;import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import java.util.Date;@Component
public class JwtConfig {@Value("${jwt.secret}")private String secret;@Value("${jwt.expiration}")private long expiration;public String generateToken(String username) {return Jwts.builder().setSubject(username).setExpiration(new Date(System.currentTimeMillis() + expiration * 1000)).signWith(SignatureAlgorithm.HS512, secret).compact();}public Claims getClaimsFromToken(String token) {return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();}
}

java_177">安全配置 (SecurityConfig.java)

java">package com.example.demo.config;import com.example.demo.service.impl.AuthServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate AuthServiceImpl authService;@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(authService).passwordEncoder(passwordEncoder());}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/api/auth/**").permitAll().anyRequest().authenticated().and().addFilter(new JwtAuthenticationFilter(authenticationManager())).addFilter(new JwtAuthorizationFilter(authenticationManager()));}@Override@Beanpublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}
}

实体类

java_232">用户实体 (User.java)
java">package com.example.demo.model;import lombok.Data;@Data
public class User {private Long id;private String username;private String password;private String email;private Long roleId;
}
java_249">角色实体 (Role.java)
java">package com.example.demo.model;import lombok.Data;@Data
public class Role {private Long id;private String name;private String description;
}
java_264">组织机构实体 (Organization.java)
java">package com.example.demo.model;import lombok.Data;@Data
public class Organization {private Long id;private String name;private String description;
}
java_279">菜单实体 (Menu.java)
java">package com.example.demo.model;import lombok.Data;@Data
public class Menu {private Long id;private String name;private String url;private Long parentId;
}
java_295">权限实体 (Permission.java)
java">package com.example.demo.model;import lombok.Data;@Data
public class Permission {private Long id;private String name;private String code;
}

Mapper接口及XML配置

为每个实体创建对应的Mapper接口和XML配置文件(如UserMapper.javaUserMapper.xml等),并定义相应的CRUD方法。

java__UserMapperxml_314">示例:UserMapper.java 和 UserMapper.xml
java">package com.example.demo.mapper;import com.example.demo.model.User;
import org.apache.ibatis.annotations.*;@Mapper
public interface UserMapper {@Select("SELECT * FROM users WHERE id = #{id}")User selectById(Long id);// ... other CRUD methods
}
<!-- src/main/resources/mapper/UserMapper.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserMapper"><!-- Additional SQL mappings can be added here if needed -->
</mapper>

Service接口及实现

为每个实体创建服务接口和服务实现类(如UserService.javaUserServiceImpl.java等),并实现业务逻辑。

java_344">示例:UserServiceImpl.java
java">package com.example.demo.service.impl;import com.example.demo.mapper.UserMapper;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic User getUserById(Long id) {return userMapper.selectById(id);}// ... other service methods
}

控制器 (Controllers)

为每个功能模块创建控制器,处理HTTP请求,并调用对应的服务层方法。

java_376">示例:UserController.java
java">package com.example.demo.controller;import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/api/users")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/{id}")public User getUser(@PathVariable Long id) {return userService.getUserById(id);}@GetMappingpublic List<User> getAllUsers() {return userService.getAllUsers();}@PostMappingpublic User createUser(@RequestBody User user) {return userService.createUser(user);}@PutMapping("/{id}")public User updateUser(@PathVariable Long id, @RequestBody User user) {user.setId(id);return userService.updateUser(user);}@DeleteMapping("/{id}")public void deleteUser(@PathVariable Long id) {userService.deleteUser(id);}
}

为了实现权限管理功能,我们将创建PermissionController.javaPermissionService.javaPermissionServiceImpl.javaPermissionMapper.java。这些组件将负责处理权限的创建、读取、更新和删除(CRUD)操作。

java_424">PermissionController.java

这个控制器将处理与权限相关的HTTP请求。它将调用服务层的方法来执行具体的操作。

java">package com.example.demo.controller;import com.example.demo.model.Permission;
import com.example.demo.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@RequestMapping("/api/permissions")
public class PermissionController {@Autowiredprivate PermissionService permissionService;@GetMappingpublic List<Permission> getAllPermissions() {return permissionService.getAllPermissions();}@GetMapping("/{id}")public Permission getPermissionById(@PathVariable Long id) {return permissionService.getPermissionById(id);}@PostMappingpublic Permission createPermission(@RequestBody Permission permission) {return permissionService.createPermission(permission);}@PutMapping("/{id}")public Permission updatePermission(@PathVariable Long id, @RequestBody Permission permissionDetails) {return permissionService.updatePermission(id, permissionDetails);}@DeleteMapping("/{id}")public void deletePermission(@PathVariable Long id) {permissionService.deletePermission(id);}
}

java_472">PermissionService.java

服务接口定义了权限管理所需的所有业务逻辑方法。

java">package com.example.demo.service;import com.example.demo.model.Permission;import java.util.List;public interface PermissionService {List<Permission> getAllPermissions();Permission getPermissionById(Long id);Permission createPermission(Permission permission);Permission updatePermission(Long id, Permission permission);void deletePermission(Long id);
}

java_492">PermissionServiceImpl.java

这是服务接口的具体实现类,包含了权限管理的实际业务逻辑。

java">package com.example.demo.service.impl;import com.example.demo.mapper.PermissionMapper;
import com.example.demo.model.Permission;
import com.example.demo.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class PermissionServiceImpl implements PermissionService {@Autowiredprivate PermissionMapper permissionMapper;@Overridepublic List<Permission> getAllPermissions() {return permissionMapper.selectAllPermissions();}@Overridepublic Permission getPermissionById(Long id) {return permissionMapper.selectPermissionById(id);}@Overridepublic Permission createPermission(Permission permission) {permissionMapper.insertPermission(permission);return permission;}@Overridepublic Permission updatePermission(Long id, Permission permissionDetails) {Permission updatedPermission = permissionMapper.selectPermissionById(id);if (updatedPermission != null) {// 更新权限信息updatedPermission.setName(permissionDetails.getName());updatedPermission.setCode(permissionDetails.getCode());permissionMapper.updatePermission(updatedPermission);}return updatedPermission;}@Overridepublic void deletePermission(Long id) {permissionMapper.deletePermissionById(id);}
}

java_548">PermissionMapper.java

MyBatis Mapper接口用于定义对数据库中权限表的操作。请注意,你还需要为这个接口编写对应的XML映射文件(PermissionMapper.xml),以提供具体的SQL语句。

java">package com.example.demo.mapper;import com.example.demo.model.Permission;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface PermissionMapper {@Select("SELECT * FROM permissions")List<Permission> selectAllPermissions();@Select("SELECT * FROM permissions WHERE id = #{id}")Permission selectPermissionById(Long id);@Insert("INSERT INTO permissions(name, code) VALUES(#{name}, #{code})")@Options(useGeneratedKeys = true, keyProperty = "id")void insertPermission(Permission permission);@Update("UPDATE permissions SET name=#{name}, code=#{code} WHERE id=#{id}")void updatePermission(Permission permission);@Delete("DELETE FROM permissions WHERE id=#{id}")void deletePermissionById(Long id);
}

PermissionMapper.xml

src/main/resources/mapper/PermissionMapper.xml中添加以下内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.PermissionMapper"><!-- You can define additional SQL mappings here if needed --></mapper>

### 数据库表结构你需要在MySQL数据库中创建多个表来存储用户、角色、组织机构、菜单和权限的信息。以下是简化版的SQL语句:```sql
CREATE TABLE users (id BIGINT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(255) NOT NULL UNIQUE,password VARCHAR(255) NOT NULL,email VARCHAR(255),role_id BIGINT
);CREATE TABLE roles (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255) NOT NULL,description TEXT
);CREATE TABLE organizations (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255) NOT NULL,description TEXT
);CREATE TABLE menus (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255) NOT NULL,url VARCHAR(255),parent_id BIGINT
);CREATE TABLE permissions (id BIGINT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(255) NOT NULL,code VARCHAR(255)
);-- 创建关联表
CREATE TABLE user_roles (user_id BIGINT,role_id BIGINT,FOREIGN KEY (user_id) REFERENCES users(id),FOREIGN KEY (role_id) REFERENCES roles(id)
);CREATE TABLE role_permissions (role_id BIGINT,permission_id BIGINT,FOREIGN KEY (role_id) REFERENCES roles(id),FOREIGN KEY (permission_id) REFERENCES permissions(id)
);

java_651">启动类 (DemoApplication.java)

java">package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

Token认证过滤器

java__JwtAuthorizationFilterjava_669">JwtAuthenticationFilter.java 和 JwtAuthorizationFilter.java
java">package com.example.demo.config;import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;public class JwtAuthenticationFilter extends OncePerRequestFilter {@Autowiredprivate JwtConfig jwtConfig;@Autowiredprivate UserDetailsService userDetailsService;@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws ServletException, IOException {try {String token = parseJwt(request);if (token != null && jwtConfig.getClaimsFromToken(token) != null) {Claims claims = jwtConfig.getClaimsFromToken(token);String username = claims.getSubject();UserDetails userDetails = userDetailsService.loadUserByUsername(username);UsernamePasswordAuthenticationToken authentication =new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));SecurityContextHolder.getContext().setAuthentication(authentication);}} catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException e) {logger.error("Invalid JWT token: {}", e.getMessage());response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid JWT token");return;}chain.doFilter(request, response);}private String parseJwt(HttpServletRequest request) {String headerAuth = request.getHeader("Authorization");if (headerAuth != null && headerAuth.startsWith("Bearer ")) {return headerAuth.substring(7);}return null;}
}// JwtAuthorizationFilter would extend OncePerRequestFilter and handle authorization logic.

总结

以上代码片段展示了如何构建一个基于Spring Boot的权限管理系统,包括用户管理、角色管理、组织机构管理、菜单管理和权限管理等功能。通过这些组件,你可以实现对用户的注册、登录、信息修改以及权限分配等操作。此外,系统还支持通过JWT进行Token认证,确保了安全性。

请注意,这只是一个基础框架,实际应用中可能还需要添加更多的特性,如输入验证、异常处理、日志记录等。同时,考虑到系统的复杂性,建议逐步开发各个模块,并进行充分的测试以保证系统的稳定性和可靠性。如果你有更具体的需求或想要进一步细化某些部分,请告诉我!


http://www.ppmy.cn/news/1555107.html

相关文章

前端(模块化)

未使用模块化 定义两个js文件simple1.js和simple2.js let a11; let a11; 两个js文件变量重名 在html测试 传统引入js文件 <script src"./simple1.js"></script> <script src"./simple2.js"></script> 浏览器报错 使用模块…

企业级日志分析系统ELK之ELK概述

ELK 概述 ELK 介绍 什么是 ELK 早期IT架构中的系统和应用的日志分散在不同的主机和文件&#xff0c;如果应用出现问题&#xff0c;开发和运维人员想排 查原因&#xff0c;就要先找到相应的主机上的日志文件再进行查找和分析&#xff0c;所以非常不方便&#xff0c;而且还涉及…

【HarmonyOS】使用AVPlayer播放音乐,导致系统其它应用音乐播放暂停 - 播放音频焦点管理

【HarmonyOS】使用AVPlayer播放音乐&#xff0c;导致系统其它应用音乐播放暂停 - 播放音频焦点管理 一、前言 在鸿蒙系统中&#xff0c;对于音乐播放分为几种场景。音乐&#xff0c;电影&#xff0c;音效&#xff0c;闹钟等。当使用AVPlayer播放音乐时&#xff0c;如果不处理…

利用Python爬虫MinC根据ID取商品详情

在电商数据分析和市场研究中&#xff0c;获取商品详情信息是至关重要的一步。本文将详细介绍如何利用Python编写爬虫程序&#xff0c;通过MinC平台的API根据商品ID获取商品详情。我们将提供完整的代码示例&#xff0c;帮助你快速上手。 一、准备工作 在开始之前&#xff0c;我…

怎么自己创建一个网站? 开发语言首选 java,使用CMS网站内容管理系统是不错的选择

怎么自己创建一个网站 推荐使用 Java CMS 网站内容管理系统&#xff0c;根据网站规划的功能模块&#xff0c;创建不同的页面风格&#xff1b; 文章目录 怎么自己创建一个网站一、规划网站1.1确定网站主题和目的1.2规划网站结构和内容 二、注册域名2.1选择域名注册商2.2 查找并…

【第六节】docker应用系列篇: 使用Dockerfile构建nginx

系列文章目录 【第六节】docker应用系列篇&#xff1a; 使用Dockerfile构建nginx 系列文章目录前言一、 准备dockerfile1. 简单dockerFile1.2 解决网络问题的dockerfile 2. 构建结果&#xff1a;3. 构建过程&#xff1a; 前言 提示&#xff1a;以下是本篇文章正文内容&#xff…

SQLServer到MySQL的数据高效迁移方案分享

SQL Server数据集成到MySQL的技术案例分享 在企业级数据管理中&#xff0c;跨平台的数据集成是一个常见且关键的任务。本次我们将探讨如何通过轻易云数据集成平台&#xff0c;将巨益OMS系统中的退款单明细表从SQL Server高效、安全地迁移到MySQL数据库中。具体方案名称为“7--…

如何在 ASP.NET Core 3.1 应用程序中使用 Log4Net

介绍 日志记录是应用程序的核心。它对于调试和故障排除以及应用程序的流畅性非常重要。 借助日志记录&#xff0c;我们可以对本地系统进行端到端的可视性&#xff0c;而对于基于云的系统&#xff0c;我们只能提供一小部分可视性。您可以将日志写入磁盘或数据库中的文件&#xf…