From 474c9499f64a7b9bdf6b5d97b3ad8b4d380b27e0 Mon Sep 17 00:00:00 2001 From: yulinling <2712495353@qq.com> Date: Tue, 10 Jun 2025 22:48:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20-=20=E4=BF=AE=E6=94=B9database=20sql=20?= =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0Security=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 + .../workflow/config/SecurityConfig.java | 24 +++++ .../workflow/controller/TestController.java | 5 + .../yulinling/workflow/model/entity/User.java | 32 +++++-- .../workflow/model/vo/user/UserVO.java | 2 +- .../service/impl/UserServiceImpl.java | 2 +- src/main/resources/application.properties | 12 +-- src/main/resources/db/data.sql | 95 ++++++++++++++++++- src/main/resources/db/schema.sql | 74 ++++++++++++--- src/main/resources/mapper/UserMapper.xml | 26 ++--- .../workflow/mapper/UserMapperTest.java | 4 +- 11 files changed, 237 insertions(+), 44 deletions(-) create mode 100644 src/main/java/asia/yulinling/workflow/config/SecurityConfig.java diff --git a/pom.xml b/pom.xml index 2df66e8..6508184 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ 17 + 0.9.1 @@ -106,6 +107,10 @@ junit test + + org.springframework.boot + spring-boot-starter-security + diff --git a/src/main/java/asia/yulinling/workflow/config/SecurityConfig.java b/src/main/java/asia/yulinling/workflow/config/SecurityConfig.java new file mode 100644 index 0000000..b1a2f69 --- /dev/null +++ b/src/main/java/asia/yulinling/workflow/config/SecurityConfig.java @@ -0,0 +1,24 @@ +package asia.yulinling.workflow.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.Customizer; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +@EnableWebSecurity +public class SecurityConfig { + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http.authorizeHttpRequests(authorize -> authorize + .requestMatchers("/users", "/users/**").permitAll() + .anyRequest().authenticated()) + .formLogin(formLogin -> formLogin + .loginPage("/login") + .permitAll()) + .rememberMe(Customizer.withDefaults()); + return http.build(); + } +} diff --git a/src/main/java/asia/yulinling/workflow/controller/TestController.java b/src/main/java/asia/yulinling/workflow/controller/TestController.java index 2bee43f..6ba19e9 100644 --- a/src/main/java/asia/yulinling/workflow/controller/TestController.java +++ b/src/main/java/asia/yulinling/workflow/controller/TestController.java @@ -72,4 +72,9 @@ public class TestController { public ApiResponse> usersPage(PageParam pageParam) { return userService.getUserListByPage(pageParam); } + + @GetMapping("/login") + public ApiResponse login() { + return ApiResponse.ofSuccess("登录成功"); + } } diff --git a/src/main/java/asia/yulinling/workflow/model/entity/User.java b/src/main/java/asia/yulinling/workflow/model/entity/User.java index 310b659..1f551c7 100644 --- a/src/main/java/asia/yulinling/workflow/model/entity/User.java +++ b/src/main/java/asia/yulinling/workflow/model/entity/User.java @@ -13,7 +13,7 @@ import java.util.Date; /** *

- * 用户实体类 + * 用户实体类 *

* * @author yulinling @@ -23,7 +23,7 @@ import java.util.Date; @NoArgsConstructor @AllArgsConstructor @Builder -@TableName("`orm_user`") +@TableName("`wk_user`") public class User { /** * 主键id @@ -34,7 +34,12 @@ public class User { /** * 用户名 */ - private String name; + private String username; + + /** + * 昵称 + */ + private String nickname; /** * 加密后的密码 @@ -51,6 +56,16 @@ public class User { */ private String email; + /** + * 生日 + */ + private String birthday; + + /** + * 性别,男-1,女-2 + */ + private Integer sex; + /** * 手机号 */ @@ -67,15 +82,16 @@ public class User { @TableField("create_time") private Date createTime; + /** + * 上次更新时间 + */ + @TableField("update_time") + private Date updateTime; + /** * 上次登录时间 */ @TableField("last_login_time") private Date lastLoginTime; - /** - * 上次更新时间 - */ - @TableField("last_update_time") - private Date lastUpdateTime; } diff --git a/src/main/java/asia/yulinling/workflow/model/vo/user/UserVO.java b/src/main/java/asia/yulinling/workflow/model/vo/user/UserVO.java index 11b2520..1ea35ad 100644 --- a/src/main/java/asia/yulinling/workflow/model/vo/user/UserVO.java +++ b/src/main/java/asia/yulinling/workflow/model/vo/user/UserVO.java @@ -15,7 +15,7 @@ public class UserVO { /** * 用户名 */ - private String name; + private String username; /** * 邮箱 diff --git a/src/main/java/asia/yulinling/workflow/service/impl/UserServiceImpl.java b/src/main/java/asia/yulinling/workflow/service/impl/UserServiceImpl.java index b7e2c42..125f94d 100644 --- a/src/main/java/asia/yulinling/workflow/service/impl/UserServiceImpl.java +++ b/src/main/java/asia/yulinling/workflow/service/impl/UserServiceImpl.java @@ -42,7 +42,7 @@ public class UserServiceImpl implements UserService { if (ArrayUtil.isNotEmpty(users)) { for (User user : users) { UserVO userVO = new UserVO(); - userVO.setName(user.getName()); + userVO.setUsername(user.getUsername()); userVO.setEmail(user.getEmail()); userVO.setPhone(user.getPhone()); userVO.setStatus(user.getStatus()); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7d529f7..e3e986e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ -# ???? +# 服务端配置 server.port=8080 server.servlet.context-path=/demo -# mysql?? +# mysql配置 spring.datasource.url=jdbc:mysql://122.152.201.90:9912/workflow?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=0andrx @@ -10,7 +10,7 @@ spring.sql.init.mode=always spring.sql.init.continue-on-error=true spring.sql.init.schema-locations=classpath:db/schema.sql spring.sql.init.data-locations=classpath:db/data.sql -# ??????? +# 连接池配置 spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.connection-test-query=SELECT 1 spring.datasource.hikari.maximum-pool-size=20 @@ -19,10 +19,10 @@ spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=MyAppHikariCP spring.datasource.hikari.max-lifetime=300000 spring.datasource.hikari.connection-timeout=30000 -# log?? +# log配置 logging.level.asia.yulinling=debug logging.level.asia.yulinling.workflow.mapper=trace -# mail?? +# mail配置 spring.mail.host=smtp.qq.com spring.mail.port=587 spring.mail.username=2712495353@qq.com @@ -35,5 +35,5 @@ spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.ssl.enable=false spring.mail.properties.mail.display.sendmail=spring-boot-demo -# Jasypt?? +# Jasypt配置 jasypt.encryptor.password=abc \ No newline at end of file diff --git a/src/main/resources/db/data.sql b/src/main/resources/db/data.sql index 405ab59..cd95de0 100644 --- a/src/main/resources/db/data.sql +++ b/src/main/resources/db/data.sql @@ -1,2 +1,93 @@ -INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone`) VALUES (1, 'user_1', 'ff342e862e7c3285cdc07e56d6b8973b', '412365a109674b2dbb1981ed561a4c70', 'user1@xkcoding.com', '17300000001'); -INSERT INTO `orm_user`(`id`,`name`,`password`,`salt`,`email`,`phone`) VALUES (2, 'user_2', '6c6bf02c8d5d3d128f34b1700cb1e32c', 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', 'user2@xkcoding.com', '17300000002'); \ No newline at end of file +BEGIN; +INSERT INTO `wk_permission` +VALUES (1072806379288399872, '测试页面', '/test', 1, 'page:test', NULL, 1, 0); +INSERT INTO `wk_permission` +VALUES (1072806379313565696, '测试页面-查询', '/**/test', 2, 'btn:test:query', 'GET', 1, 1072806379288399872); +INSERT INTO `wk_permission` +VALUES (1072806379330342912, '测试页面-添加', '/**/test', 2, 'btn:test:insert', 'POST', 2, 1072806379288399872); +INSERT INTO `wk_permission` +VALUES (1072806379342925824, '监控在线用户页面', '/monitor', 1, 'page:monitor:online', NULL, 2, 0); +INSERT INTO `wk_permission` +VALUES (1072806379363897344, '在线用户页面-查询', '/**/api/monitor/online/user', 2, 'btn:monitor:online:query', 'GET', + 1, + 1072806379342925824); +INSERT INTO `wk_permission` +VALUES (1072806379384868864, '在线用户页面-踢出', '/**/api/monitor/online/user/kickout', 2, + 'btn:monitor:online:kickout', + 'DELETE', 2, 1072806379342925824); +COMMIT; + +BEGIN; +INSERT INTO `wk_role` +VALUES (1072806379208708096, '管理员', '超级管理员', '2018-12-12 14:52:27', '2018-12-12 14:52:27'); +INSERT INTO `wk_role` +VALUES (1072806379238068224, '普通用户', '普通用户', '2018-12-12 14:52:27', '2018-12-12 14:52:27'); +COMMIT; + +BEGIN; +INSERT INTO `wk_role_permission` +VALUES (1072806379208708096, 1072806379288399872); +INSERT INTO `wk_role_permission` +VALUES (1072806379208708096, 1072806379313565696); +INSERT INTO `wk_role_permission` +VALUES (1072806379208708096, 1072806379330342912); +INSERT INTO `wk_role_permission` +VALUES (1072806379208708096, 1072806379342925824); +INSERT INTO `wk_role_permission` +VALUES (1072806379208708096, 1072806379363897344); +INSERT INTO `wk_role_permission` +VALUES (1072806379208708096, 1072806379384868864); +INSERT INTO `wk_role_permission` +VALUES (1072806379238068224, 1072806379288399872); +INSERT INTO `wk_role_permission` +VALUES (1072806379238068224, 1072806379313565696); +COMMIT; + +BEGIN; + +INSERT INTO `wk_user` ( + id, username, nickname, password, salt, email, birthday, sex, phone, status, + create_time, update_time, last_login_time +) VALUES ( + 1072806377661009920, + 'admin', + '管理员', + 'ff342e862e7c3285cdc07e56d6b8973b', + '412365a109674b2dbb1981ed561a4c70', + 'admin@xkcoding.com', + '1994-11-28 00:00:00', -- birthday: 785433600000 → 1994-11-28 + 1, + '17300000000', + 1, + '2018-12-12 14:52:27', -- create_time + '2018-12-12 14:52:27', -- update_time + '2018-12-12 14:52:27' -- last_login_time + ); + +INSERT INTO `wk_user` ( + id, username, nickname, password, salt, email, birthday, sex, phone, status, + create_time, update_time, last_login_time +) VALUES ( + 1072806378780889088, + 'user', + '普通用户', + '6c6bf02c8d5d3d128f34b1700cb1e32c', + 'fcbdd0e8a9404a5585ea4e01d0e4d7a0', + 'user@xkcoding.com', + '1994-11-28 00:00:00', -- birthday: 785433600000 → 1994-11-28 + 1, + '17300001111', + 1, + '2018-12-12 14:52:27', -- create_time + '2018-12-12 14:52:27', -- update_time + '2018-12-12 14:52:27' -- last_login_time + ); + +COMMIT; + +BEGIN; +INSERT INTO `wk_role_user` +VALUES (1072806379208708096, 1072806377661009920); +INSERT INTO `wk_role_user` +VALUES (1072806379238068224, 1072806378780889088); +COMMIT; \ No newline at end of file diff --git a/src/main/resources/db/schema.sql b/src/main/resources/db/schema.sql index e2abd42..80c9d90 100644 --- a/src/main/resources/db/schema.sql +++ b/src/main/resources/db/schema.sql @@ -1,13 +1,61 @@ -DROP TABLE IF EXISTS `orm_user`; -CREATE TABLE `orm_user` ( - `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', - `name` VARCHAR(32) NOT NULL UNIQUE COMMENT '用户名', - `password` VARCHAR(32) NOT NULL COMMENT '加密后的密码', - `salt` VARCHAR(32) NOT NULL COMMENT '加密使用的盐', - `email` VARCHAR(32) NOT NULL UNIQUE COMMENT '邮箱', - `phone` VARCHAR(15) NOT NULL UNIQUE COMMENT '手机号码', - `status` INT(2) NOT NULL DEFAULT 1 COMMENT '状态,-1:逻辑删除,0:禁用,1:启用', - `create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', - `last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间', - `last_update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间' -) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表'; +DROP TABLE IF EXISTS `wk_user`; +CREATE TABLE `wk_user` +( + `id` bigint(64) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', + `username` VARCHAR(32) NOT NULL UNIQUE COMMENT '用户名', + `nickname` VARCHAR(32) NOT NULL UNIQUE COMMENT '昵称', + `password` VARCHAR(32) NOT NULL COMMENT '加密后的密码', + `salt` VARCHAR(32) NOT NULL COMMENT '加密使用的盐', + `email` VARCHAR(32) NOT NULL UNIQUE COMMENT '邮箱', + `birthday` DATETIME DEFAULT NULL COMMENT '生日', + `sex` INT(2) DEFAULT NULL COMMENT '性别,男-1,女-2', + `phone` VARCHAR(15) DEFAULT NULL UNIQUE COMMENT '手机号', + `status` INT(2) NOT NULL DEFAULT 1 COMMENT '状态 -1:删除 0:警用 1:启用', + `create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', + `update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '上次更新时间', + `last_login_time` DATETIME DEFAULT NULL COMMENT '上次登录时间' +) ENGINE = INNODB + DEFAULT CHARSET = UTF8 COMMENT '用户表'; + +DROP TABLE IF EXISTS `wk_role`; +CREATE TABLE `wk_role` +( + `id` BIGINT(64) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', + `name` VARCHAR(32) NOT NULL UNIQUE COMMENT '角色名', + `description` VARCHAR(100) DEFAULT NULL COMMENT '描述', + `create_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '创建时间', + `update_time` DATETIME NOT NULL DEFAULT NOW() COMMENT '更新时间' +) ENGINE = INNODB + DEFAULT CHARSET = UTF8 COMMENT '角色表'; + +DROP TABLE IF EXISTS `wk_permission`; +CREATE TABLE `wk_permission` +( + `id` BIGINT(64) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键', + `name` VARCHAR(32) NOT NULL UNIQUE COMMENT '权限名', + `url` VARCHAR(1000) DEFAULT NULL COMMENT '类型为页面时,代表前端路由地址,类型为按钮时,代表后端接口地址', + `type` INT(2) NOT NULL COMMENT '权限类型,页面-1,按钮-2', + `permission` VARCHAR(50) DEFAULT NULL COMMENT '权限表达式', + `method` VARCHAR(50) DEFAULT NULL COMMENT '后端接口访问方式', + `sort` INT(11) NOT NULL COMMENT '排序', + `parent_id` BIGINT(64) NOT NULL COMMENT '父级ID' +) ENGINE = INNODB + DEFAULT CHARSET = UTF8 COMMENT '权限表'; + +DROP TABLE IF EXISTS `wk_role_permission`; +CREATE TABLE `wk_role_permission` +( + `role_id` BIGINT(64) NOT NULL COMMENT '', + `permission_id` BIGINT(64) NOT NULL COMMENT '', + PRIMARY KEY (`role_id`, `permission_id`) +) ENGINE = INNODB + DEFAULT CHARSET = UTF8 COMMENT '角色权限表'; + +DROP TABLE IF EXISTS `wk_role_user`; +CREATE TABLE `wk_role_user` +( + `role_id` BIGINT(64) NOT NULL COMMENT '', + `user_id` BIGINT(64) NOT NULL COMMENT '', + PRIMARY KEY (`role_id`, `user_id`) +) ENGINE = INNODB + DEFAULT CHARSET = UTF8 COMMENT '角色用户表'; \ No newline at end of file diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml index ce88300..1d4d659 100644 --- a/src/main/resources/mapper/UserMapper.xml +++ b/src/main/resources/mapper/UserMapper.xml @@ -2,16 +2,18 @@ - INSERT INTO `orm_user` (`name`, - `password`, - `salt`, - `email`, - `phone`, - `status`, - `create_time`, - `last_login_time`, - `last_update_time`) + INSERT INTO `wk_user` (`username`, + `nickname`, + `password`, + `salt`, + `email`, + `phone`, + `status`, + `create_time`, + `last_login_time`, + `update_time`) VALUES (#{user.name}, + #{user.nickname}, #{user.password}, #{user.salt}, #{user.email}, @@ -19,10 +21,12 @@ #{user.status}, #{user.createTime}, #{user.lastLoginTime}, - #{user.lastUpdateTime}) + #{user.updateTime}) - DELETE FROM `orm_user` WHERE id = #{id} + DELETE + FROM `wk_user` + WHERE id = #{id} \ No newline at end of file diff --git a/src/test/java/asia/yulinling/workflow/mapper/UserMapperTest.java b/src/test/java/asia/yulinling/workflow/mapper/UserMapperTest.java index d6a70d6..3251ab3 100644 --- a/src/test/java/asia/yulinling/workflow/mapper/UserMapperTest.java +++ b/src/test/java/asia/yulinling/workflow/mapper/UserMapperTest.java @@ -57,7 +57,7 @@ public class UserMapperTest { public void saveUser() { String salt = IdUtil.simpleUUID(); User user = User.builder() - .name("yulinling_test") + .username("yulinling_test") .password(SecureUtil.md5("123456" + salt)) .salt(salt) .email("2712495353@qq.com") @@ -65,7 +65,7 @@ public class UserMapperTest { .status(1) .lastLoginTime(new DateTime()) .createTime(new DateTime()) - .lastUpdateTime(new DateTime()) + .updateTime(new DateTime()) .build(); // int i = userMapper.saveUser(user);