From ff54f169079ceb5b77d93cc60f5d2a167c6c253e Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 1 Oct 2022 20:49:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20yudao-sso-demo-by-code=20?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E8=AE=BF=E9=97=AE=E4=BB=A4=E7=89=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yudao/ssodemo/client/OAuth2Client.java | 33 +++++++++++++++++++ .../ssodemo/controller/AuthController.java | 11 +++++++ .../src/main/resources/static/index.html | 29 +++++++++++++++- .../vo/open/OAuth2OpenCheckTokenRespVO.java | 1 + .../oauth2/vo/user/OAuth2UserInfoRespVO.java | 2 +- yudao-ui-admin/src/views/sso.vue | 2 +- 6 files changed, 75 insertions(+), 3 deletions(-) diff --git a/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/client/OAuth2Client.java b/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/client/OAuth2Client.java index bdb417c41..6e179c49c 100644 --- a/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/client/OAuth2Client.java +++ b/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/client/OAuth2Client.java @@ -68,6 +68,12 @@ public class OAuth2Client { return exchange.getBody(); } + /** + * 校验访问令牌,并返回它的基本信息 + * + * @param token 访问令牌 + * @return 访问令牌的基本信息 + */ public CommonResult checkToken(String token) { // 1.1 构建请求头 HttpHeaders headers = new HttpHeaders(); @@ -88,6 +94,33 @@ public class OAuth2Client { return exchange.getBody(); } + /** + * 使用刷新令牌,获得(刷新)访问令牌 + * + * @param refreshToken 刷新令牌 + * @return 访问令牌 + */ + public CommonResult refreshToken(String refreshToken) { + // 1.1 构建请求头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + headers.set("tenant-id", TENANT_ID.toString()); + addClientHeader(headers); + // 1.2 构建请求参数 + MultiValueMap body = new LinkedMultiValueMap<>(); + body.add("grant_type", "refresh_token"); + body.add("refresh_token", refreshToken); + + // 2. 执行请求 + ResponseEntity> exchange = restTemplate.exchange( + BASE_URL + "/token", + HttpMethod.POST, + new HttpEntity<>(body, headers), + new ParameterizedTypeReference>() {}); // 解决 CommonResult 的泛型丢失 + Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功"); + return exchange.getBody(); + } + private static void addClientHeader(HttpHeaders headers) { // client 拼接,需要 BASE64 编码 String client = CLIENT_ID + ":" + CLIENT_SECRET; diff --git a/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/controller/AuthController.java b/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/controller/AuthController.java index 21807170e..4dfa3d0b5 100644 --- a/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/controller/AuthController.java +++ b/yudao-example/yudao-sso-demo-by-code/src/main/java/cn/iocoder/yudao/ssodemo/controller/AuthController.java @@ -30,4 +30,15 @@ public class AuthController { return oauth2Client.postAccessToken(code, redirectUri); } + /** + * 使用刷新令牌,获得(刷新)访问令牌 + * + * @param refreshToken 刷新令牌 + * @return 访问令牌;注意,实际项目中,最好创建对应的 ResponseVO 类,只返回必要的字段 + */ + @PostMapping("/refresh-token") + public CommonResult refreshToken(@RequestParam("refreshToken") String refreshToken) { + return oauth2Client.refreshToken(refreshToken); + } + } diff --git a/yudao-example/yudao-sso-demo-by-code/src/main/resources/static/index.html b/yudao-example/yudao-sso-demo-by-code/src/main/resources/static/index.html index 0f69e6b58..a4bfa666e 100644 --- a/yudao-example/yudao-sso-demo-by-code/src/main/resources/static/index.html +++ b/yudao-example/yudao-sso-demo-by-code/src/main/resources/static/index.html @@ -47,6 +47,33 @@ }); } + /** + * 刷新令牌 + */ + function refreshToken() { + const refreshToken = localStorage.getItem('REFRESH-TOKEN'); + if (!refreshToken) { + alert("获取不到刷新令牌"); + return; + } + $.ajax({ + url: "http://127.0.0.1:18080/auth/refresh-token?refreshToken=" + refreshToken, + method: 'POST', + success: function (result) { + if (result.code !== 0) { + alert('刷新访问令牌失败,原因:' + result.msg) + return; + } + alert('更新访问令牌成功!'); + $('#accessTokenSpan').html(result.data.access_token); + + // 设置到 localStorage 中 + localStorage.setItem('ACCESS-TOKEN', result.data.access_token); + localStorage.setItem('REFRESH-TOKEN', result.data.refresh_token); + } + }); + } + $(function () { const accessToken = localStorage.getItem('ACCESS-TOKEN'); // 情况一:未登录 @@ -86,7 +113,7 @@