UserController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package com.genersoft.iot.vmp.vmanager.user;
  2. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  3. import com.genersoft.iot.vmp.conf.security.JwtUtils;
  4. import com.genersoft.iot.vmp.conf.security.SecurityUtils;
  5. import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
  6. import com.genersoft.iot.vmp.service.IRoleService;
  7. import com.genersoft.iot.vmp.service.IUserService;
  8. import com.genersoft.iot.vmp.storager.dao.dto.Role;
  9. import com.genersoft.iot.vmp.storager.dao.dto.User;
  10. import com.genersoft.iot.vmp.utils.DateUtil;
  11. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  12. import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
  13. import com.github.pagehelper.PageInfo;
  14. import io.swagger.v3.oas.annotations.Operation;
  15. import io.swagger.v3.oas.annotations.Parameter;
  16. import io.swagger.v3.oas.annotations.tags.Tag;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.security.authentication.AuthenticationManager;
  19. import org.springframework.util.DigestUtils;
  20. import org.springframework.util.ObjectUtils;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.security.sasl.AuthenticationException;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.time.LocalDateTime;
  26. import java.util.List;
  27. @Tag(name = "用户管理")
  28. @RestController
  29. @RequestMapping("/api/user")
  30. public class UserController {
  31. @Autowired
  32. private AuthenticationManager authenticationManager;
  33. @Autowired
  34. private IUserService userService;
  35. @Autowired
  36. private IRoleService roleService;
  37. @GetMapping("/login")
  38. @PostMapping("/login")
  39. @Operation(summary = "登录", description = "登录成功后返回AccessToken, 可以从返回值获取到也可以从响应头中获取到," +
  40. "后续的请求需要添加请求头 'access-token'或者放在参数里")
  41. @Parameter(name = "username", description = "用户名", required = true)
  42. @Parameter(name = "password", description = "密码(32位md5加密)", required = true)
  43. public LoginUser login(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, @RequestParam String password){
  44. LoginUser user;
  45. try {
  46. user = SecurityUtils.login(username, password, authenticationManager);
  47. } catch (AuthenticationException e) {
  48. throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
  49. }
  50. if (user == null) {
  51. throw new ControllerException(ErrorCode.ERROR100.getCode(), "用户名或密码错误");
  52. }else {
  53. String jwt = JwtUtils.createToken(username, password, user.getRole().getId());
  54. response.setHeader(JwtUtils.getHeader(), jwt);
  55. user.setAccessToken(jwt);
  56. }
  57. return user;
  58. }
  59. @PostMapping("/changePassword")
  60. @Operation(summary = "修改密码")
  61. @Parameter(name = "username", description = "用户名", required = true)
  62. @Parameter(name = "oldpassword", description = "旧密码(已md5加密的密码)", required = true)
  63. @Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
  64. public void changePassword(@RequestParam String oldPassword, @RequestParam String password){
  65. // 获取当前登录用户id
  66. LoginUser userInfo = SecurityUtils.getUserInfo();
  67. if (userInfo== null) {
  68. throw new ControllerException(ErrorCode.ERROR100);
  69. }
  70. String username = userInfo.getUsername();
  71. LoginUser user = null;
  72. try {
  73. user = SecurityUtils.login(username, oldPassword, authenticationManager);
  74. if (user == null) {
  75. throw new ControllerException(ErrorCode.ERROR100);
  76. }
  77. //int userId = SecurityUtils.getUserId();
  78. boolean result = userService.changePassword(user.getId(), DigestUtils.md5DigestAsHex(password.getBytes()));
  79. if (!result) {
  80. throw new ControllerException(ErrorCode.ERROR100);
  81. }
  82. } catch (AuthenticationException e) {
  83. throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
  84. }
  85. }
  86. @PostMapping("/add")
  87. @Operation(summary = "添加用户")
  88. @Parameter(name = "username", description = "用户名", required = true)
  89. @Parameter(name = "password", description = "密码(未md5加密的密码)", required = true)
  90. @Parameter(name = "roleId", description = "角色ID", required = true)
  91. public void add(@RequestParam String username,
  92. @RequestParam String password,
  93. @RequestParam Integer roleId){
  94. if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || roleId == null) {
  95. throw new ControllerException(ErrorCode.ERROR400.getCode(), "参数不可为空");
  96. }
  97. // 获取当前登录用户id
  98. int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
  99. if (currenRoleId != 1) {
  100. // 只用角色id为1才可以删除和添加用户
  101. throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
  102. }
  103. User user = new User();
  104. user.setUsername(username);
  105. user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
  106. //新增用户的pushKey的生成规则为md5(时间戳+用户名)
  107. user.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis()+password).getBytes()));
  108. Role role = roleService.getRoleById(roleId);
  109. if (role == null) {
  110. throw new ControllerException(ErrorCode.ERROR400.getCode(), "角色不存在");
  111. }
  112. user.setRole(role);
  113. user.setCreateTime(DateUtil.getNow());
  114. user.setUpdateTime(DateUtil.getNow());
  115. int addResult = userService.addUser(user);
  116. if (addResult <= 0) {
  117. throw new ControllerException(ErrorCode.ERROR100);
  118. }
  119. }
  120. @DeleteMapping("/delete")
  121. @Operation(summary = "删除用户")
  122. @Parameter(name = "id", description = "用户Id", required = true)
  123. public void delete(@RequestParam Integer id){
  124. // 获取当前登录用户id
  125. int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
  126. if (currenRoleId != 1) {
  127. // 只用角色id为0才可以删除和添加用户
  128. throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
  129. }
  130. int deleteResult = userService.deleteUser(id);
  131. if (deleteResult <= 0) {
  132. throw new ControllerException(ErrorCode.ERROR100);
  133. }
  134. }
  135. @GetMapping("/all")
  136. @Operation(summary = "查询用户")
  137. public List<User> all(){
  138. // 获取当前登录用户id
  139. return userService.getAllUsers();
  140. }
  141. /**
  142. * 分页查询用户
  143. *
  144. * @param page 当前页
  145. * @param count 每页查询数量
  146. * @return 分页用户列表
  147. */
  148. @GetMapping("/users")
  149. @Operation(summary = "分页查询用户")
  150. @Parameter(name = "page", description = "当前页", required = true)
  151. @Parameter(name = "count", description = "每页查询数量", required = true)
  152. public PageInfo<User> users(int page, int count) {
  153. return userService.getUsers(page, count);
  154. }
  155. @RequestMapping("/changePushKey")
  156. @Operation(summary = "修改pushkey")
  157. @Parameter(name = "userId", description = "用户Id", required = true)
  158. @Parameter(name = "pushKey", description = "新的pushKey", required = true)
  159. public void changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) {
  160. // 获取当前登录用户id
  161. int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
  162. WVPResult<String> result = new WVPResult<>();
  163. if (currenRoleId != 1) {
  164. // 只用角色id为0才可以删除和添加用户
  165. throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
  166. }
  167. int resetPushKeyResult = userService.changePushKey(userId,pushKey);
  168. if (resetPushKeyResult <= 0) {
  169. throw new ControllerException(ErrorCode.ERROR100);
  170. }
  171. }
  172. @PostMapping("/changePasswordForAdmin")
  173. @Operation(summary = "管理员修改普通用户密码")
  174. @Parameter(name = "adminId", description = "管理员id", required = true)
  175. @Parameter(name = "userId", description = "用户id", required = true)
  176. @Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
  177. public void changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
  178. // 获取当前登录用户id
  179. LoginUser userInfo = SecurityUtils.getUserInfo();
  180. if (userInfo == null) {
  181. throw new ControllerException(ErrorCode.ERROR100);
  182. }
  183. Role role = userInfo.getRole();
  184. if (role != null && role.getId() == 1) {
  185. boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
  186. if (!result) {
  187. throw new ControllerException(ErrorCode.ERROR100);
  188. }
  189. }
  190. }
  191. @PostMapping("/userInfo")
  192. @Operation(summary = "管理员修改普通用户密码")
  193. public LoginUser getUserInfo() {
  194. // 获取当前登录用户id
  195. LoginUser userInfo = SecurityUtils.getUserInfo();
  196. if (userInfo == null) {
  197. throw new ControllerException(ErrorCode.ERROR100);
  198. }
  199. User user = userService.getUser(userInfo.getUsername(), userInfo.getPassword());
  200. return new LoginUser(user, LocalDateTime.now());
  201. }
  202. }