AuthController.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.shanghaichengdi.ghjgitem.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.shanghaichengdi.ghjgitem.constant.Constants;
  5. import com.shanghaichengdi.ghjgitem.constant.HttpStatus;
  6. import com.shanghaichengdi.ghjgitem.domain.ReturnMsg;
  7. import com.shanghaichengdi.ghjgitem.domain.SysDatashareUser;
  8. import com.shanghaichengdi.ghjgitem.service.RedisService;
  9. import com.shanghaichengdi.ghjgitem.service.SysDatashareUserService;
  10. import com.shanghaichengdi.ghjgitem.util.JwtUtils;
  11. import com.shanghaichengdi.ghjgitem.vo.request.AuthRequest;
  12. import java.util.Date;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Objects;
  17. import java.util.concurrent.TimeUnit;
  18. import javax.annotation.Resource;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.util.CollectionUtils;
  22. import org.springframework.validation.annotation.Validated;
  23. import org.springframework.web.bind.annotation.CrossOrigin;
  24. import org.springframework.web.bind.annotation.GetMapping;
  25. import org.springframework.web.bind.annotation.PathVariable;
  26. import org.springframework.web.bind.annotation.PostMapping;
  27. import org.springframework.web.bind.annotation.RequestBody;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.RestController;
  30. @RestController
  31. @CrossOrigin
  32. @RequestMapping("/auth")
  33. @Slf4j
  34. public class AuthController {
  35. @Resource
  36. private SysDatashareUserService sysDatashareUserService;
  37. @Resource
  38. private RedisService redisService;
  39. @Value("${cache.token.expire}")
  40. private Integer tokenExpire;
  41. @PostMapping("/getToken")
  42. public ReturnMsg getToken(@RequestBody @Validated AuthRequest request) {
  43. List<SysDatashareUser> datashareUserList = sysDatashareUserService.list(
  44. Wrappers.<SysDatashareUser>lambdaQuery().eq(SysDatashareUser::getKey, request.getKey())
  45. .eq(SysDatashareUser::getSecret, request.getSecret())
  46. .eq(SysDatashareUser::getIsvalid, 1));
  47. if (CollectionUtils.isEmpty(datashareUserList)) {
  48. return ReturnMsg.error(HttpStatus.NO_CONTENT, "系统中未找到该用户信息");
  49. }
  50. SysDatashareUser sysDatashareUser = datashareUserList.get(0);
  51. Map<String, Object> claims = new HashMap<>();
  52. claims.put(sysDatashareUser.getKey(), sysDatashareUser.getId());
  53. String tokenString;
  54. if (!Objects.isNull(sysDatashareUser.getToken()) && Boolean.TRUE.equals(redisService.hasKey(
  55. Constants.TOKEN + Constants.SPLIT, sysDatashareUser.getToken()))) {
  56. tokenString = sysDatashareUser.getToken();
  57. } else {
  58. tokenString = JwtUtils.generateJwt(claims, Constants.JWT_KEY);
  59. redisService.setStringCache(Constants.TOKEN + Constants.SPLIT, tokenString,
  60. JSONObject.toJSONString(sysDatashareUser), tokenExpire,
  61. TimeUnit.MINUTES);
  62. sysDatashareUser.setToken(tokenString);
  63. sysDatashareUser.setUpdateTime(new Date());
  64. sysDatashareUserService.updateById(sysDatashareUser);
  65. }
  66. return ReturnMsg.success(tokenString);
  67. }
  68. @GetMapping("/test/{token}")
  69. public ReturnMsg test(@PathVariable("token") String token) {
  70. String o = redisService.getValue(Constants.TOKEN + Constants.SPLIT, token);
  71. return ReturnMsg.success(o);
  72. }
  73. }