提交 | 用户 | 时间
|
07073f
|
1 |
package com.iailab.module.ansteel.client; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.pojo.CommonResult; |
|
4 |
import com.iailab.module.ansteel.client.dto.oauth2.OAuth2AccessTokenRespDTO; |
|
5 |
import com.iailab.module.ansteel.client.dto.oauth2.OAuth2CheckTokenRespDTO; |
|
6 |
import org.springframework.core.ParameterizedTypeReference; |
|
7 |
import org.springframework.http.*; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
import org.springframework.util.Assert; |
|
10 |
import org.springframework.util.Base64Utils; |
|
11 |
import org.springframework.util.LinkedMultiValueMap; |
|
12 |
import org.springframework.util.MultiValueMap; |
|
13 |
import org.springframework.web.client.RestTemplate; |
|
14 |
|
|
15 |
import java.nio.charset.StandardCharsets; |
|
16 |
|
|
17 |
/** |
|
18 |
* OAuth 2.0 客户端 |
|
19 |
* |
|
20 |
* 对应调用 OAuth2OpenController 接口 |
|
21 |
*/ |
|
22 |
@Component |
|
23 |
public class OAuth2Client { |
|
24 |
|
|
25 |
private static final String BASE_URL = "http://127.0.0.1:48080/admin-api/system/oauth2"; |
|
26 |
|
|
27 |
/** |
|
28 |
* 租户编号 |
|
29 |
* |
|
30 |
* 默认使用 1;如果使用别的租户,可以调整 |
|
31 |
*/ |
|
32 |
public static final Long TENANT_ID = 1L; |
|
33 |
|
|
34 |
private static final String CLIENT_ID = "ansteel"; |
|
35 |
private static final String CLIENT_SECRET = "ansteel111111111111111"; |
|
36 |
|
|
37 |
|
|
38 |
// @Resource // 可优化,注册一个 RestTemplate Bean,然后注入 |
|
39 |
private final RestTemplate restTemplate = new RestTemplate(); |
|
40 |
|
|
41 |
/** |
|
42 |
* 使用 code 授权码,获得访问令牌 |
|
43 |
* |
|
44 |
* @param code 授权码 |
|
45 |
* @param redirectUri 重定向 URI |
|
46 |
* @return 访问令牌 |
|
47 |
*/ |
|
48 |
public CommonResult<OAuth2AccessTokenRespDTO> postAccessToken(String code, String redirectUri) { |
|
49 |
// 1.1 构建请求头 |
|
50 |
HttpHeaders headers = new HttpHeaders(); |
|
51 |
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
52 |
headers.set("tenant-id", TENANT_ID.toString()); |
|
53 |
addClientHeader(headers); |
|
54 |
// 1.2 构建请求参数 |
|
55 |
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|
56 |
body.add("grant_type", "authorization_code"); |
|
57 |
body.add("code", code); |
|
58 |
body.add("redirect_uri", redirectUri); |
|
59 |
// body.add("state", ""); // 选填;填了会校验 |
|
60 |
|
|
61 |
// 2. 执行请求 |
|
62 |
ResponseEntity<CommonResult<OAuth2AccessTokenRespDTO>> exchange = restTemplate.exchange( |
|
63 |
BASE_URL + "/token", |
|
64 |
HttpMethod.POST, |
|
65 |
new HttpEntity<>(body, headers), |
|
66 |
new ParameterizedTypeReference<CommonResult<OAuth2AccessTokenRespDTO>>() {}); // 解决 CommonResult 的泛型丢失 |
|
67 |
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); |
|
68 |
return exchange.getBody(); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 校验访问令牌,并返回它的基本信息 |
|
73 |
* |
|
74 |
* @param token 访问令牌 |
|
75 |
* @return 访问令牌的基本信息 |
|
76 |
*/ |
|
77 |
public CommonResult<OAuth2CheckTokenRespDTO> checkToken(String token) { |
|
78 |
// 1.1 构建请求头 |
|
79 |
HttpHeaders headers = new HttpHeaders(); |
|
80 |
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
81 |
headers.set("tenant-id", TENANT_ID.toString()); |
|
82 |
addClientHeader(headers); |
|
83 |
// 1.2 构建请求参数 |
|
84 |
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|
85 |
body.add("token", token); |
|
86 |
|
|
87 |
// 2. 执行请求 |
|
88 |
ResponseEntity<CommonResult<OAuth2CheckTokenRespDTO>> exchange = restTemplate.exchange( |
|
89 |
BASE_URL + "/check-token", |
|
90 |
HttpMethod.POST, |
|
91 |
new HttpEntity<>(body, headers), |
|
92 |
new ParameterizedTypeReference<CommonResult<OAuth2CheckTokenRespDTO>>() {}); // 解决 CommonResult 的泛型丢失 |
|
93 |
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); |
|
94 |
return exchange.getBody(); |
|
95 |
} |
|
96 |
|
|
97 |
/** |
|
98 |
* 使用刷新令牌,获得(刷新)访问令牌 |
|
99 |
* |
|
100 |
* @param refreshToken 刷新令牌 |
|
101 |
* @return 访问令牌 |
|
102 |
*/ |
|
103 |
public CommonResult<OAuth2AccessTokenRespDTO> refreshToken(String refreshToken) { |
|
104 |
// 1.1 构建请求头 |
|
105 |
HttpHeaders headers = new HttpHeaders(); |
|
106 |
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
107 |
headers.set("tenant-id", TENANT_ID.toString()); |
|
108 |
addClientHeader(headers); |
|
109 |
// 1.2 构建请求参数 |
|
110 |
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|
111 |
body.add("grant_type", "refresh_token"); |
|
112 |
body.add("refresh_token", refreshToken); |
|
113 |
|
|
114 |
// 2. 执行请求 |
|
115 |
ResponseEntity<CommonResult<OAuth2AccessTokenRespDTO>> exchange = restTemplate.exchange( |
|
116 |
BASE_URL + "/token", |
|
117 |
HttpMethod.POST, |
|
118 |
new HttpEntity<>(body, headers), |
|
119 |
new ParameterizedTypeReference<CommonResult<OAuth2AccessTokenRespDTO>>() {}); // 解决 CommonResult 的泛型丢失 |
|
120 |
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); |
|
121 |
return exchange.getBody(); |
|
122 |
} |
|
123 |
|
|
124 |
/** |
|
125 |
* 删除访问令牌 |
|
126 |
* |
|
127 |
* @param token 访问令牌 |
|
128 |
* @return 成功 |
|
129 |
*/ |
|
130 |
public CommonResult<Boolean> revokeToken(String token) { |
|
131 |
// 1.1 构建请求头 |
|
132 |
HttpHeaders headers = new HttpHeaders(); |
|
133 |
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
|
134 |
headers.set("tenant-id", TENANT_ID.toString()); |
|
135 |
addClientHeader(headers); |
|
136 |
// 1.2 构建请求参数 |
|
137 |
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|
138 |
body.add("token", token); |
|
139 |
|
|
140 |
// 2. 执行请求 |
|
141 |
ResponseEntity<CommonResult<Boolean>> exchange = restTemplate.exchange( |
|
142 |
BASE_URL + "/token", |
|
143 |
HttpMethod.DELETE, |
|
144 |
new HttpEntity<>(body, headers), |
|
145 |
new ParameterizedTypeReference<CommonResult<Boolean>>() {}); // 解决 CommonResult 的泛型丢失 |
|
146 |
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); |
|
147 |
return exchange.getBody(); |
|
148 |
} |
|
149 |
|
|
150 |
private static void addClientHeader(HttpHeaders headers) { |
|
151 |
// client 拼接,需要 BASE64 编码 |
|
152 |
String client = CLIENT_ID + ":" + CLIENT_SECRET; |
|
153 |
client = Base64Utils.encodeToString(client.getBytes(StandardCharsets.UTF_8)); |
|
154 |
headers.add("Authorization", "Basic " + client); |
|
155 |
} |
|
156 |
|
|
157 |
} |