From ccedf0ab5c9f1be014069a081f0ff70e29edeea7 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Thu, 30 Jun 2022 16:28:28 +0800 Subject: [PATCH 01/13] =?UTF-8?q?fix:=20:bug:=E4=BF=AE=E6=94=B9mapper=20xm?= =?UTF-8?q?l=E7=9A=84=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/resources/templates/mapper.xml.vm | 37 ++----------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/ym-generator/src/main/resources/templates/mapper.xml.vm b/ym-generator/src/main/resources/templates/mapper.xml.vm index b0909e8..49be86c 100644 --- a/ym-generator/src/main/resources/templates/mapper.xml.vm +++ b/ym-generator/src/main/resources/templates/mapper.xml.vm @@ -1,39 +1,10 @@ - -#if(${enableCache}) - - - -#end -#if(${baseResultMap}) - - -#foreach($field in ${table.fields}) -#if(${field.keyFlag})##生成主键排在第一位 - -#end -#end -#foreach($field in ${table.commonFields})##生成公共字段 - -#end -#foreach($field in ${table.fields}) -#if(!${field.keyFlag})##生成普通字段 - -#end -#end + + #foreach($field in ${table.fields}) + + #end -#end -#if(${baseColumnList}) - - -#foreach($field in ${table.commonFields}) - ${field.columnName}, -#end - ${table.fieldNames} - - -#end From 1f85905317fc2ae0f6b62e03589c6f794af2bf64 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Fri, 1 Jul 2022 14:49:50 +0800 Subject: [PATCH 02/13] =?UTF-8?q?feat:=20:memo:=E6=96=B0=E5=A2=9EREADME?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..185a4bd --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +**项目说明** +- 这是根据renren框架重新修改出来的框架 +- 采用SpringBoot、MyBatis、spring security框架 +- 提供了代码生成器,只需编写30%左右代码,其余的代码交给系统自动生成,可快速完成开发任务 +- 代码生成器是用的Velocity模版引擎,如果需要可以在ym-generator模块中进行vm模板的修改和一些包名等基础修改 +
+ +**新增业务模块需要注意** +- 新增业务模块时,业务模块需要依赖ym-admin,并且ym-gateway模块maven需要依赖新建模块 +- 自动生成的controller接口都带鉴权注解,如果开发阶段可以先注释掉,后面开发完成之后再把鉴权添加到数据库 +
+ +**技术选型:** +- 核心框架:Spring Boot 2.7 +- 安全框架:Spring Security +- 持久层框架:MyBatis 3.5 +- 定时器:Quartz 2.3 +- 数据库连接池:HikariCP +- 日志管理:log4j2 +- 页面交互:Vue2.x + +
+ +**软件需求** +- JDK1.8 +- Maven3.0+ +- MySQL8.0 +- redis6.0+ +
\ No newline at end of file From cd1d203d875aab0400c546f8441812ecec88a5b8 Mon Sep 17 00:00:00 2001 From: Lin-XCC <754310653@QQ.COM> Date: Tue, 5 Jul 2022 10:44:09 +0800 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20=E9=80=BB=E8=BE=91=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 逻辑删除实现,修改了生成器和yml --- ym-gateway/src/main/resources/application.yml | 4 ++++ .../cnbm/generator/build/CodeGenerator.java | 18 +++++------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/ym-gateway/src/main/resources/application.yml b/ym-gateway/src/main/resources/application.yml index 3d651ce..d3ab12d 100644 --- a/ym-gateway/src/main/resources/application.yml +++ b/ym-gateway/src/main/resources/application.yml @@ -58,6 +58,10 @@ mybatis-plus: db-config: #主键类型 id-type: ASSIGN_ID + #逻辑删除配置 + logic-delete-value: 0 + logic-not-delete-value: 1 + logic-delete-field: valid banner: false #原生配置 configuration: diff --git a/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java b/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java index a133634..1b2682c 100644 --- a/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java +++ b/ym-generator/src/main/java/com/cnbm/generator/build/CodeGenerator.java @@ -1,23 +1,13 @@ package com.cnbm.generator.build; import com.baomidou.mybatisplus.generator.FastAutoGenerator; -import com.baomidou.mybatisplus.generator.config.*; -import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder; -import com.baomidou.mybatisplus.generator.config.po.TableInfo; -import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; -import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine; -import com.baomidou.mybatisplus.generator.function.ConverterFileName; import com.cnbm.generator.config.DataConfig; import com.cnbm.generator.engine.EnhanceVelocityTemplateEngine; -import com.cnbm.generator.utils.IdWorker; -import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.io.File; -import java.util.*; -import java.util.function.BiConsumer; - -import static com.baomidou.mybatisplus.generator.config.ConstVal.MODULE_NAME; +import java.util.HashMap; +import java.util.Map; /** * @Author weihongyang @@ -63,7 +53,9 @@ public class CodeGenerator { //设置需要生成的表名 builder.addInclude(include) //设置过滤表前缀 - .addTablePrefix("sys_"); + .addTablePrefix("sys_") + //添加逻辑删除列 + .entityBuilder().logicDeleteColumnName("valid").build(); }) .injectionConfig(consumer -> { From f154d361a82d2597b0fb7afbc76feb0ca69eb5a6 Mon Sep 17 00:00:00 2001 From: Lin-XCC <754310653@QQ.COM> Date: Tue, 5 Jul 2022 10:45:06 +0800 Subject: [PATCH 04/13] =?UTF-8?q?feat:=20=E5=9F=BA=E7=A1=80=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增和修改时快速设置通用列 --- .../com/cnbm/admin/enums/WhetherEnum.java | 52 +++++++ .../cnbm/admin/utils/BaseSupportUtils.java | 136 ++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 ym-admin/src/main/java/com/cnbm/admin/enums/WhetherEnum.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/utils/BaseSupportUtils.java diff --git a/ym-admin/src/main/java/com/cnbm/admin/enums/WhetherEnum.java b/ym-admin/src/main/java/com/cnbm/admin/enums/WhetherEnum.java new file mode 100644 index 0000000..b1b2fc3 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/enums/WhetherEnum.java @@ -0,0 +1,52 @@ +package com.cnbm.admin.enums; + +/** + *

