UserApiController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package me.zhengjie.modules.thirdparty.v1;
  2. import cn.hutool.http.HtmlUtil;
  3. import cn.hutool.http.HttpRequest;
  4. import cn.hutool.http.HttpUtil;
  5. import cn.hutool.http.Method;
  6. import com.alibaba.fastjson.JSONObject;
  7. import io.swagger.annotations.Api;
  8. import io.swagger.annotations.ApiOperation;
  9. import lombok.RequiredArgsConstructor;
  10. import lombok.extern.slf4j.Slf4j;
  11. import me.zhengjie.annotation.Log;
  12. import me.zhengjie.annotation.rest.AnonymousGetMapping;
  13. import me.zhengjie.annotation.rest.AnonymousPostMapping;
  14. import me.zhengjie.base.BaseResponse;
  15. import me.zhengjie.base.QueryPageParams;
  16. import me.zhengjie.exception.BadRequestException;
  17. import me.zhengjie.modules.dm.user.domain.DmUser;
  18. import me.zhengjie.modules.dm.user.service.DmUserService;
  19. import me.zhengjie.modules.dm.user.service.dto.DmUserDto;
  20. import me.zhengjie.modules.dm.user.service.dto.DmUserQueryCriteria;
  21. import me.zhengjie.modules.security.config.bean.LoginProperties;
  22. import me.zhengjie.modules.security.config.bean.SecurityProperties;
  23. import me.zhengjie.modules.security.security.TokenProvider;
  24. import me.zhengjie.modules.security.service.OnlineUserService;
  25. import me.zhengjie.modules.security.service.dto.JwtUserDto;
  26. import me.zhengjie.modules.system.service.*;
  27. import me.zhengjie.modules.system.service.dto.*;
  28. import me.zhengjie.utils.EncryptUtils;
  29. import me.zhengjie.utils.SecurityUtils;
  30. import me.zhengjie.utils.StringUtils;
  31. import org.hibernate.Criteria;
  32. import org.springframework.data.domain.PageRequest;
  33. import org.springframework.data.domain.Pageable;
  34. import org.springframework.http.HttpStatus;
  35. import org.springframework.http.ResponseEntity;
  36. import org.springframework.security.access.prepost.PreAuthorize;
  37. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  38. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  39. import org.springframework.security.core.Authentication;
  40. import org.springframework.security.core.context.SecurityContextHolder;
  41. import org.springframework.security.crypto.password.PasswordEncoder;
  42. import org.springframework.web.bind.annotation.GetMapping;
  43. import org.springframework.web.bind.annotation.RequestBody;
  44. import org.springframework.web.bind.annotation.RequestMapping;
  45. import org.springframework.web.bind.annotation.RestController;
  46. import javax.annotation.Resource;
  47. import javax.servlet.http.HttpServletRequest;
  48. import javax.servlet.http.HttpServletResponse;
  49. import java.io.IOException;
  50. import java.text.SimpleDateFormat;
  51. import java.util.*;
  52. @Slf4j
  53. @RestController
  54. @RequiredArgsConstructor
  55. @Api(tags = "用户接口")
  56. @RequestMapping("/api/thirdparty/v1/user")
  57. public class UserApiController {
  58. private final DmUserService dmUserService;
  59. private final UserService userService;
  60. private final DeptService deptService;
  61. private final JobService jobService;
  62. private final RoleService roleService;
  63. private final TokenProvider tokenProvider;
  64. private final AuthenticationManagerBuilder authenticationManagerBuilder;
  65. private final OnlineUserService onlineUserService;
  66. private final SecurityProperties properties;
  67. @Resource
  68. private LoginProperties loginProperties;
  69. private final PasswordEncoder passwordEncoder;
  70. private final String erpapiUrl = "http://10.19.0.234:30170";
  71. private final String key = "DMERPYT!@#$QWER2021+{:>";
  72. @Log("获取ERP用户数据")
  73. @ApiOperation("获取ERP用户数据")
  74. @AnonymousGetMapping(value = "/busi/comm/usky/queryUser")
  75. public ResponseEntity<Object> getusers(HttpServletRequest request, String formData) throws IOException {
  76. // SecurityUtils.CheckApiAuth(request);
  77. // List<DmUserDto> list = dmUserService.queryAll(criteria);
  78. String url = "/api-third-party/busi/comm/usky/queryUser";
  79. String res = HttpRequest.get(erpapiUrl+url)
  80. .header("X_AUTO_USER_INFO_HEAD", "{\"id\":\"anonymous\",\"tenantId\":\"caih\"}")
  81. .body("{\n" +
  82. " \"formData\":{\n" +
  83. " \"pageNum\":1,\n" +
  84. " \"pageSize\":100\n" +
  85. " },\n" +
  86. " \"product\":\"17\",\n" +
  87. " \"sign\":\"e34b2f59-0e9d-45ed-a32a-f4abd4381000\"\n" +
  88. "}").execute().body();
  89. JSONObject json = JSONObject.parseObject(res);
  90. return new ResponseEntity<>(json, HttpStatus.OK);
  91. }
  92. // @Log("单点登录")
  93. // @ApiOperation("单点登录")
  94. // @AnonymousGetMapping(value = "/sso")
  95. // public ResponseEntity<Object> sso(HttpServletRequest request) {
  96. // String token = request.getParameter("token");
  97. // String url = request.getParameter("url");
  98. //
  99. // if (StringUtils.isBlank(token) || StringUtils.isBlank(url)) {
  100. // throw new BadRequestException("参数错误");
  101. // }
  102. //
  103. // String res = HttpRequest.get(erpapiUrl + "/api-third-party/busi/comm/authLogin/api/verifyToken?token=" + token).execute().body();
  104. // JSONObject json = JSONObject.parseObject(res);
  105. //
  106. // if (json != null) {
  107. // JSONObject data = json.getJSONObject("data");
  108. //
  109. // if (data != null) {
  110. //
  111. // UserDto userDto = userService.findByName(data.getString("username"));
  112. //
  113. // if (userDto != null) {
  114. // UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(data.getString("username"), userDto.getPassword());
  115. // Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
  116. // SecurityContextHolder.getContext().setAuthentication(authentication);
  117. // // 生成令牌
  118. // String n_token = tokenProvider.createToken(authentication);
  119. // final JwtUserDto jwtUserDto = (JwtUserDto) authentication.getPrincipal();
  120. // // 保存在线信息
  121. // onlineUserService.save(jwtUserDto, n_token, request);
  122. // // 返回 token 与 用户信息
  123. // Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
  124. // put("token", properties.getTokenStartWith() + n_token);
  125. // put("user", jwtUserDto);
  126. // }};
  127. // if (loginProperties.isSingleLogin()) {
  128. // //踢掉之前已经登录的token
  129. // onlineUserService.checkLoginOnUser(data.getString("username"), n_token);
  130. // }
  131. // return ResponseEntity.ok(authInfo);
  132. // }
  133. // }
  134. // }
  135. // throw new BadRequestException("认证失败");
  136. // }
  137. @Log("单点登录")
  138. @ApiOperation("单点登录")
  139. @AnonymousGetMapping(value = "/sso11")
  140. public BaseResponse<Object> sso11(String token) throws Exception {
  141. Map<String,Object> newToken = SecurityUtils.getToken(key);
  142. UserDto userDto = userService.findByName("chaoshengqin");
  143. //组装返回值
  144. JSONObject returnData = new JSONObject();
  145. returnData.put("nonce",newToken.get("nonce"));
  146. returnData.put("timestamp",newToken.get("timestamp"));
  147. returnData.put("token",newToken.get("token"));
  148. returnData.put("userId",userDto.getId());
  149. String cipher = EncryptUtils.desEncrypt(returnData.toJSONString());
  150. returnData.put("cipher", cipher);
  151. returnData.put("user", userDto);
  152. Map<String, Object> authInfo = new HashMap<String, Object>(1) {{
  153. put("data", returnData);
  154. }};
  155. return new BaseResponse<>(authInfo);
  156. }
  157. @Log("单点登录")
  158. @ApiOperation("单点登录")
  159. @AnonymousGetMapping(value = "/sso")
  160. public BaseResponse<Object> sso(String token) throws Exception {
  161. if (StringUtils.isBlank(token)) {
  162. throw new BadRequestException("参数错误");
  163. }
  164. String res = HttpRequest.get(erpapiUrl + "/api-third-party/busi/comm/authLogin/api/verifyToken?token=" + token).method(Method.POST).execute().body();
  165. JSONObject json = JSONObject.parseObject(res);
  166. if (json != null) {
  167. JSONObject data = json.getJSONObject("data");
  168. if (data != null) {
  169. UserDto userDto = userService.findByName(data.getString("username"));
  170. if (userDto != null) {
  171. Map<String,Object> newToken = SecurityUtils.getToken(key);
  172. //组装返回值
  173. JSONObject returnData = new JSONObject();
  174. returnData.put("nonce",newToken.get("nonce"));
  175. returnData.put("timestamp",newToken.get("timestamp"));
  176. returnData.put("token",newToken.get("token"));
  177. returnData.put("userId",userDto.getId());
  178. String cipher = EncryptUtils.desEncrypt(returnData.toJSONString());
  179. returnData.put("cipher", cipher);
  180. returnData.put("user", userDto);
  181. Map<String, Object> authInfo = new HashMap<String, Object>(1) {{
  182. put("data", returnData);
  183. }};
  184. return new BaseResponse<>(authInfo);
  185. }
  186. }
  187. }
  188. throw new BadRequestException("认证失败");
  189. }
  190. @Log("获取用户数据")
  191. @ApiOperation("获取用户数据")
  192. @AnonymousPostMapping(value = "/getzkusers")
  193. public BaseResponse<Object> getzkusers(@RequestBody QueryPageParams<DmUserQueryCriteria> params, Pageable pageable) {
  194. SecurityUtils.CheckApiAuth(params);
  195. Map<String, Object> list = dmUserService.queryAll(params.getQuery(), pageable);
  196. return new BaseResponse<>(list);
  197. }
  198. @Log("根据密文获取用户信息")
  199. @ApiOperation("根据密文获取用户信息")
  200. @AnonymousGetMapping(value = "/getZkUserByCipher")
  201. public BaseResponse<Object> getZkUserByCipher(String cipher) throws Exception {
  202. String dataStr = EncryptUtils.desDecrypt(cipher);
  203. JSONObject data = JSONObject.parseObject(dataStr);
  204. String token = data.getString("token");
  205. log.info("token:"+token);
  206. String timestamp = data.getString("timestamp");
  207. log.info("timestamp:"+timestamp);
  208. String nonce = data.getString("nonce");
  209. log.info("nonce:"+nonce);
  210. String userId = data.getString("userId");
  211. log.info("userId:"+userId);
  212. String[] arr = {"DMERPYT!@#$QWER2021+{:>", timestamp,nonce};
  213. Arrays.sort(arr);
  214. String str = "";
  215. for (int i = 0; i < arr.length; i++) {
  216. str += arr[i];
  217. }
  218. String newtoken = EncryptUtils.sha1(str);
  219. if (!token.equals(newtoken)) {
  220. throw new BadRequestException(HttpStatus.UNAUTHORIZED, "认证失败");
  221. }
  222. DmUserDto dmUser = dmUserService.findById(userId);
  223. return new BaseResponse<>(dmUser);
  224. }
  225. @Log("获取部门数据")
  226. @ApiOperation("获取部门数据")
  227. @AnonymousPostMapping(value = "/getzkdeps")
  228. public BaseResponse<Object> getzkdeps(@RequestBody QueryPageParams<DeptQueryNoAuthCriteria> params, Pageable pageable) {
  229. SecurityUtils.CheckApiAuth(params);
  230. Map<String, Object> list = deptService.queryAll(params.getQuery(), pageable);
  231. return new BaseResponse<>(list);
  232. }
  233. @Log("获取岗位数据")
  234. @ApiOperation("获取岗位数据")
  235. @AnonymousPostMapping(value = "/getzkjobs")
  236. public BaseResponse<Object> getzkjobs(@RequestBody QueryPageParams<JobQueryCriteria> params, Pageable pageable) {
  237. SecurityUtils.CheckApiAuth(params);
  238. Map<String, Object> list = jobService.queryAll(params.getQuery(), pageable);
  239. return new BaseResponse<>(list);
  240. }
  241. @Log("获取角色数据")
  242. @ApiOperation("获取角色数据")
  243. @AnonymousPostMapping(value = "/getzkroles")
  244. public BaseResponse<Object> getzkroles(@RequestBody QueryPageParams<RoleQueryCriteria> params, Pageable pageable) {
  245. SecurityUtils.CheckApiAuth(params);
  246. Map<String, Object> list = roleService.queryAll(params.getQuery(), pageable);
  247. return new BaseResponse<>(list);
  248. }
  249. @Log("修改人员数据")
  250. @ApiOperation("修改人员数据")
  251. @AnonymousPostMapping(value = "/updateZkUsers")
  252. public BaseResponse<Object> updateZkUsers(@RequestBody QueryPageParams<DmUser> params) {
  253. SecurityUtils.CheckApiAuth(params);
  254. System.out.println("params:"+params.getQuery().getId());
  255. dmUserService.update(params.getQuery());
  256. return new BaseResponse<>(null);
  257. }
  258. @Log("获取系统用户数据")
  259. @ApiOperation("获取系统用户数据")
  260. @AnonymousPostMapping(value = "/getZkSysUsers")
  261. public BaseResponse<Object> getZkSysUsers(@RequestBody QueryPageParams<UserQueryCriteria> params, Pageable pageable) {
  262. SecurityUtils.CheckApiAuth(params);
  263. return new BaseResponse<>(userService.queryAll(params.getQuery(), pageable));
  264. }
  265. }