LoginController.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package com.fdage.controller;
  2. import com.fdage.dto.ResourceTree;
  3. import com.fdage.enums.ResponEnum;
  4. import com.fdage.pojo.TbLog;
  5. import com.fdage.pojo.TbUser;
  6. import com.fdage.request.RequestUser;
  7. import com.fdage.respon.ResponUser;
  8. import com.fdage.service.ILogService;
  9. import com.fdage.service.IResourceService;
  10. import com.fdage.service.IRoleService;
  11. import com.fdage.service.IUserService;
  12. import com.fdage.shiro.JwtUtil2;
  13. import com.fdage.util.AjaxJson;
  14. import com.fdage.util.PasswordUtils;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiImplicitParam;
  17. import io.swagger.annotations.ApiImplicitParams;
  18. import io.swagger.annotations.ApiOperation;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.beans.BeanUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.data.redis.core.RedisTemplate;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.web.bind.annotation.PostMapping;
  26. import org.springframework.web.bind.annotation.RequestBody;
  27. import org.springframework.web.bind.annotation.ResponseBody;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpSession;
  30. import java.util.Date;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.concurrent.TimeUnit;
  34. /**
  35. * Created by Hb_zzZ on 2019/9/11.
  36. */
  37. @Slf4j
  38. @Controller
  39. @Api(tags = "登陆模块")
  40. public class LoginController {
  41. @Autowired
  42. private IUserService userService;
  43. @Autowired
  44. private IResourceService resourceService;
  45. @Autowired
  46. private RedisTemplate<String, String> redisTemplate;
  47. @Autowired
  48. private ILogService logService;
  49. @PostMapping("login")
  50. @ResponseBody
  51. @ApiOperation("登陆")
  52. @ApiImplicitParams({
  53. @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String"),
  54. @ApiImplicitParam(name = "password", value = "密码", dataType = "String")})
  55. public AjaxJson login(@RequestBody RequestUser bo){
  56. if(bo == null || StringUtils.isEmpty(bo.getUserName()) || StringUtils.isEmpty(bo.getPassword())){
  57. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  58. }
  59. TbUser user = userService.findByUserName(bo.getUserName());
  60. if(user == null){
  61. return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage());
  62. }
  63. String encryptPwd = PasswordUtils.encrypt(bo.getPassword(), bo.getUserName(), PasswordUtils.getStaticSalt());
  64. if(!encryptPwd.equals(user.getPassword())){
  65. return AjaxJson.failure(ResponEnum.PASSWORD_ERROR.getCode(), ResponEnum.PASSWORD_ERROR.getMessage());
  66. }
  67. // 检查账号是否启用
  68. if (user.getState() != 0) {
  69. log.error("账号已停用: {}", user.getUserName());
  70. return AjaxJson.failure(ResponEnum.USER_STOP_USING.getCode(), ResponEnum.USER_STOP_USING.getMessage());
  71. }
  72. // ResponUser result = new ResponUser();
  73. // BeanUtils.copyProperties(user, result);
  74. List<ResourceTree> resourcesTreeByUserPermission = resourceService.getResourcesAllByUserPermission(user);
  75. // log.info("获取权限 success");
  76. // 获取用户角色
  77. List<String> roles = userService.findRoleByUserId(user.getId());
  78. // log.info("获取角色 success");
  79. // 创建新token
  80. HashMap<String, Object> tokenMap = new HashMap<>();
  81. tokenMap.put("userName", user.getUserName());
  82. tokenMap.put("id", user.getId());
  83. tokenMap.put("role", roles);
  84. String token = JwtUtil2.createJWT(-1, tokenMap);
  85. HashMap<String, Object> result = new HashMap<>();
  86. result.put("user", user);
  87. result.put("token", token);
  88. result.put("permission", resourcesTreeByUserPermission);
  89. result.put("role", roles);
  90. // log.info("token create");
  91. // 更新到 redis, 有效期24h, 旧token无效
  92. redisTemplate.opsForValue().set(user.getUserName(), token, Long.parseLong("23"), TimeUnit.HOURS);
  93. TbLog logEntity = new TbLog();
  94. logEntity.setCreateTime(new Date());
  95. logEntity.setType("登陆");
  96. logEntity.setDescription("登陆系统");
  97. logEntity.setUserId(user.getId());
  98. logService.save(logEntity);
  99. return AjaxJson.success(result);
  100. }
  101. }