IndexController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.xiaoan.web.backend;
  2. import com.xiaoan.common.constant.MsgCode;
  3. import com.xiaoan.common.util.PasswordUtils;
  4. import com.xiaoan.common.util.ResultJson;
  5. import com.xiaoan.domain.backend.UserEntity;
  6. import com.xiaoan.domain.dto.request.UserRequest;
  7. import com.xiaoan.service.backend.ResourceService;
  8. import com.xiaoan.service.backend.RoleService;
  9. import com.xiaoan.service.backend.UserService;
  10. import com.xiaoan.service.backend.dto.ResourceTree;
  11. import com.xiaoan.web.shiro.JWTUtil;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import lombok.extern.log4j.Log4j2;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.data.redis.core.RedisTemplate;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import org.springframework.web.bind.annotation.*;
  19. import javax.servlet.http.HttpServletRequest;
  20. import java.util.*;
  21. import java.util.concurrent.TimeUnit;
  22. /**
  23. * Created by owen on 2020/2/19 0019 15:53
  24. */
  25. @Api(tags = "IndexController", description = "后台登录管理")
  26. @RestController
  27. @RequestMapping("api/manage/user")
  28. @Transactional
  29. @Log4j2
  30. public class IndexController {
  31. @Autowired
  32. private UserService userService;
  33. @Autowired
  34. private RoleService roleService;
  35. @Autowired
  36. private ResourceService resourceService;
  37. @Autowired
  38. private RedisTemplate<String, String> redisTemplate;
  39. @ApiOperation("查询用户信息")
  40. @PostMapping(value = "/login")
  41. public ResultJson login(@RequestBody UserRequest param) throws Exception {
  42. log.warn("run login , userName:{}, password:{}", param.getUserName(), param.getPassword());
  43. // 1.获取用户
  44. UserEntity userEntity = userService.findByUserName(param.getUserName());
  45. if (userEntity == null){
  46. return new ResultJson(MsgCode.FAILURE_CODE_4001, MsgCode.FAILURE_MSG_4001);
  47. }
  48. // 验证密码
  49. String decryptName = PasswordUtils.decrypt(userEntity.getPassword(), param.getPassword(), PasswordUtils.getStaticSalt());
  50. if (!param.getUserName().equals(decryptName)) {
  51. return new ResultJson(MsgCode.FAILURE_CODE_4002, MsgCode.FAILURE_MSG_4002);
  52. }
  53. // 检查账号是否启用
  54. if (userEntity.getStatus() != 0) {
  55. return new ResultJson(MsgCode.FAILURE_CODE_4003, MsgCode.FAILURE_MSG_4003);
  56. }
  57. // 获取用户菜单
  58. List<ResourceTree> resourcesByUserMenu = resourceService.getResourcesTreeByUserMenu(userEntity);
  59. List<ResourceTree> resourcesTreeByUserPermission = resourceService.getResourcesTreeByUserPermission(userEntity);
  60. // 创建新token
  61. String token = JWTUtil.sign(param.getUserName(), userEntity.getPassword());
  62. log.warn("new token: {}", token);
  63. HashMap<String, Object> result = new HashMap<>();
  64. result.put("userEntity", userEntity);
  65. // result.put("resources", resourcesByUserMenu);
  66. result.put("token", token);
  67. result.put("resourcesTreeByUserPermission", resourcesTreeByUserPermission);
  68. // 更新到 redis, 有效期24h, 旧token无效
  69. redisTemplate.opsForValue().set(userEntity.getUserName(), token, Long.parseLong("24"), TimeUnit.HOURS);
  70. return new ResultJson(MsgCode.SUCCESS_CODE, result);
  71. }
  72. @GetMapping("/logout")
  73. public ResultJson logout(HttpServletRequest request) {
  74. log.info("run logout");
  75. String token = request.getHeader("Authorization");
  76. String username = JWTUtil.getUsername(token);
  77. String redisToken = (String) redisTemplate.opsForValue().get(username);
  78. // token username 一致,代表没有被踢出
  79. if (username.equals(redisToken)) {
  80. redisTemplate.delete(username);
  81. }
  82. log.info("end logout");
  83. return new ResultJson(MsgCode.SUCCESS_CODE, MsgCode.SUCCESS_MSG);
  84. }
  85. }