AuthController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.shanghaichengdi.ghjgitem.controller;
  2. import cn.hutool.core.util.IdUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.shanghaichengdi.ghjgitem.constant.Constants;
  7. import com.shanghaichengdi.ghjgitem.constant.HttpStatus;
  8. import com.shanghaichengdi.ghjgitem.domain.SysDataShareUser;
  9. import com.shanghaichengdi.ghjgitem.domain.TransportSgDwNzxContainers;
  10. import com.shanghaichengdi.ghjgitem.enums.OutsideInterface;
  11. import com.shanghaichengdi.ghjgitem.service.RedisService;
  12. import com.shanghaichengdi.ghjgitem.service.SysDatashareServiceNewService;
  13. import com.shanghaichengdi.ghjgitem.service.SysDatashareUserService;
  14. import com.shanghaichengdi.ghjgitem.util.ApiUtils;
  15. import com.shanghaichengdi.ghjgitem.util.Encodes;
  16. import com.shanghaichengdi.ghjgitem.util.JwtUtils;
  17. import com.shanghaichengdi.ghjgitem.vo.request.AuthRequest;
  18. import com.shanghaichengdi.ghjgitem.vo.request.DecryptReq;
  19. import com.shanghaichengdi.ghjgitem.vo.request.FrequencyCacheReq;
  20. import com.shanghaichengdi.ghjgitem.vo.response.ReturnMsg;
  21. import java.math.BigDecimal;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Objects;
  28. import java.util.concurrent.TimeUnit;
  29. import javax.annotation.Resource;
  30. import lombok.extern.slf4j.Slf4j;
  31. import org.springframework.beans.factory.annotation.Value;
  32. import org.springframework.util.CollectionUtils;
  33. import org.springframework.validation.annotation.Validated;
  34. import org.springframework.web.bind.annotation.CrossOrigin;
  35. import org.springframework.web.bind.annotation.GetMapping;
  36. import org.springframework.web.bind.annotation.PathVariable;
  37. import org.springframework.web.bind.annotation.PostMapping;
  38. import org.springframework.web.bind.annotation.RequestBody;
  39. import org.springframework.web.bind.annotation.RequestMapping;
  40. import org.springframework.web.bind.annotation.RestController;
  41. @RestController
  42. @CrossOrigin
  43. @RequestMapping("/auth")
  44. @Slf4j
  45. public class AuthController {
  46. @Resource
  47. private SysDatashareUserService sysDatashareUserService;
  48. @Resource
  49. private SysDatashareServiceNewService datashareServiceNewService;
  50. @Resource
  51. private RedisService redisService;
  52. @Value("${cache.token.expire}")
  53. private Integer tokenExpire;
  54. @PostMapping("/getToken")
  55. public ReturnMsg getToken(@RequestBody @Validated AuthRequest request) {
  56. List<SysDataShareUser> datashareUserList = sysDatashareUserService.list(
  57. Wrappers.<SysDataShareUser>lambdaQuery().eq(SysDataShareUser::getKey, request.getKey())
  58. .eq(SysDataShareUser::getSecret, request.getSecret())
  59. .eq(SysDataShareUser::getIsvalid, 1));
  60. if (CollectionUtils.isEmpty(datashareUserList)) {
  61. return ReturnMsg.error(HttpStatus.NO_CONTENT, "系统中未找到该用户信息");
  62. }
  63. SysDataShareUser sysDatashareUser = datashareUserList.get(0);
  64. Map<String, Object> claims = new HashMap<>();
  65. claims.put(sysDatashareUser.getKey(), sysDatashareUser.getId());
  66. String tokenString;
  67. if (!Objects.isNull(sysDatashareUser.getToken()) && Boolean.TRUE.equals(redisService.hasKey(
  68. Constants.TOKEN + Constants.SPLIT, sysDatashareUser.getToken()))) {
  69. tokenString = sysDatashareUser.getToken();
  70. } else {
  71. tokenString = JwtUtils.generateJwt(claims, Constants.JWT_KEY);
  72. redisService.setStringCache(Constants.TOKEN + Constants.SPLIT, tokenString,
  73. JSONObject.toJSONString(sysDatashareUser), tokenExpire,
  74. TimeUnit.MINUTES);
  75. sysDatashareUser.setToken(tokenString);
  76. sysDatashareUser.setUpdateTime(new Date());
  77. sysDatashareUserService.updateById(sysDatashareUser);
  78. }
  79. return ReturnMsg.success(tokenString);
  80. }
  81. @GetMapping("/checkToken/{tokenString}")
  82. public ReturnMsg checkToken(@PathVariable("tokenString") String tokenString) {
  83. Objects.requireNonNull(tokenString, "tokenString不可为空");
  84. Map<String, String> map = new HashMap<>();
  85. map.put("token", tokenString);
  86. String result = ApiUtils.api(map, OutsideInterface.AUTH_URL.getUrlString(), "checkToken");
  87. return ReturnMsg.success(result);
  88. }
  89. @PostMapping("/refresh/frequency")
  90. public ReturnMsg refreshFrequency(@RequestBody FrequencyCacheReq req) {
  91. datashareServiceNewService.refreshFrequency(req.getClientList());
  92. return ReturnMsg.success("缓存刷新成功");
  93. }
  94. @GetMapping("/test/{token}")
  95. public ReturnMsg test(@PathVariable("token") String token) {
  96. String o = redisService.getValue(Constants.TOKEN + Constants.SPLIT, token);
  97. return ReturnMsg.success(o);
  98. }
  99. @PostMapping("/encrypt")
  100. public ReturnMsg encrypt(@RequestBody Map<String,Object> req) {
  101. JSONObject jsonObject = new JSONObject(req);
  102. String mvData = Encodes.encodeBase64(
  103. Encodes.aesEncrypt(jsonObject.toString().getBytes(StandardCharsets.UTF_8),
  104. "ghThreeDataShare".getBytes(),
  105. "ghThreeDataShare".getBytes()));
  106. return ReturnMsg.success(mvData);
  107. }
  108. @PostMapping("/encryptArray")
  109. public ReturnMsg encrypt(@RequestBody String req) {
  110. JSONArray jsonObject = JSONArray.parseArray(req);
  111. String mvData = Encodes.encodeBase64(
  112. Encodes.aesEncrypt(jsonObject.toString().getBytes(StandardCharsets.UTF_8),
  113. "ghThreeDataShare".getBytes(),
  114. "ghThreeDataShare".getBytes()));
  115. return ReturnMsg.success(mvData);
  116. }
  117. @PostMapping("/decrypt")
  118. public ReturnMsg decrypt(@RequestBody DecryptReq req) {
  119. String result = new String(Encodes.aesDecrypt(
  120. Encodes.decodeBase64(req.getEncryptData()),
  121. "ghThreeDataShare".getBytes(),
  122. "ghThreeDataShare".getBytes()), StandardCharsets.UTF_8);
  123. return ReturnMsg.success(result);
  124. }
  125. }