From 82773ba2eb00ef46e0e6933ef0495985d9b5cc2e Mon Sep 17 00:00:00 2001 From: whh <2846845743@qq.com> Date: Fri, 27 Sep 2024 10:15:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8=E6=88=B7=E5=92=8C=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E7=9B=B8=E5=85=B3=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- document/src/main/java/com/haihang/App.java | 12 ++ .../java/com/haihang/config/MvcConfig.java | 31 ++++ .../com/haihang/config/RedissonConfig.java | 32 ++++ .../java/com/haihang/constant/Constant.java | 8 + .../haihang/controller/RoleController.java | 35 +++++ .../haihang/controller/UserController.java | 60 ++++++++ .../main/java/com/haihang/entity/Role.java | 16 ++ .../main/java/com/haihang/entity/User.java | 16 ++ .../main/java/com/haihang/entity/UserDTO.java | 18 +++ .../java/com/haihang/entity/UserInfo.java | 12 ++ .../java/com/haihang/mapper/RoleMapper.java | 24 +++ .../java/com/haihang/mapper/UserMapper.java | 28 ++++ .../com/haihang/mapper/UserRoleMapper.java | 25 ++++ .../java/com/haihang/service/RoleService.java | 12 ++ .../java/com/haihang/service/UserService.java | 19 +++ .../haihang/service/impl/RoleServiceImpl.java | 46 ++++++ .../haihang/service/impl/UserServiceImpl.java | 138 ++++++++++++++++++ .../com/haihang/utils/LoginInterceptor.java | 60 ++++++++ .../main/java/com/haihang/utils/Result.java | 30 ++++ document/src/main/resources/application.yaml | 27 ++++ .../src/main/resources/mapper/userMapper.xml | 8 + pom.xml | 90 ++++++++++++ 23 files changed, 752 insertions(+), 1 deletion(-) create mode 100644 document/src/main/java/com/haihang/App.java create mode 100644 document/src/main/java/com/haihang/config/MvcConfig.java create mode 100644 document/src/main/java/com/haihang/config/RedissonConfig.java create mode 100644 document/src/main/java/com/haihang/constant/Constant.java create mode 100644 document/src/main/java/com/haihang/controller/RoleController.java create mode 100644 document/src/main/java/com/haihang/controller/UserController.java create mode 100644 document/src/main/java/com/haihang/entity/Role.java create mode 100644 document/src/main/java/com/haihang/entity/User.java create mode 100644 document/src/main/java/com/haihang/entity/UserDTO.java create mode 100644 document/src/main/java/com/haihang/entity/UserInfo.java create mode 100644 document/src/main/java/com/haihang/mapper/RoleMapper.java create mode 100644 document/src/main/java/com/haihang/mapper/UserMapper.java create mode 100644 document/src/main/java/com/haihang/mapper/UserRoleMapper.java create mode 100644 document/src/main/java/com/haihang/service/RoleService.java create mode 100644 document/src/main/java/com/haihang/service/UserService.java create mode 100644 document/src/main/java/com/haihang/service/impl/RoleServiceImpl.java create mode 100644 document/src/main/java/com/haihang/service/impl/UserServiceImpl.java create mode 100644 document/src/main/java/com/haihang/utils/LoginInterceptor.java create mode 100644 document/src/main/java/com/haihang/utils/Result.java create mode 100644 document/src/main/resources/application.yaml create mode 100644 document/src/main/resources/mapper/userMapper.xml create mode 100644 pom.xml diff --git a/README.md b/README.md index 495cad6..3061cdf 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # excise-demo -练习 \ No newline at end of file +用户表没密码,统一成123456在逻辑中判断。 +用户新增时要指定好它对应的角色 +新建角色不需要指定他关联的用户 +删除用户或角色都要先删除他们对应的关联表。 +部门修改相当于部门改名字了,部门对应的用户原班人马应该不变。 \ No newline at end of file diff --git a/document/src/main/java/com/haihang/App.java b/document/src/main/java/com/haihang/App.java new file mode 100644 index 0000000..05b2d4f --- /dev/null +++ b/document/src/main/java/com/haihang/App.java @@ -0,0 +1,12 @@ +package com.haihang; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(App.class, args); + + } +} diff --git a/document/src/main/java/com/haihang/config/MvcConfig.java b/document/src/main/java/com/haihang/config/MvcConfig.java new file mode 100644 index 0000000..8ccb969 --- /dev/null +++ b/document/src/main/java/com/haihang/config/MvcConfig.java @@ -0,0 +1,31 @@ +package com.haihang.config; + +import com.haihang.utils.LoginInterceptor; +import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.annotation.Resource; + +/** + * mvc配置 + * + * @author CHEN + * @date 2022/10/07 + */ +@Configuration +public class MvcConfig implements WebMvcConfigurer { + @Resource + private StringRedisTemplate stringRedisTemplate; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + //登陆拦截器 + registry + .addInterceptor(new LoginInterceptor(stringRedisTemplate)) + .excludePathPatterns("/user/login"); + } +} diff --git a/document/src/main/java/com/haihang/config/RedissonConfig.java b/document/src/main/java/com/haihang/config/RedissonConfig.java new file mode 100644 index 0000000..b337336 --- /dev/null +++ b/document/src/main/java/com/haihang/config/RedissonConfig.java @@ -0,0 +1,32 @@ +package com.haihang.config; + +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * redisson配置 + * + * @author CHEN + * @date 2022/10/10 + */ +@Configuration +public class RedissonConfig { + @Value("${spring.redis.host}") + private String host; + @Value("${spring.redis.port}") + private String port; + + @Bean + public RedissonClient redissonClient(){ + //配置 + Config config=new Config(); + config.useSingleServer().setAddress("redis://"+host+":"+port); + //创建对并且返回 + return Redisson.create(config); + } + +} diff --git a/document/src/main/java/com/haihang/constant/Constant.java b/document/src/main/java/com/haihang/constant/Constant.java new file mode 100644 index 0000000..2666a15 --- /dev/null +++ b/document/src/main/java/com/haihang/constant/Constant.java @@ -0,0 +1,8 @@ +package com.haihang.constant; + +public class Constant { + + public static String LOGIN_COUNT_KEY = "login:count:";//登录次数 + public static String LOGIN_LOCK_KEY = "login:lock:";//1代表被锁定 + public static String LOGIN_USER_KEY = "login:token:"; +} diff --git a/document/src/main/java/com/haihang/controller/RoleController.java b/document/src/main/java/com/haihang/controller/RoleController.java new file mode 100644 index 0000000..cb2ff90 --- /dev/null +++ b/document/src/main/java/com/haihang/controller/RoleController.java @@ -0,0 +1,35 @@ +package com.haihang.controller; + + +import com.haihang.entity.Role; +import com.haihang.service.RoleService; +import com.haihang.utils.Result; +import org.apache.ibatis.annotations.Delete; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/role") +public class RoleController { + + @Autowired + private RoleService roleService; + + + @PutMapping("/add") + public Result add(Role role) { + return roleService.add(role); + + } + + + @Delete("/{id}") + public Result delete(@PathVariable Integer id) { + return roleService.delete(id); + } + @PutMapping("/update") + public Result update(@RequestBody Role role) { + return roleService.update(role); + } + +} diff --git a/document/src/main/java/com/haihang/controller/UserController.java b/document/src/main/java/com/haihang/controller/UserController.java new file mode 100644 index 0000000..0195c6a --- /dev/null +++ b/document/src/main/java/com/haihang/controller/UserController.java @@ -0,0 +1,60 @@ +package com.haihang.controller; + +import com.haihang.entity.User; +import com.haihang.entity.UserDTO; +import com.haihang.service.UserService; +import com.haihang.utils.Result; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +@Slf4j +@RequestMapping("/user") +@RestController +public class UserController { + + @Autowired + private UserService userService; + + /** + * 用户登录接口 + */ + + @PostMapping("/login") + public Result login(@RequestBody User user) { + //接收一个用户登录名和用户的密码,因为数据库中没有密码字段,所以可以直接把密码存数据库里面。 + //因为没有注册接口,可以做一个缓存预热。提前存用户密码 + + return userService.login(user); + + } + + + @PutMapping("/add") + public Result add(@RequestBody UserDTO user) { + //新增用户 + log.info("新增用户:{}",user); + return userService.add(user); + } + + @DeleteMapping("/{id}") + public Result delete(@PathVariable("id") int id) { + log.info("删除用户的id:{}",id); + return userService.deleteUserById(id); + } + + @GetMapping("/{id}") + public Result getUserById(@PathVariable("id") int id) { + log.info("查询用户:{}",id); + return userService.selectUserById(id); + } + + @PutMapping("/updat") + public Result update(@RequestBody UserDTO userDTO) { + + return userService.update(userDTO); + } + +} diff --git a/document/src/main/java/com/haihang/entity/Role.java b/document/src/main/java/com/haihang/entity/Role.java new file mode 100644 index 0000000..8f62576 --- /dev/null +++ b/document/src/main/java/com/haihang/entity/Role.java @@ -0,0 +1,16 @@ +package com.haihang.entity; + +import lombok.Data; + +import java.util.Date; + +@Data +public class Role { + + + private int id; + private String roleName; + private String roleCode; //英文代号 + private Date createTime; + private Date updateTime; +} diff --git a/document/src/main/java/com/haihang/entity/User.java b/document/src/main/java/com/haihang/entity/User.java new file mode 100644 index 0000000..1937495 --- /dev/null +++ b/document/src/main/java/com/haihang/entity/User.java @@ -0,0 +1,16 @@ +package com.haihang.entity; + +import lombok.Data; + +import java.util.Date; + +@Data +public class User { + private int id;//id + private String password; + private String loginName;//登录名 + private String userChnName;//用户中文名 + private Date createTime; + private Date updateTime; +} + diff --git a/document/src/main/java/com/haihang/entity/UserDTO.java b/document/src/main/java/com/haihang/entity/UserDTO.java new file mode 100644 index 0000000..39aa733 --- /dev/null +++ b/document/src/main/java/com/haihang/entity/UserDTO.java @@ -0,0 +1,18 @@ +package com.haihang.entity; + +import lombok.Data; + +import java.util.Date; + +@Data +public class UserDTO { + + + private int id;//id + + + private String loginName;//登录名 + private String userChnName;//用户中文名 + + private int roleId;//前端传过来的角色id。新增用户时要指定他的角色是什么。 +} diff --git a/document/src/main/java/com/haihang/entity/UserInfo.java b/document/src/main/java/com/haihang/entity/UserInfo.java new file mode 100644 index 0000000..0f877a7 --- /dev/null +++ b/document/src/main/java/com/haihang/entity/UserInfo.java @@ -0,0 +1,12 @@ +package com.haihang.entity; + +import lombok.Data; + +//用来存放token的用户简单信息 +@Data +public class UserInfo { + + private int id; + private String loginName; + private String UserChnName; +} diff --git a/document/src/main/java/com/haihang/mapper/RoleMapper.java b/document/src/main/java/com/haihang/mapper/RoleMapper.java new file mode 100644 index 0000000..971fae7 --- /dev/null +++ b/document/src/main/java/com/haihang/mapper/RoleMapper.java @@ -0,0 +1,24 @@ +package com.haihang.mapper; + +import com.haihang.entity.Role; +import com.haihang.entity.User; +import org.apache.ibatis.annotations.*; + +@Mapper +public interface RoleMapper { + + @Insert("insert into user (ID, ROLE_NAME,ROLE_CODE,CREATE_TIME,UPDATE_TIME) values " + + " (null,#{roleName},#{roleCode},now(),now())") + void insertRole(Role role); + + + @Delete("delete from role where id=#{id}") + void deleteRoleById(int id); + + + @Select("select * from role where id=#{id}") + User getRoleById(int id); + + @Update("update role set ROLE_NAME = #{roleName},ROLE_CODE =#{roleCode} where ROLE_ID=#{id}") + void update(Role role); +} diff --git a/document/src/main/java/com/haihang/mapper/UserMapper.java b/document/src/main/java/com/haihang/mapper/UserMapper.java new file mode 100644 index 0000000..b63a8c1 --- /dev/null +++ b/document/src/main/java/com/haihang/mapper/UserMapper.java @@ -0,0 +1,28 @@ +package com.haihang.mapper; + +import com.haihang.entity.User; +import com.haihang.entity.UserDTO; +import org.apache.ibatis.annotations.*; + +@Mapper +public interface UserMapper { + + @Select("select * from user where LOGIN_NAME = #{loginName}") + User getUserByLoginName(String loginName); + + @Insert("insert into user (ID, LOGIN_NAME,USER_CHN_NAME,CREATE_TIME,UPDATE_TIME) values " + + " (null,#{loginName},#{userChnName},now(),now())") + @Options(useGeneratedKeys = true, keyProperty = "id", flushCache = Options.FlushCachePolicy.TRUE) + void insertUser(User user); + + + @Delete("delete from user where id=#{id}") + void deleteUserById(int id); + + + @Select("select * from user where id=#{id}") + User getUserById(int id); + + @Update("update user set LOGIN_NAME = #{loginName},USER_CHN_NAME =#{userChnName} where id =#{id} ") + void updateUser(UserDTO userDTO); +} diff --git a/document/src/main/java/com/haihang/mapper/UserRoleMapper.java b/document/src/main/java/com/haihang/mapper/UserRoleMapper.java new file mode 100644 index 0000000..498ab4b --- /dev/null +++ b/document/src/main/java/com/haihang/mapper/UserRoleMapper.java @@ -0,0 +1,25 @@ +package com.haihang.mapper; + +import com.haihang.entity.UserDTO; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Update; + +@Mapper +public interface UserRoleMapper { + + @Delete("delete from user_role_relation where USER_ID = #{userId}") + void deleteByUserId(Integer userId); + + @Delete("delete from user_role_relation where ROLE_ID = #{id}") + void deleteByRoleId(int id); + + + @Insert("insert into user_role_relation values (null,#{roleId},#{userId})") + void insert(int userId, int roleId); + + @Update("update user_role_relation " + + "set ROLE_ID = #{roleId} where USER_ID =#{id}") + void updateRoleIdByUserId(UserDTO userDTO); +} diff --git a/document/src/main/java/com/haihang/service/RoleService.java b/document/src/main/java/com/haihang/service/RoleService.java new file mode 100644 index 0000000..d78ddf2 --- /dev/null +++ b/document/src/main/java/com/haihang/service/RoleService.java @@ -0,0 +1,12 @@ +package com.haihang.service; + +import com.haihang.entity.Role; +import com.haihang.utils.Result; + +public interface RoleService { + Result add(Role role); + + Result delete(int id); + + Result update(Role role); +} diff --git a/document/src/main/java/com/haihang/service/UserService.java b/document/src/main/java/com/haihang/service/UserService.java new file mode 100644 index 0000000..8213315 --- /dev/null +++ b/document/src/main/java/com/haihang/service/UserService.java @@ -0,0 +1,19 @@ +package com.haihang.service; + +import com.haihang.entity.User; +import com.haihang.entity.UserDTO; +import com.haihang.utils.Result; +import org.springframework.stereotype.Service; + +@Service +public interface UserService { + Result login(User user); + + Result add(UserDTO user); + + Result deleteUserById(int id); + + Result selectUserById(int id); + + Result update(UserDTO userDTO); +} diff --git a/document/src/main/java/com/haihang/service/impl/RoleServiceImpl.java b/document/src/main/java/com/haihang/service/impl/RoleServiceImpl.java new file mode 100644 index 0000000..28fce81 --- /dev/null +++ b/document/src/main/java/com/haihang/service/impl/RoleServiceImpl.java @@ -0,0 +1,46 @@ +package com.haihang.service.impl; + +import com.haihang.entity.Role; +import com.haihang.mapper.RoleMapper; +import com.haihang.mapper.UserRoleMapper; +import com.haihang.service.RoleService; +import com.haihang.utils.Result; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class RoleServiceImpl implements RoleService { + + @Autowired + private RoleMapper roleMapper; + + + @Autowired + private UserRoleMapper userRoleMapper; + + @Override + public Result add(Role role) { + roleMapper.insertRole(role); + return Result.ok(); + } + + @Override + public Result delete(int id) { + + //删除用户记录的时候要先删除用户角色关联的信息 + userRoleMapper.deleteByRoleId(id); + roleMapper.deleteRoleById(id); + return Result.ok(); + } + + @Override + public Result update(Role role) { + //部门改名字了,但是原本的用户没有变化 + roleMapper.update(role); + + + return Result.ok(); + } + + +} diff --git a/document/src/main/java/com/haihang/service/impl/UserServiceImpl.java b/document/src/main/java/com/haihang/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..dcd23e1 --- /dev/null +++ b/document/src/main/java/com/haihang/service/impl/UserServiceImpl.java @@ -0,0 +1,138 @@ +package com.haihang.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.bean.copier.CopyOptions; +import cn.hutool.core.lang.UUID; +import com.haihang.constant.Constant; +import com.haihang.entity.User; +import com.haihang.entity.UserDTO; +import com.haihang.entity.UserInfo; +import com.haihang.mapper.UserMapper; +import com.haihang.mapper.UserRoleMapper; +import com.haihang.service.UserService; +import com.haihang.utils.Result; +import lombok.extern.slf4j.Slf4j; +import org.redisson.api.RedissonClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static com.haihang.constant.Constant.LOGIN_USER_KEY; + +@Slf4j +@Service +public class UserServiceImpl implements UserService { + + + @Autowired + private UserMapper userMapper; + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + @Autowired + private RedissonClient redissonClient; + + @Autowired + private UserRoleMapper userRoleMapper; + + @Override + public Result login(User user) { + + //根据传过来的用户名和密码判断登录。 + // 先根据用户名查询所有信息 + User userDb = userMapper.getUserByLoginName(user.getLoginName()); + int id = userDb.getId(); + + + //判断是否已经被锁定 + String isLock = stringRedisTemplate.opsForValue().get(Constant.LOGIN_LOCK_KEY + id); + + if("1".equals(isLock)){ + return Result.fail("因为错误密码次数过多!账户已经锁定!请联系管理员解锁"); + } + + if(!"123456".equals(user.getPassword())){ + log.info("密码错误!"); + //密码错误时redis中的错误记录加1 + stringRedisTemplate.opsForValue().increment(Constant.LOGIN_COUNT_KEY+id); + String s = stringRedisTemplate.opsForValue().get(Constant.LOGIN_COUNT_KEY + id); + + if (Integer.parseInt(s)>=5){ + stringRedisTemplate.opsForValue().set(Constant.LOGIN_LOCK_KEY+id + ,"1"); + return Result.fail("账户将被锁定"); + + } + return Result.fail("用户输入密码错误"); + } + //走到这里说明密码正确,清空并放行 + stringRedisTemplate.delete(Constant.LOGIN_COUNT_KEY + id); + + UserInfo userInfo = new UserInfo(); + userInfo.setId(userDb.getId()); + userInfo.setLoginName(user.getLoginName()); + userInfo.setUserChnName(userDb.getUserChnName()); + String token = UUID.fastUUID().toString(true); + //将用户信息存放到redis中 + Map map = BeanUtil.beanToMap(userInfo, new HashMap<>() + , CopyOptions.create().setIgnoreNullValue(true) + .setFieldValueEditor( + (name, value) -> value.toString() + )); + //保存用户信息到redis + stringRedisTemplate.opsForHash().putAll(LOGIN_USER_KEY + token, map); + //设置过期时间 + stringRedisTemplate.expire(LOGIN_USER_KEY + token, 100000000000L, TimeUnit.MINUTES); + log.info("success"); + return Result.ok(token); + } + + @Override + public Result add(UserDTO userDTO) { + + User user = new User(); + + // 把这个DTO解析成一个user,一个角色id然后分别存入两张表 + BeanUtil.copyProperties(userDTO,user); + userMapper.insertUser(user);//插入user + int roleId = userDTO.getRoleId(); + int userId = user.getId(); + + userRoleMapper.insert(userId,roleId); + + return Result.ok(); + } + + @Override + public Result deleteUserById(int id) { + + //删除用户时,需要优先删除用户角色关联表中用户id为id的记录 + userRoleMapper.deleteByUserId(id); + + userMapper.deleteUserById(id); + return Result.ok(); + } + + @Override + public Result selectUserById(int id) { + User user = userMapper.getUserById(id); + return Result.ok(user); + } + + @Override + public Result update(UserDTO userDTO) { + //根据给定的id更新用户信息 + userMapper.updateUser(userDTO); + + //根据roleid更新用户对于角色 + userRoleMapper.updateRoleIdByUserId(userDTO); + return Result.ok(); + } + + +} diff --git a/document/src/main/java/com/haihang/utils/LoginInterceptor.java b/document/src/main/java/com/haihang/utils/LoginInterceptor.java new file mode 100644 index 0000000..c75361b --- /dev/null +++ b/document/src/main/java/com/haihang/utils/LoginInterceptor.java @@ -0,0 +1,60 @@ +package com.haihang.utils; + + + + +import cn.hutool.core.bean.BeanUtil; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import static com.haihang.constant.Constant.LOGIN_USER_KEY; + +/** + * 登录拦截器 + * + * @author CHEN + * @date 2022/10/07 + */ +public class LoginInterceptor implements HandlerInterceptor { + + private final StringRedisTemplate stringRedisTemplate; + + public LoginInterceptor(StringRedisTemplate stringRedisTemplate) { + this.stringRedisTemplate=stringRedisTemplate; + } + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + //解析token + //从请求头中获取token + String token = request.getHeader("authorization"); + if (StringUtils.isEmpty(token)) { + //前端没传 + throw new Exception("前端没有携带token!"); + + } + //从redis中获取用户 + Map userMap = + stringRedisTemplate.opsForHash() + .entries(LOGIN_USER_KEY + token); + //前端的这个token没有值 + if (userMap.isEmpty()) { + throw new Exception("无效token!!"); + + } +// //hash转UserDTO存入ThreadLocal +// UserHolder.saveUser(BeanUtil.fillBeanWithMap(userMap, new UserDTO(), false)); + //走到这里说明前端传过来的token里面有值且是登录用户,放行就完事了,暂时不用ThreadLocal + return true; + } + + +} diff --git a/document/src/main/java/com/haihang/utils/Result.java b/document/src/main/java/com/haihang/utils/Result.java new file mode 100644 index 0000000..be8f589 --- /dev/null +++ b/document/src/main/java/com/haihang/utils/Result.java @@ -0,0 +1,30 @@ +package com.haihang.utils; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Result { + private Boolean success; + private String errorMsg; + private Object data; + private Long total; + + public static Result ok(){ + return new Result(true, null, null, null); + } + public static Result ok(Object data){ + return new Result(true, null, data, null); + } + public static Result ok(List data, Long total){ + return new Result(true, null, data, total); + } + public static Result fail(String errorMsg){ + return new Result(false, errorMsg, null, null); + } +} diff --git a/document/src/main/resources/application.yaml b/document/src/main/resources/application.yaml new file mode 100644 index 0000000..912bad6 --- /dev/null +++ b/document/src/main/resources/application.yaml @@ -0,0 +1,27 @@ +server: + port: 8081 +spring: + application: + name: hmdp + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://117.72.91.159:13306/test_repo?useSSL=false&serverTimezone=UTC + username: root + password: 123456 + redis: + host: 117.72.91.159 + port: 16379 + + lettuce: + pool: + max-active: 10 + max-idle: 10 + min-idle: 1 + time-between-eviction-runs: 10s + jackson: + default-property-inclusion: non_null # JSON处理时忽略非空字段 +mybatis-plus: + type-aliases-package: com.hmdp.entity # 别名扫描包 +logging: + level: + com.haihang: debug \ No newline at end of file diff --git a/document/src/main/resources/mapper/userMapper.xml b/document/src/main/resources/mapper/userMapper.xml new file mode 100644 index 0000000..1af354f --- /dev/null +++ b/document/src/main/resources/mapper/userMapper.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..81b443d --- /dev/null +++ b/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + com.haihang + excersice + 1.0-SNAPSHOT + + org.springframework.boot + spring-boot-starter-parent + 2.7.4 + + + + + 8 + 8 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + org.apache.commons + commons-pool2 + + + org.springframework.boot + spring-boot-starter-web + + + + mysql + mysql-connector-java + 8.0.19 + + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + com.baomidou + mybatis-plus-boot-starter + 3.5.2 + + + + cn.hutool + hutool-all + 5.8.8 + + + + org.redisson + redisson + 3.17.7 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.7.4 + + + + org.projectlombok + lombok + + + + + + + + \ No newline at end of file