# coco-rush-server **Repository Path**: wanneme/coco-rush-server ## Basic Information - **Project Name**: coco-rush-server - **Description**: CocoRush项目的拆分 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-11-04 - **Last Updated**: 2025-07-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 简介 学习和记录 `SpringBoot` 相关知识的单体后端应用项目。 ## 要点 ### 基础框架:`Springboot` + `mybatis-plus` 使用 `SpringBoot-2.6.6` 做基础框架, `myabtis-plus-3.5.1` 做 `orm` 。 `mybatis-plus` 的配置中使用 `id` 生成使用雪花算法,详见 `cc.wanforme.st.server.config.MybatisIdentifierConfiguration` #### mybatis-plus 别名问题 `mybatis-plus` 开启了实体类别名配置 `mybatis-plus.type-aliases-package` ,多个包使用 `,` 分隔。 但是打包后会出现别名无法识别, **问题详情:** [MyBatis无法扫描Spring Boot别名的Bug](https://blog.csdn.net/taotoxht/article/details/70171542) **一种解决方式:** [于此项目无效](https://blog.csdn.net/u012954706/article/details/103977335) ,但可以学习,猜测是因为数据源非自动装配 **项目内解决方式:** 配置并实例化 `sqlSessionFactory` 中,在设置自动扫描 `Mapper` 前,添加如下代码 ``` java // mybatis-plus sqlSessionFactory.setVfs(com.baomidou.mybatisplus.autoconfigure.SpringBootVFS.class); // mybatis //sqlSessionFactory.setVfs(org.mybatis.spring.boot.autoconfigure.SpringBootVFS.class); // 如果上面的方式没有效果,那么请使用下面的代码 // mybatis-plus VFS.USER_IMPLEMENTATIONS.add(com.baomidou.mybatisplus.autoconfigure.SpringBootVFS.class); // mybatis //VFS.USER_IMPLEMENTATIONS.add(org.mybatis.spring.boot.autoconfigure.SpringBootVFS.class); ``` 在单数据源和多数据源的配置类中,你都能找到这段代码。
### 多数据源配置 多数据源配置位于 `cc.wanforme.st.server.config.db.dynamic` 包下,目前多数据源默认为 `mysql` 和 `sqlite` (`sqlite` 没有使用)。 多数据源的配置从某大佬那里学来的,但是不记得是谁了..... #### 增删改多数据源 以增加为例: 1. `cc.wanforme.st.server.config.db.dynamic.DynamicDatabaseConfiguration.multipleDataSource(DataSource, DataSource)` 方法中的 `targetDataSources` 内添加新的数据源。 2. 添加时需要用的枚举类 `TableDbEnum` ,所以你要在 `cc.wanforme.st.server.config.db.dynamic.TableDbEnum` 中定义新的数据源。 3. 通过 `@RouteDB` 注解指定数据源,使用的方式如下: ``` java @RequestMapping("/3") @PreAuthorize("hasRole('USER')") @RouteDB(TableDbEnum.SQLITE) public String hello3() { return "hello 3"; } ``` 多数据源的配置为 `spring.datasource.druid.default` 和 `spring.datasource.druid.sqlite`
#### 不使用多数据源 设置配置 `spring.datasource.enable-dynamic=false` (默认为 `true`)即可使用单数据源。多数据源中只有一个数据源也是可以的。**注:此条配置为自定义,非官方配置** 单数据源的配置为 `spring.datasource.druid.default`,配置类 `cc.wanforme.st.server.config.db.DatabaseConfiguration`
### 整合 `redis` 开启 `SpringCache` 缓存,并使用 `reids` 作为缓存数据库。 配置类: `cc.wanforme.st.server.config.RedisConfig` 。`@EnableCaching` 用于开启 `SpringCache` 缓存。不使用 `SpringCache` 则可以删掉注释。 缓存使用参考: `cc.wanforme.st.server.base.service.impl.RolePermissionServiceImpl#selectPermissions` #### redis 缓存 key 前缀 这里分为两个部分,一部分是整合进 `SpringCache` 的操作,另一部分是配置 `RedisConfig` 中实例化出来手动操作 `redis` 的实例 `RedisTemplate` 。 前缀配置: `spring.cache.redis.key-prefix` 。这条配置是 `SpringCache` 整合 `redis` 时的前缀配置,为了保证统一,所以 `RedisTemplate` 实例也使用了这条配置。 `RedisTemplate` 的 `KeySerializer` 为自定义,详见:`cc.wanforme.st.server.config.RedisConfig.PreffixStringRedisSerializer`
### `log4j2` 日志 没什么说的,去掉 `SpringBoot` 内置日志,重新引入 `slf4j` 和 `log4j2` 。
### `SpringSecurity` 授权认证 使用 `SpringSecurity` 认证,重写授权部分。 配置类:`cc.wanforme.st.server.config.SpringSecurityConfiguration` 。 设置权限:`cc.wanforme.st.server.authen.service.AuthService#setAuthentications` 配置中 `http.antMatchers("/**").permitAll()` 表示接口默认不需要认证。需要认证的接口则使用注解 `@PreAuthorize("hasAuthority('xxx')")` 或 `@PreAuthorize("hasRole('xxx')")` 控制。 使用参考 `cc.wanforme.st.server.base.controller.SimpleLoginController` 后面的方法。
认证时,首先获取请求头 `Authorization` 中的 `token` ,如果请求头中没有`token` ,再去寻找 `cookie` 中的 `token` (后端里 `cookie` 名可以配置), 根据 `token` 获取当前用户的权限和角色,详见过滤器 `cc.wanforme.st.server.authen.filter.MRememberMeFilter`。 从 `MRememberMeFilter` 中可以分析出 ,用户登录后,可以调用 `Spring` 的方法获取当前用户信息 (没有密码) ```java SecurityContextHolder.getContext().getAuthentication().getPrincipal() ``` 获取请求中的 `token`:静态方法 `cc.wanforme.st.server.authen.filter.MRememberMeFilter#getTokenFromHeader` 获取当前登录用户示例: `cc.wanforme.st.server.biz.service.LoginService#currentUser` **注:授权认证采用的是粒度最细的权限判断(只能使用 `@PreAuthorize("hasAuthority('xxx')")`)的方式,如需支持 `@PreAuthorize("hasRole('xxx')")` ,在授权时进行修改**
### 全局异常处理和断言检查 全局异常配置类: `cc.wanforme.st.server.config.GlobalExceptionHandler` 自定义运行时异常: `cc.wanforme.st.server.exception.MunkRuntimeException` 断言检查工具: `cc.wanforme.st.server.util.AssertCheck` 断言检查会将对不通过的检查抛出 `MunkRuntimeException` ,全局异常处理会处理此异常。因此可以酌情添加常用的检查。断言检查工具参考的是 `Spring` 自带的断言检查器 `org.springframework.util.Assert` 。