+ * 是、否有效枚举 + *

+ * + * @author xcc + * @date 2022年7月5日 + * @since 1.0 + */ +public enum WhetherEnum { + + NO(0, "否"), + + YES(1, "是"); + + private final Integer value; + private final String label; + private final String remark; + + WhetherEnum(final int value, final String label) { + this(value, label, null); + } + + WhetherEnum(final int value, final String label, final String remark) { + this.value = value; + this.label = label; + this.remark = remark; + } + + /** + * @return 数据值 + */ + public Integer getValue() { + return value; + } + + /** + * @return 标签名 + */ + public String getLabel() { + return label; + } + + /** + * @return 备注 + */ + public String getRemark() { + return remark; + } +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/utils/BaseSupportUtils.java b/ym-admin/src/main/java/com/cnbm/admin/utils/BaseSupportUtils.java new file mode 100644 index 0000000..4f86c50 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/utils/BaseSupportUtils.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2018. + * http://www.ulabcare.com + */ + +package com.cnbm.admin.utils; + +import com.cnbm.admin.entity.LoginUser; +import com.cnbm.admin.entity.SysUserEntity; +import com.cnbm.admin.enums.WhetherEnum; +import lombok.Builder; +import lombok.Data; +import org.springframework.beans.BeanUtils; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * 接口支持基类 + * + * @author jiff + * @date 2018/11/1 + * @since 1.0 + */ +public abstract class BaseSupportUtils { + + /** + * 获取当前登录用户信息 + * + * @return + */ + private static SysUserEntity getLoginUser() { + //登录用户信息 + UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + LoginUser loginUser = (LoginUser) authentication.getPrincipal(); + return loginUser.getSysUserEntity(); + } + + /** + * 设置公共字段值,一般用于创建新记录,包含以下字段: + * + *

+ * {@link CommonField#enabled}
+ * {@link CommonField#valid}
+ * {@link CommonField#creatorId}
+ * {@link CommonField#creatorName}
+ * {@link CommonField#createTime}
+ * {@link CommonField#updaterId}
+ * {@link CommonField#updaterName}
+ * {@link CommonField#updateTime}
+ *

+ * + * @param t 需要设置的对象 + * @param ignoreProperties 忽略的字段 + * @param + */ + public static T setCommonField(T t, String... ignoreProperties) { + CommonField commonField = CommonField.builder() + .enabled(WhetherEnum.YES.getValue()) + .valid(WhetherEnum.YES.getValue()) + .createTime(LocalDateTime.now()) + .creatorId(getLoginUser().getId()) + .creatorName(getLoginUser().getUsername()) + .updateTime(LocalDateTime.now()) + .updaterId(getLoginUser().getId()) + .updaterName(getLoginUser().getUsername()) + .build(); + BeanUtils.copyProperties(commonField, t, ignoreProperties); + return t; + } + + /** + * 设置更新的公共字段值,一般用于更新记录,包含以下字段: + * + *

+ * {@link CommonField#updaterId}
+ * {@link CommonField#updaterName}
+ * {@link CommonField#updateTime}
+ *

+ * + * @param t 需要设置的对象 + * @param + */ + public static T setUpdateCommonField(T t) { + CommonField commonField = CommonField.builder() + .updaterId(getLoginUser().getId()) + .updaterName(getLoginUser().getUsername()) + .updateTime(LocalDateTime.now()) + .build(); + BeanUtils.copyProperties(commonField, t, "enabled", "valid"); + return t; + } + + @Data + @Builder + private static class CommonField implements Serializable { + /** + * 启用状态:0 、停用,1、启用 + */ + private Integer enabled; + /** + * 删除标志,是否有效:1 可用 0不可用 + */ + private Integer valid; + + + /** + * 创建人 + */ + private Long creatorId; + /** + * 创建人 + */ + private String creatorName; + /** + * 创建时间 + */ + private LocalDateTime createTime; + + + /** + * 更新人 + */ + private Long updaterId; + /** + * 更新人 + */ + private String updaterName; + /** + * 更新时间 + */ + private LocalDateTime updateTime; + } +} From d3e337d622b826aabef98246f8aea13d9d0302da Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Tue, 5 Jul 2022 15:25:50 +0800 Subject: [PATCH 05/13] =?UTF-8?q?fix:=20:art:=E4=BF=AE=E6=94=B9controller?= =?UTF-8?q?=E7=9A=84vm=E6=A8=A1=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/templates/controller.java.vm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ym-generator/src/main/resources/templates/controller.java.vm b/ym-generator/src/main/resources/templates/controller.java.vm index fc2f2b1..ebd19b7 100644 --- a/ym-generator/src/main/resources/templates/controller.java.vm +++ b/ym-generator/src/main/resources/templates/controller.java.vm @@ -68,26 +68,26 @@ public class ${table.controllerName} { @ApiOperation("保存") @LogOperation("保存") @PreAuthorize("@ex.hasAuthority('${package.ModuleName}:${table.entityPath}:save')") - public Result save(@RequestBody ${entity}DTO dto){ + public Result save(@RequestBody ${entity}DTO dto){ //效验数据 ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class); ${table.entityPath}Service.save(dto); - return new Result(); + return new Result().ok(dto.getId()); } @PutMapping @ApiOperation("修改") @LogOperation("修改") @PreAuthorize("@ex.hasAuthority('${package.ModuleName}:${table.entityPath}:update')") - public Result update(@RequestBody ${entity}DTO dto){ + public Result update(@RequestBody ${entity}DTO dto){ //效验数据 ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class); ${table.entityPath}Service.update(dto); - return new Result(); + return new Result().ok(dto.getId()); } @DeleteMapping From e9a39ae11bda0beeeefc7ac7f5b4f127533023f1 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Tue, 5 Jul 2022 16:05:03 +0800 Subject: [PATCH 06/13] =?UTF-8?q?fix:=20:bug:=E4=BF=AE=E6=94=B9=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E6=95=B0=E6=8D=AEcontroller=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/cnbm/admin/controller/SysDictDataController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ym-admin/src/main/java/com/cnbm/admin/controller/SysDictDataController.java b/ym-admin/src/main/java/com/cnbm/admin/controller/SysDictDataController.java index 982d63d..3f5fce7 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/controller/SysDictDataController.java +++ b/ym-admin/src/main/java/com/cnbm/admin/controller/SysDictDataController.java @@ -40,10 +40,11 @@ public class SysDictDataController { @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataTypeClass=Integer.class) , @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataTypeClass=String.class) , @ApiImplicitParam(name = Constant.ORDER, value = "排序方式,可选值(asc、desc)", paramType = "query", dataTypeClass=String.class) , + @ApiImplicitParam(name = "dictTypeId", value = "字典类型id", paramType = "query", dataTypeClass = String.class), @ApiImplicitParam(name = "dictLabel", value = "字典标签", paramType = "query", dataTypeClass=String.class), @ApiImplicitParam(name = "dictValue", value = "字典值", paramType = "query", dataTypeClass=String.class) }) - @PreAuthorize("@ex.hasAuthority('sys:dept:page')") + @PreAuthorize("@ex.hasAuthority('sys:dict:page')") public Result> page(@ApiIgnore @RequestParam Map params){ //字典类型 PageData page = sysDictDataService.page(params); From 0f239f7978d894010a7f346ef60bc4b1a2255b96 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Tue, 5 Jul 2022 16:16:31 +0800 Subject: [PATCH 07/13] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0db=E7=9A=84mysq?= =?UTF-8?q?l=E6=96=87=E4=BB=B6=E7=9A=84=E9=87=8C=E9=9D=A2=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E5=88=A0=E9=99=A4=E6=A0=87=E8=AF=86=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ym-admin/db/mysql.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ym-admin/db/mysql.sql b/ym-admin/db/mysql.sql index c326a9d..627f6cd 100644 --- a/ym-admin/db/mysql.sql +++ b/ym-admin/db/mysql.sql @@ -15,6 +15,7 @@ CREATE TABLE sys_user ( create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), unique key uk_username (username), key idx_create_date (create_date) @@ -31,6 +32,7 @@ CREATE TABLE sys_dept ( create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_pid (pid), key idx_sort (sort) @@ -47,6 +49,7 @@ create table sys_role create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_dept_id (dept_id) )ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='角色管理'; @@ -66,6 +69,7 @@ create table sys_menu create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_pid (pid), key idx_sort (sort) @@ -79,6 +83,7 @@ create table sys_role_user user_id bigint COMMENT '用户ID', creator bigint COMMENT '创建者', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_role_id (role_id), key idx_user_id (user_id) @@ -92,6 +97,7 @@ create table sys_role_menu menu_id bigint COMMENT '菜单ID', creator bigint COMMENT '创建者', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_role_id (role_id), key idx_menu_id (menu_id) @@ -105,6 +111,7 @@ create table sys_role_data_scope dept_id bigint COMMENT '部门ID', creator bigint COMMENT '创建者', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_role_id (role_id) )ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='角色数据权限'; @@ -121,6 +128,7 @@ create table sys_params create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), unique key uk_param_code (param_code), key idx_create_date (create_date) @@ -138,6 +146,7 @@ create table sys_dict_type create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), UNIQUE KEY(dict_type) )ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='字典类型'; @@ -155,6 +164,7 @@ create table sys_dict_data create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), unique key uk_dict_type_value (dict_type_id, dict_value), key idx_sort (sort) @@ -171,6 +181,7 @@ create table sys_log_login creator_name varchar(50) COMMENT '用户名', creator bigint COMMENT '创建者', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_status (status), key idx_create_date (create_date) @@ -191,6 +202,7 @@ create table sys_log_operation creator_name varchar(50) COMMENT '用户名', creator bigint COMMENT '创建者', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_create_date (create_date) )ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='操作日志'; @@ -207,6 +219,7 @@ create table sys_log_error error_info text COMMENT '异常信息', creator bigint COMMENT '创建者', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', primary key (id), key idx_create_date (create_date) )ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='异常日志'; @@ -218,6 +231,7 @@ CREATE TABLE sys_oss ( url varchar(200) COMMENT 'URL地址', creator bigint COMMENT '创建者', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', PRIMARY KEY (id), key idx_create_date (create_date) ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='文件上传'; @@ -234,6 +248,7 @@ CREATE TABLE schedule_job ( create_date datetime COMMENT '创建时间', updater bigint COMMENT '更新者', update_date datetime COMMENT '更新时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', PRIMARY KEY (id), key idx_create_date (create_date) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='定时任务'; @@ -248,6 +263,7 @@ CREATE TABLE schedule_job_log ( error varchar(2000) DEFAULT NULL COMMENT '失败信息', times int NOT NULL COMMENT '耗时(单位:毫秒)', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', PRIMARY KEY (id), key idx_job_id (job_id), key idx_create_date (create_date) @@ -261,6 +277,7 @@ CREATE TABLE sys_user_token ( expire_date datetime COMMENT '过期时间', update_date datetime COMMENT '更新时间', create_date datetime COMMENT '创建时间', + valid tinyint COMMENT '删除标识,是否有效:1可用 0不可用', PRIMARY KEY (id), UNIQUE KEY user_id (user_id), UNIQUE KEY token (token) From 3a91f8b8fe7ba15bde64e2819dbe2a7af8387727 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Mon, 25 Jul 2022 16:57:49 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E2=9C=85=20vscode=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/cnbm/admin/controller/LoginController.java | 1 + 1 file changed, 1 insertion(+) diff --git a/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java b/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java index 85abb6a..029970c 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java +++ b/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java @@ -60,5 +60,6 @@ public class LoginController { @ApiOperation(value = "退出") public void logout(){ } + } From c0f460e1e466c33f451ee239631485ba6162ddf1 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Mon, 8 Aug 2022 09:53:41 +0800 Subject: [PATCH 09/13] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=97=A5=E5=BF=97=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SysLogLoginController.java | 65 +++++++++++++++++++ .../com/cnbm/admin/dao/SysLogLoginDao.java | 10 +++ .../com/cnbm/admin/dto/SysLogLoginDTO.java | 36 ++++++++++ .../cnbm/admin/entity/SysLogLoginEntity.java | 35 ++++++++++ .../cnbm/admin/execl/SysLogLoginExcel.java | 24 +++++++ .../admin/service/SysLogLoginService.java | 18 +++++ .../service/impl/SysLogLoginServiceImpl.java | 57 ++++++++++++++++ .../main/resources/mapper/SysLogLoginDao.xml | 6 ++ 8 files changed, 251 insertions(+) create mode 100644 ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/dao/SysLogLoginDao.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/dto/SysLogLoginDTO.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/entity/SysLogLoginEntity.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/execl/SysLogLoginExcel.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/service/SysLogLoginService.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java create mode 100644 ym-admin/src/main/resources/mapper/SysLogLoginDao.xml diff --git a/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java b/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java new file mode 100644 index 0000000..95cf2a1 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java @@ -0,0 +1,65 @@ +package com.cnbm.admin.controller; + +import com.cnbm.admin.annotation.LogOperation; +import com.cnbm.admin.dto.SysLogLoginDTO; +import com.cnbm.admin.execl.SysLogLoginExcel; +import com.cnbm.admin.service.SysLogLoginService; +import com.cnbm.common.constant.Constant; +import com.cnbm.common.page.PageData; +import com.cnbm.common.utils.ExcelUtils; +import com.cnbm.common.utils.Result; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import springfox.documentation.annotations.ApiIgnore; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("sys/log/login") +@Api(tags="登录日志") +public class SysLogLoginController { + @Autowired + private SysLogLoginService sysLogLoginService; + + @GetMapping("page") + @ApiOperation("分页") + @ApiImplicitParams({ + @ApiImplicitParam(name = Constant.PAGE, value = "当前页码,从1开始", paramType = "query", required = true, dataTypeClass=Integer.class) , + @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataTypeClass=Integer.class) , + @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataTypeClass=String.class) , + @ApiImplicitParam(name = Constant.ORDER, value = "排序方式,可选值(asc、desc)", paramType = "query", dataTypeClass=String.class) , + @ApiImplicitParam(name = "status", value = "状态 0:失败 1:成功 2:账号已锁定", paramType = "query", dataTypeClass=Integer.class), + @ApiImplicitParam(name = "creatorName", value = "用户名", paramType = "query", dataTypeClass=String.class) + }) + @PreAuthorize("@ex.hasAuthority('sys:log:login')") + public Result> page(@ApiIgnore @RequestParam Map params){ + PageData page = sysLogLoginService.page(params); + + return new Result>().ok(page); + } + + @GetMapping("export") + @ApiOperation("导出") + @LogOperation("导出") + @ApiImplicitParams({ + @ApiImplicitParam(name = "status", value = "状态 0:失败 1:成功 2:账号已锁定", paramType = "query", dataTypeClass=Integer.class), + @ApiImplicitParam(name = "creatorName", value = "用户名", paramType = "query", dataTypeClass=String.class) + }) + @PreAuthorize("@ex.hasAuthority('sys:log:login')") + public void export(@ApiIgnore @RequestParam Map params, HttpServletResponse response) throws Exception { + List list = sysLogLoginService.list(params); + + ExcelUtils.exportExcelToTarget(response, null, list, SysLogLoginExcel.class); + } + +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/dao/SysLogLoginDao.java b/ym-admin/src/main/java/com/cnbm/admin/dao/SysLogLoginDao.java new file mode 100644 index 0000000..c4a9ddb --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/dao/SysLogLoginDao.java @@ -0,0 +1,10 @@ +package com.cnbm.admin.dao; + +import com.cnbm.admin.entity.SysLogLoginEntity; +import com.cnbm.common.dao.BaseDao; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface SysLogLoginDao extends BaseDao { + +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/dto/SysLogLoginDTO.java b/ym-admin/src/main/java/com/cnbm/admin/dto/SysLogLoginDTO.java new file mode 100644 index 0000000..c52d0c4 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/dto/SysLogLoginDTO.java @@ -0,0 +1,36 @@ +package com.cnbm.admin.dto; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +@Data +@ApiModel(value = "登录日志") +public class SysLogLoginDTO implements Serializable { + private static final long serialVersionUID = 1L; + + @ApiModelProperty(value = "id") + private Long id; + + @ApiModelProperty(value = "用户操作 0:用户登录 1:用户退出") + private Integer operation; + + @ApiModelProperty(value = "状态 0:失败 1:成功 2:账号已锁定") + private Integer status; + + @ApiModelProperty(value = "用户代理") + private String userAgent; + + @ApiModelProperty(value = "操作IP") + private String ip; + + @ApiModelProperty(value = "用户名") + private String creatorName; + + @ApiModelProperty(value = "创建时间") + private Date createDate; + +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/entity/SysLogLoginEntity.java b/ym-admin/src/main/java/com/cnbm/admin/entity/SysLogLoginEntity.java new file mode 100644 index 0000000..45b6848 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/entity/SysLogLoginEntity.java @@ -0,0 +1,35 @@ +package com.cnbm.admin.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.cnbm.common.entity.BaseEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper=false) +@TableName("sys_log_login") +public class SysLogLoginEntity extends BaseEntity { + private static final long serialVersionUID = 1L; + + /** + * 用户操作 0:用户登录 1:用户退出 + */ + private Integer operation; + /** + * 状态 0:失败 1:成功 2:账号已锁定 + */ + private Integer status; + /** + * 用户代理 + */ + private String userAgent; + /** + * 操作IP + */ + private String ip; + /** + * 用户名 + */ + private String creatorName; + +} \ No newline at end of file diff --git a/ym-admin/src/main/java/com/cnbm/admin/execl/SysLogLoginExcel.java b/ym-admin/src/main/java/com/cnbm/admin/execl/SysLogLoginExcel.java new file mode 100644 index 0000000..8230aa2 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/execl/SysLogLoginExcel.java @@ -0,0 +1,24 @@ +package com.cnbm.admin.execl; + +import cn.afterturn.easypoi.excel.annotation.Excel; +import lombok.Data; + +import java.util.Date; + +@Data +public class SysLogLoginExcel { + @Excel(name = "用户操作") + private String operation; + @Excel(name = "状态", replace = {"失败_0", "成功_1", "账号已锁定_1"}) + private Integer status; + @Excel(name = "User-Agent") + private String userAgent; + @Excel(name = "操作IP") + private String ip; + @Excel(name = "用户名") + private String creatorName; + @Excel(name = "创建时间", format = "yyyy-MM-dd HH:mm:ss") + private Date createDate; + +} + diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/SysLogLoginService.java b/ym-admin/src/main/java/com/cnbm/admin/service/SysLogLoginService.java new file mode 100644 index 0000000..2fec755 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/service/SysLogLoginService.java @@ -0,0 +1,18 @@ +package com.cnbm.admin.service; + +import com.cnbm.admin.dto.SysLogLoginDTO; +import com.cnbm.admin.entity.SysLogLoginEntity; +import com.cnbm.common.page.PageData; +import com.cnbm.common.service.BaseService; + +import java.util.List; +import java.util.Map; + +public interface SysLogLoginService extends BaseService { + + PageData page(Map params); + + List list(Map params); + + void save(SysLogLoginEntity entity); +} \ No newline at end of file diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java new file mode 100644 index 0000000..21db772 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java @@ -0,0 +1,57 @@ +package com.cnbm.admin.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.cnbm.admin.dao.SysLogLoginDao; +import com.cnbm.admin.dto.SysLogLoginDTO; +import com.cnbm.admin.entity.SysLogLoginEntity; +import com.cnbm.admin.service.SysLogLoginService; +import com.cnbm.common.constant.Constant; +import com.cnbm.common.page.PageData; +import com.cnbm.common.service.impl.BaseServiceImpl; +import com.cnbm.common.utils.ConvertUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Map; + +@Service +public class SysLogLoginServiceImpl extends BaseServiceImpl implements SysLogLoginService { + + @Override + public PageData page(Map params) { + IPage page = baseDao.selectPage( + getPage(params, Constant.CREATE_DATE, false), + getWrapper(params) + ); + + return getPageData(page, SysLogLoginDTO.class); + } + + @Override + public List list(Map params) { + List entityList = baseDao.selectList(getWrapper(params)); + + return ConvertUtils.sourceToTarget(entityList, SysLogLoginDTO.class); + } + + private QueryWrapper getWrapper(Map params){ + String status = (String) params.get("status"); + String creatorName = (String) params.get("creatorName"); + + QueryWrapper wrapper = new QueryWrapper<>(); + wrapper.eq(StringUtils.isNotBlank(status), "status", status); + wrapper.like(StringUtils.isNotBlank(creatorName), "creator_name", creatorName); + + return wrapper; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void save(SysLogLoginEntity entity) { + insert(entity); + } + +} diff --git a/ym-admin/src/main/resources/mapper/SysLogLoginDao.xml b/ym-admin/src/main/resources/mapper/SysLogLoginDao.xml new file mode 100644 index 0000000..587044e --- /dev/null +++ b/ym-admin/src/main/resources/mapper/SysLogLoginDao.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file From 16e656d8202bfd946b48d51812e1f2e444c4058f Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Mon, 8 Aug 2022 10:01:52 +0800 Subject: [PATCH 10/13] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=97=A5=E5=BF=97=E7=9A=84=E6=93=8D=E4=BD=9C=E4=BA=BA?= =?UTF-8?q?=E5=92=8C=E6=93=8D=E4=BD=9C=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/cnbm/admin/aspect/LogOperationAspect.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ym-admin/src/main/java/com/cnbm/admin/aspect/LogOperationAspect.java b/ym-admin/src/main/java/com/cnbm/admin/aspect/LogOperationAspect.java index 619c43f..e55c8c5 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/aspect/LogOperationAspect.java +++ b/ym-admin/src/main/java/com/cnbm/admin/aspect/LogOperationAspect.java @@ -21,6 +21,7 @@ import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; +import java.util.Date; /** * @Author weihongyang @@ -78,6 +79,7 @@ public class LogOperationAspect { LoginUser loginUser = (LoginUser) authentication.getPrincipal(); if(loginUser != null){ log.setCreatorName(loginUser.getUsername()); + log.setCreator(loginUser.getSysUserEntity().getCreator()); } log.setStatus(status); @@ -89,6 +91,7 @@ public class LogOperationAspect { log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT)); log.setRequestUri(request.getRequestURI()); log.setRequestMethod(request.getMethod()); + log.setCreateDate(new Date()); //请求参数 Object[] args = joinPoint.getArgs(); From 030abafd1f0d591e604fd14614c72c9dc2c63de9 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Mon, 8 Aug 2022 16:36:53 +0800 Subject: [PATCH 11/13] =?UTF-8?q?fix:=20:bug:=E4=BF=AE=E6=94=B9=E5=88=86?= =?UTF-8?q?=E9=A1=B5=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 11 ++- .../cnbm/admin/config/MybatisPlusConfig.java | 29 +++++++ .../controller/SysLogLoginController.java | 4 + .../cnbm/admin/enums/LoginOperationEnum.java | 22 ++++++ .../com/cnbm/admin/enums/LoginStatusEnum.java | 26 +++++++ .../admin/service/impl/LoginServiceImpl.java | 26 +++++++ .../service/impl/SysLogLoginServiceImpl.java | 4 +- .../service/impl/UserDetailsServiceImpl.java | 10 ++- .../interceptor/DataFilterInterceptor.java | 76 +++++++++++++++++++ .../cnbm/common/interceptor/DataScope.java | 22 ++++++ .../src/main/resources/application-dev.yml | 34 ++++++++- ym-gateway/src/main/resources/application.yml | 8 +- .../src/main/resources/logback-spring.xml | 21 +++++ 13 files changed, 277 insertions(+), 16 deletions(-) create mode 100644 ym-admin/src/main/java/com/cnbm/admin/config/MybatisPlusConfig.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/enums/LoginOperationEnum.java create mode 100644 ym-admin/src/main/java/com/cnbm/admin/enums/LoginStatusEnum.java create mode 100644 ym-common/src/main/java/com/cnbm/common/interceptor/DataFilterInterceptor.java create mode 100644 ym-common/src/main/java/com/cnbm/common/interceptor/DataScope.java create mode 100644 ym-gateway/src/main/resources/logback-spring.xml diff --git a/pom.xml b/pom.xml index e30dc2c..165be5f 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ 1.8 4.13.2 4.2.2 - + 1.2.9 3.5.1 4.0 11.2.0.3 @@ -119,9 +119,14 @@ postgresql - org.springframework.boot - spring-boot-starter-data-jdbc + com.alibaba + druid-spring-boot-starter + ${druid.version} + + + + cn.afterturn diff --git a/ym-admin/src/main/java/com/cnbm/admin/config/MybatisPlusConfig.java b/ym-admin/src/main/java/com/cnbm/admin/config/MybatisPlusConfig.java new file mode 100644 index 0000000..3f5b021 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/config/MybatisPlusConfig.java @@ -0,0 +1,29 @@ +package com.cnbm.admin.config; + +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import com.cnbm.common.interceptor.DataFilterInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MybatisPlusConfig { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); + // 数据权限 + mybatisPlusInterceptor.addInnerInterceptor(new DataFilterInterceptor()); + // 分页插件 + mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); + // 乐观锁 + mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); + // 防止全表更新与删除 + mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); + + return mybatisPlusInterceptor; + } + +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java b/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java index 95cf2a1..8234ffa 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java +++ b/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java @@ -12,6 +12,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.GetMapping; @@ -27,6 +28,7 @@ import java.util.Map; @RestController @RequestMapping("sys/log/login") @Api(tags="登录日志") +@Log4j2 public class SysLogLoginController { @Autowired private SysLogLoginService sysLogLoginService; @@ -45,6 +47,8 @@ public class SysLogLoginController { public Result> page(@ApiIgnore @RequestParam Map params){ PageData page = sysLogLoginService.page(params); + log.info("PageData====={}",page.toString()); + return new Result>().ok(page); } diff --git a/ym-admin/src/main/java/com/cnbm/admin/enums/LoginOperationEnum.java b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginOperationEnum.java new file mode 100644 index 0000000..dea9804 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginOperationEnum.java @@ -0,0 +1,22 @@ +package com.cnbm.admin.enums; + +public enum LoginOperationEnum { + /** + * 用户登录 + */ + LOGIN(0), + /** + * 用户退出 + */ + LOGOUT(1); + + private int value; + + LoginOperationEnum(int value) { + this.value = value; + } + + public int value() { + return this.value; + } +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/enums/LoginStatusEnum.java b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginStatusEnum.java new file mode 100644 index 0000000..e30d1b3 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginStatusEnum.java @@ -0,0 +1,26 @@ +package com.cnbm.admin.enums; + +public enum LoginStatusEnum { + /** + * 失败 + */ + FAIL(0), + /** + * 成功 + */ + SUCCESS(1), + /** + * 账号已锁定 + */ + LOCK(2); + + private int value; + + LoginStatusEnum(int value) { + this.value = value; + } + + public int value() { + return this.value; + } +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java index ada95af..d1cf8c1 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java @@ -1,14 +1,20 @@ package com.cnbm.admin.service.impl; import com.cnbm.admin.entity.LoginUser; +import com.cnbm.admin.entity.SysLogLoginEntity; +import com.cnbm.admin.enums.LoginOperationEnum; +import com.cnbm.admin.enums.LoginStatusEnum; import com.cnbm.admin.params.LoginParam; import com.cnbm.admin.service.CaptchaService; +import com.cnbm.admin.service.SysLogLoginService; import com.cnbm.admin.utils.JwtUtil; import com.cnbm.admin.service.LoginService; import com.cnbm.admin.utils.ResponseResult; import com.cnbm.common.exception.ErrorCode; +import com.cnbm.common.utils.IpUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.HttpHeaders; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -16,6 +22,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; +import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -37,6 +44,9 @@ public class LoginServiceImpl implements LoginService { @Autowired private CaptchaService captchaService; + @Autowired + private SysLogLoginService sysLogLoginService; + @Override public ResponseResult login(HttpServletRequest request, LoginParam loginParam) { //验证码是否正确 @@ -46,7 +56,18 @@ public class LoginServiceImpl implements LoginService { } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginParam.getUsername(),loginParam.getPassword()); Authentication authenticate = authenticationManager.authenticate(authenticationToken); + //登录日志 + SysLogLoginEntity log = new SysLogLoginEntity(); + log.setOperation(LoginOperationEnum.LOGIN.value()); + log.setCreateDate(new Date()); + log.setIp(IpUtils.getIpAddr(request)); + log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT)); + log.setCreatorName(loginParam.getUsername()); + if (Objects.isNull(authenticate)) { + log.setStatus(LoginStatusEnum.FAIL.value()); + log.setCreatorName(loginParam.getUsername()); + sysLogLoginService.save(log); throw new RuntimeException("登录失败"); } //如果认证通过了,使用userid生成一个jwt jwt存入ResponseResult返回 @@ -57,6 +78,11 @@ public class LoginServiceImpl implements LoginService { map.put("token",jwt); //把完整的用户信息存入redis userid作为key redisTemplate.opsForValue().set("login:"+userid,loginUser); + //登录成功 + log.setStatus(LoginStatusEnum.SUCCESS.value()); + log.setCreator(loginUser.getSysUserEntity().getId()); + log.setCreatorName(loginUser.getUsername()); + sysLogLoginService.save(log); return new ResponseResult(200,"登录成功",map); } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java index 21db772..e233466 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java @@ -10,6 +10,7 @@ import com.cnbm.common.constant.Constant; import com.cnbm.common.page.PageData; import com.cnbm.common.service.impl.BaseServiceImpl; import com.cnbm.common.utils.ConvertUtils; +import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,6 +19,7 @@ import java.util.List; import java.util.Map; @Service +@Log4j2 public class SysLogLoginServiceImpl extends BaseServiceImpl implements SysLogLoginService { @Override @@ -26,6 +28,7 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(status), "status", status); wrapper.like(StringUtils.isNotBlank(creatorName), "creator_name", creatorName); - return wrapper; } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java index 389c4c9..0204aa6 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java @@ -5,23 +5,24 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.cnbm.admin.dto.SysMenuDTO; import com.cnbm.admin.dto.SysUserDTO; import com.cnbm.admin.entity.LoginUser; +import com.cnbm.admin.entity.SysLogLoginEntity; import com.cnbm.admin.entity.SysMenuEntity; import com.cnbm.admin.entity.SysUserEntity; +import com.cnbm.admin.enums.LoginOperationEnum; import com.cnbm.admin.exception.LoginStatusException; import com.cnbm.admin.service.SysMenuService; import com.cnbm.common.utils.ConvertUtils; +import com.cnbm.common.utils.IpUtils; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.security.authentication.DisabledException; 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.Objects; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -45,6 +46,7 @@ public class UserDetailsServiceImpl implements UserDetailsService { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(SysUserEntity::getUsername,username); SysUserEntity sysUserEntity = userDao.selectOne(lambdaQueryWrapper); + if (Objects.isNull(sysUserEntity)) { throw new UsernameNotFoundException("用户名不存在"); } diff --git a/ym-common/src/main/java/com/cnbm/common/interceptor/DataFilterInterceptor.java b/ym-common/src/main/java/com/cnbm/common/interceptor/DataFilterInterceptor.java new file mode 100644 index 0000000..9c8db8d --- /dev/null +++ b/ym-common/src/main/java/com/cnbm/common/interceptor/DataFilterInterceptor.java @@ -0,0 +1,76 @@ +package com.cnbm.common.interceptor; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.toolkit.PluginUtils; +import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; +import net.sf.jsqlparser.JSQLParserException; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.StringValue; +import net.sf.jsqlparser.expression.operators.conditional.AndExpression; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.statement.select.PlainSelect; +import net.sf.jsqlparser.statement.select.Select; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.util.Map; + +public class DataFilterInterceptor implements InnerInterceptor { + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { + DataScope scope = getDataScope(parameter); + // 不进行数据过滤 + if(scope == null || StrUtil.isBlank(scope.getSqlFilter())){ + return; + } + + // 拼接新SQL + String buildSql = getSelect(boundSql.getSql(), scope); + + // 重写SQL + PluginUtils.mpBoundSql(boundSql).sql(buildSql); + } + + private DataScope getDataScope(Object parameter){ + if (parameter == null){ + return null; + } + + // 判断参数里是否有DataScope对象 + if (parameter instanceof Map) { + Map parameterMap = (Map) parameter; + for (Map.Entry entry : parameterMap.entrySet()) { + if (entry.getValue() != null && entry.getValue() instanceof DataScope) { + return (DataScope) entry.getValue(); + } + } + } else if (parameter instanceof DataScope) { + return (DataScope) parameter; + } + + return null; + } + + private String getSelect(String buildSql, DataScope scope){ + try { + Select select = (Select) CCJSqlParserUtil.parse(buildSql); + PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); + + Expression expression = plainSelect.getWhere(); + if(expression == null){ + plainSelect.setWhere(new StringValue(scope.getSqlFilter())); + }else{ + AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter())); + plainSelect.setWhere(andExpression); + } + + return select.toString().replaceAll("'", ""); + }catch (JSQLParserException e){ + return buildSql; + } + } +} diff --git a/ym-common/src/main/java/com/cnbm/common/interceptor/DataScope.java b/ym-common/src/main/java/com/cnbm/common/interceptor/DataScope.java new file mode 100644 index 0000000..1f7f8a2 --- /dev/null +++ b/ym-common/src/main/java/com/cnbm/common/interceptor/DataScope.java @@ -0,0 +1,22 @@ +package com.cnbm.common.interceptor; + +public class DataScope { + private String sqlFilter; + + public DataScope(String sqlFilter) { + this.sqlFilter = sqlFilter; + } + + public String getSqlFilter() { + return sqlFilter; + } + + public void setSqlFilter(String sqlFilter) { + this.sqlFilter = sqlFilter; + } + + @Override + public String toString() { + return this.sqlFilter; + } +} diff --git a/ym-gateway/src/main/resources/application-dev.yml b/ym-gateway/src/main/resources/application-dev.yml index 609c59e..2bdf131 100644 --- a/ym-gateway/src/main/resources/application-dev.yml +++ b/ym-gateway/src/main/resources/application-dev.yml @@ -20,10 +20,36 @@ spring: # url: jdbc:postgresql://192.168.10.10:5432/postgres # username: postgres # password: 123456 - hikari: - pool-name: GrowUpHikariCP - minimum-idle: 1 - maximum-pool-size: 10 + initial-size: 10 + max-active: 100 + min-idle: 10 + max-wait: 60000 + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 20 + time-between-eviction-runs-millis: 60000 + min-evictable-idle-time-millis: 300000 + #Oracle需要打开注释 + #validation-query: SELECT 1 FROM DUAL + test-while-idle: true + test-on-borrow: false + test-on-return: false + stat-view-servlet: + enabled: true + url-pattern: /druid/* + #login-username: admin + #login-password: admin + filter: + stat: + log-slow-sql: true + slow-sql-millis: 1000 + merge-sql: false + wall: + config: + multi-statement-allow: true +# hikari: +# pool-name: GrowUpHikariCP +# minimum-idle: 1 +# maximum-pool-size: 10 ##多数据源的配置,需要引用renren-dynamic-datasource #dynamic: diff --git a/ym-gateway/src/main/resources/application.yml b/ym-gateway/src/main/resources/application.yml index d3ab12d..f6a9105 100644 --- a/ym-gateway/src/main/resources/application.yml +++ b/ym-gateway/src/main/resources/application.yml @@ -69,12 +69,12 @@ mybatis-plus: cache-enabled: false call-setters-on-nulls: true jdbc-type-for-null: 'null' - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl configuration-properties: prefix: blobType: BLOB boolValue: TRUE -logging: - level: - com.cnbm.admin.dao: DEBUG \ No newline at end of file +#logging: +# level: +# com.cnbm.admin.dao: DEBUG \ No newline at end of file diff --git a/ym-gateway/src/main/resources/logback-spring.xml b/ym-gateway/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..7111d2f --- /dev/null +++ b/ym-gateway/src/main/resources/logback-spring.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + From 78fa6f42b055f817af49c70b9798d206ff480552 Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Tue, 9 Aug 2022 11:05:44 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=F0=9F=90=9B=20fix:=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cnbm/admin/config/SecurityConfig.java | 3 ++- .../java/com/cnbm/admin/entity/LoginUser.java | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/ym-admin/src/main/java/com/cnbm/admin/config/SecurityConfig.java b/ym-admin/src/main/java/com/cnbm/admin/config/SecurityConfig.java index 2fff7b1..4c58b45 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/config/SecurityConfig.java +++ b/ym-admin/src/main/java/com/cnbm/admin/config/SecurityConfig.java @@ -72,7 +72,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { "/swagger-ui/**", "/webjars/**", "/websocket/**", - "/captcha").anonymous() + "/captcha", + "druid/**").anonymous() // .antMatchers("/testCors").hasAuthority("system:dept:list222") // 除上面外的所有请求全部需要鉴权认证 .anyRequest() diff --git a/ym-admin/src/main/java/com/cnbm/admin/entity/LoginUser.java b/ym-admin/src/main/java/com/cnbm/admin/entity/LoginUser.java index 6729ecd..21569dd 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/entity/LoginUser.java +++ b/ym-admin/src/main/java/com/cnbm/admin/entity/LoginUser.java @@ -1,16 +1,19 @@ package com.cnbm.admin.entity; -import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import lombok.Data; -import lombok.NoArgsConstructor; +import java.io.Serializable; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.Data; +import lombok.NoArgsConstructor; /** * @Author weihongyang @@ -20,7 +23,9 @@ import java.util.stream.Collectors; @Data @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class LoginUser implements UserDetails { +public class LoginUser implements UserDetails,Serializable { + + private static final long serialVersionUID = 1L; private SysUserEntity sysUserEntity; From f5988be7361184a194eff260b62e3b72308fdf3c Mon Sep 17 00:00:00 2001 From: weihongyang <1075331873@qq.com> Date: Tue, 9 Aug 2022 14:21:05 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E2=9C=A8=20feat:=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E9=80=80=E5=87=BA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/LoginController.java | 9 ++++-- .../com/cnbm/admin/service/LoginService.java | 6 ++-- .../admin/service/impl/LoginServiceImpl.java | 29 +++++++++++++++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java b/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java index 029970c..14d31ca 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java +++ b/ym-admin/src/main/java/com/cnbm/admin/controller/LoginController.java @@ -6,6 +6,7 @@ import com.cnbm.admin.service.CaptchaService; import com.cnbm.admin.service.LoginService; import com.cnbm.admin.utils.ResponseResult; import com.cnbm.common.exception.ErrorCode; +import com.cnbm.common.utils.Result; import com.cnbm.common.validator.AssertUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -52,13 +53,15 @@ public class LoginController { @PostMapping("/login") @ApiOperation(value = "登录") - public ResponseResult login(HttpServletRequest request, @RequestBody LoginParam loginParam) { + public Result login(HttpServletRequest request, @RequestBody LoginParam loginParam) { return loginService.login(request,loginParam); } - @PostMapping("/doLogout") + @PostMapping("logout") @ApiOperation(value = "退出") - public void logout(){ + public Result logout(HttpServletRequest request){ + loginService.logout(request); + return new Result(); } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java b/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java index 9f93524..c5bd308 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/LoginService.java @@ -1,7 +1,7 @@ package com.cnbm.admin.service; import com.cnbm.admin.params.LoginParam; -import com.cnbm.admin.utils.ResponseResult; +import com.cnbm.common.utils.Result; import javax.servlet.http.HttpServletRequest; @@ -12,6 +12,8 @@ import javax.servlet.http.HttpServletRequest; */ public interface LoginService { - ResponseResult login(HttpServletRequest request, LoginParam loginParam); + Result login(HttpServletRequest request, LoginParam loginParam); + + void logout(HttpServletRequest request); } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java index d1cf8c1..27468aa 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java @@ -12,6 +12,8 @@ import com.cnbm.admin.service.LoginService; import com.cnbm.admin.utils.ResponseResult; import com.cnbm.common.exception.ErrorCode; import com.cnbm.common.utils.IpUtils; +import com.cnbm.common.utils.Result; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpHeaders; @@ -48,11 +50,11 @@ public class LoginServiceImpl implements LoginService { private SysLogLoginService sysLogLoginService; @Override - public ResponseResult login(HttpServletRequest request, LoginParam loginParam) { + public Result login(HttpServletRequest request, LoginParam loginParam) { //验证码是否正确 boolean flag = captchaService.validate(loginParam.getUuid(), loginParam.getCaptcha()); if(!flag){ - return new ResponseResult(ErrorCode.CAPTCHA_ERROR,"验证码错误"); + return new Result<>().error(ErrorCode.CAPTCHA_ERROR, "验证码错误"); } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginParam.getUsername(),loginParam.getPassword()); Authentication authenticate = authenticationManager.authenticate(authenticationToken); @@ -83,7 +85,28 @@ public class LoginServiceImpl implements LoginService { log.setCreator(loginUser.getSysUserEntity().getId()); log.setCreatorName(loginUser.getUsername()); sysLogLoginService.save(log); - return new ResponseResult(200,"登录成功",map); + return new Result<>().ok(map); + } + + @Override + public void logout(HttpServletRequest request) { + //获取SecurityContextHolder中的用户id + UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); + LoginUser loginUser = (LoginUser) authentication.getPrincipal(); + Long userid = loginUser.getSysUserEntity().getId(); + //日志 + SysLogLoginEntity log = new SysLogLoginEntity(); + log.setOperation(LoginOperationEnum.LOGOUT.value()); + log.setIp(IpUtils.getIpAddr(request)); + log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT)); + log.setIp(IpUtils.getIpAddr(request)); + log.setStatus(LoginStatusEnum.SUCCESS.value()); + log.setCreator(userid); + log.setCreatorName(loginUser.getUsername()); + log.setCreateDate(new Date()); + sysLogLoginService.save(log); + //删除redis中的值 + redisTemplate.delete("login:"+userid); } }