完成 yudao-sso-demo-by-code 刷新访问令牌

This commit is contained in:
YunaiV 2022-10-01 20:49:11 +08:00
parent ea71002ed6
commit ff54f16907
6 changed files with 75 additions and 3 deletions

View File

@ -68,6 +68,12 @@ public class OAuth2Client {
return exchange.getBody();
}
/**
* 校验访问令牌并返回它的基本信息
*
* @param token 访问令牌
* @return 访问令牌的基本信息
*/
public CommonResult<OAuth2CheckTokenRespDTO> checkToken(String token) {
// 1.1 构建请求头
HttpHeaders headers = new HttpHeaders();
@ -88,6 +94,33 @@ public class OAuth2Client {
return exchange.getBody();
}
/**
* 使用刷新令牌获得刷新访问令牌
*
* @param refreshToken 刷新令牌
* @return 访问令牌
*/
public CommonResult<OAuth2AccessTokenRespDTO> 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<String, Object> body = new LinkedMultiValueMap<>();
body.add("grant_type", "refresh_token");
body.add("refresh_token", refreshToken);
// 2. 执行请求
ResponseEntity<CommonResult<OAuth2AccessTokenRespDTO>> exchange = restTemplate.exchange(
BASE_URL + "/token",
HttpMethod.POST,
new HttpEntity<>(body, headers),
new ParameterizedTypeReference<CommonResult<OAuth2AccessTokenRespDTO>>() {}); // 解决 CommonResult 的泛型丢失
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
return exchange.getBody();
}
private static void addClientHeader(HttpHeaders headers) {
// client 拼接需要 BASE64 编码
String client = CLIENT_ID + ":" + CLIENT_SECRET;

View File

@ -30,4 +30,15 @@ public class AuthController {
return oauth2Client.postAccessToken(code, redirectUri);
}
/**
* 使用刷新令牌获得刷新访问令牌
*
* @param refreshToken 刷新令牌
* @return 访问令牌注意实际项目中最好创建对应的 ResponseVO 只返回必要的字段
*/
@PostMapping("/refresh-token")
public CommonResult<OAuth2AccessTokenRespDTO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
return oauth2Client.refreshToken(refreshToken);
}
}

View File

@ -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 @@
<div id="yesLoginDiv" style="display: none">
您已登录!<button>退出登录</button> <br />
昵称:<span id="nicknameSpan"> 加载中... </span> <button onclick="updateNickname()">修改昵称</button> <br />
访问令牌:<span id="accessTokenSpan"> 加载中... </span> <br />
访问令牌:<span id="accessTokenSpan"> 加载中... </span> <button onclick="refreshToken()">刷新令牌</button> <br />
</div>
</body>
<style>

View File

@ -26,6 +26,7 @@ public class OAuth2OpenCheckTokenRespVO {
private Long tenantId;
@ApiModelProperty(value = "客户端编号", required = true, example = "car")
@JsonProperty("client_id")
private String clientId;
@ApiModelProperty(value = "授权范围", required = true, example = "user_info")
private List<String> scopes;

View File

@ -17,7 +17,7 @@ public class OAuth2UserInfoRespVO {
@ApiModelProperty(value = "用户编号", required = true, example = "1")
private Long id;
@ApiModelProperty(value = "用户昵称", required = true, example = "芋艿")
@ApiModelProperty(value = "用户账号", required = true, example = "芋艿")
private String username;
@ApiModelProperty(value = "用户昵称", required = true, example = "芋道")

View File

@ -19,7 +19,7 @@
</el-tab-pane>
</el-tabs>
<div>
<el-form ref="loginForm" :model="loginForm" :rules="LoginRules" class="login-form">
<el-form ref="loginForm" :model="loginForm" class="login-form">
<!-- 授权范围的选择 -->
此第三方应用请求获得以下权限
<el-form-item prop="scopes">