选煤厂安全管理平台后台代码
houzhongjian
2024-11-22 c18e5a14aa49877a49a07ffd8f3b3917d5aca55a
提交 | 用户 | 时间
c18e5a 1 package com.iailab.module.sms.client;
H 2
3 import com.iailab.framework.common.pojo.CommonResult;
4 import com.iailab.framework.security.core.LoginUser;
5 import com.iailab.module.sms.client.dto.user.UserInfoRespDTO;
6 import com.iailab.module.sms.client.dto.user.UserUpdateReqDTO;
7 import org.springframework.core.ParameterizedTypeReference;
8 import org.springframework.http.*;
9 import org.springframework.stereotype.Component;
10 import org.springframework.util.Assert;
11 import org.springframework.util.LinkedMultiValueMap;
12 import org.springframework.util.MultiValueMap;
13 import org.springframework.web.client.RestTemplate;
14
15 import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
16
17 /**
18  * 用户 User 信息的客户端
19  *
20  * 对应调用 OAuth2UserController 接口
21  */
22 @Component
23 public class UserClient {
24
25     private static final String BASE_URL = "http://127.0.0.1:48080/admin-api/system/oauth2/user";
26
27     //    @Resource // 可优化,注册一个 RestTemplate Bean,然后注入
28     private final RestTemplate restTemplate = new RestTemplate();
29
30     public CommonResult<UserInfoRespDTO> getUser() {
31         // 1.1 构建请求头
32         HttpHeaders headers = new HttpHeaders();
33         headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
34         headers.set("tenant-id", OAuth2Client.TENANT_ID.toString());
35         addTokenHeader(headers);
36         // 1.2 构建请求参数
37         MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
38
39         // 2. 执行请求
40         ResponseEntity<CommonResult<UserInfoRespDTO>> exchange = restTemplate.exchange(
41                 BASE_URL + "/get",
42                 HttpMethod.GET,
43                 new HttpEntity<>(body, headers),
44                 new ParameterizedTypeReference<CommonResult<UserInfoRespDTO>>() {}); // 解决 CommonResult 的泛型丢失
45         Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
46         return exchange.getBody();
47     }
48
49     public CommonResult<Boolean> updateUser(UserUpdateReqDTO updateReqDTO) {
50         // 1.1 构建请求头
51         HttpHeaders headers = new HttpHeaders();
52         headers.setContentType(MediaType.APPLICATION_JSON);
53         headers.set("tenant-id", OAuth2Client.TENANT_ID.toString());
54         addTokenHeader(headers);
55         // 1.2 构建请求参数
56         // 使用 updateReqDTO 即可
57
58         // 2. 执行请求
59         ResponseEntity<CommonResult<Boolean>> exchange = restTemplate.exchange(
60                 BASE_URL + "/update",
61                 HttpMethod.PUT,
62                 new HttpEntity<>(updateReqDTO, headers),
63                 new ParameterizedTypeReference<CommonResult<Boolean>>() {}); // 解决 CommonResult 的泛型丢失
64         Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
65         return exchange.getBody();
66     }
67
68
69     private static void addTokenHeader(HttpHeaders headers) {
70         LoginUser loginUser = getLoginUser();
71         Assert.notNull(loginUser, "登录用户不能为空");
72         headers.add("Authorization", "Bearer " + loginUser.getAccessToken());
73     }
74 }