mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
优化 yudao-sso-demo-by-code 的代码
This commit is contained in:
parent
f634ecf7c3
commit
6be30c0887
@ -52,7 +52,7 @@ public class AuthController {
|
|||||||
*/
|
*/
|
||||||
@PostMapping("/logout")
|
@PostMapping("/logout")
|
||||||
public CommonResult<Boolean> logout(HttpServletRequest request) {
|
public CommonResult<Boolean> logout(HttpServletRequest request) {
|
||||||
String token = SecurityUtils.obtainAuthorization(request, "Authentication");
|
String token = SecurityUtils.obtainAuthorization(request, "Authorization");
|
||||||
if (StrUtil.isNotBlank(token)) {
|
if (StrUtil.isNotBlank(token)) {
|
||||||
return oauth2Client.revokeToken(token);
|
return oauth2Client.revokeToken(token);
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||||
FilterChain filterChain) throws ServletException, IOException {
|
FilterChain filterChain) throws ServletException, IOException {
|
||||||
// 1. 获得访问令牌
|
// 1. 获得访问令牌
|
||||||
String token = SecurityUtils.obtainAuthorization(request, "Authentication");
|
String token = SecurityUtils.obtainAuthorization(request, "Authorization");
|
||||||
if (StringUtils.hasText(token)) {
|
if (StringUtils.hasText(token)) {
|
||||||
// 2. 基于 token 构建登录用户
|
// 2. 基于 token 构建登录用户
|
||||||
LoginUser loginUser = buildLoginUserByToken(token);
|
LoginUser loginUser = buildLoginUserByToken(token);
|
||||||
|
@ -9,8 +9,8 @@
|
|||||||
<script type="application/javascript">
|
<script type="application/javascript">
|
||||||
(function ($) {
|
(function ($) {
|
||||||
/**
|
/**
|
||||||
* 获得 URL 的指定参数的值
|
* 获得 URL 的指定参数的值
|
||||||
*
|
*
|
||||||
* @param name 参数名
|
* @param name 参数名
|
||||||
* @returns 参数值
|
* @returns 参数值
|
||||||
*/
|
*/
|
||||||
@ -25,17 +25,17 @@
|
|||||||
<script type="application/javascript">
|
<script type="application/javascript">
|
||||||
$(function () {
|
$(function () {
|
||||||
// 获得 code 授权码
|
// 获得 code 授权码
|
||||||
const code = $.getUrlParam('code');
|
const code = $.getUrlParam('code');
|
||||||
if (!code) {
|
if (!code) {
|
||||||
alert('获取不到 code 参数,请排查!')
|
alert('获取不到 code 参数,请排查!')
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提交
|
// 提交
|
||||||
const redirectUri = 'http://127.0.0.1:18080/callback.html'; // 需要修改成,你回调的地址,就是在 index.html 拼接的 redirectUri
|
const redirectUri = 'http://127.0.0.1:18080/callback.html'; // 需要修改成,你回调的地址,就是在 index.html 拼接的 redirectUri
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "http://127.0.0.1:18080/auth/login-by-code?code=" + code
|
url: "http://127.0.0.1:18080/auth/login-by-code?code=" + code
|
||||||
+ '&redirectUri=' + redirectUri,
|
+ '&redirectUri=' + redirectUri,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
success: function( result ) {
|
success: function( result ) {
|
||||||
if (result.code !== 0) {
|
if (result.code !== 0) {
|
||||||
@ -52,7 +52,7 @@
|
|||||||
window.location.href = '/index.html';
|
window.location.href = '/index.html';
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
@ -8,20 +8,20 @@
|
|||||||
|
|
||||||
<script type="application/javascript">
|
<script type="application/javascript">
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 跳转单点登录
|
* 跳转单点登录
|
||||||
*/
|
*/
|
||||||
function ssoLogin() {
|
function ssoLogin() {
|
||||||
const clientId = 'yudao-sso-demo-by-code'; // 可以改写成,你的 clientId
|
const clientId = 'yudao-sso-demo-by-code'; // 可以改写成,你的 clientId
|
||||||
const redirectUri = encodeURIComponent('http://127.0.0.1:18080/callback.html'); // 注意,需要使用 encodeURIComponent 编码地址
|
const redirectUri = encodeURIComponent('http://127.0.0.1:18080/callback.html'); // 注意,需要使用 encodeURIComponent 编码地址
|
||||||
const responseType = 'code'; // 1)授权码模式,对应 code;2)简化模式,对应 token
|
const responseType = 'code'; // 1)授权码模式,对应 code;2)简化模式,对应 token
|
||||||
window.location.href = 'http://127.0.0.1:1024/sso?client_id=' + clientId
|
window.location.href = 'http://127.0.0.1:1024/sso?client_id=' + clientId
|
||||||
+ '&redirect_uri=' + redirectUri
|
+ '&redirect_uri=' + redirectUri
|
||||||
+ '&response_type=' + responseType;
|
+ '&response_type=' + responseType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改昵称
|
* 修改昵称
|
||||||
*/
|
*/
|
||||||
function updateNickname() {
|
function updateNickname() {
|
||||||
const nickname = prompt("请输入新的昵称", "");
|
const nickname = prompt("请输入新的昵称", "");
|
||||||
@ -34,7 +34,7 @@
|
|||||||
url: "http://127.0.0.1:18080/user/update?nickname=" + nickname,
|
url: "http://127.0.0.1:18080/user/update?nickname=" + nickname,
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
'Authentication': 'Bearer ' + accessToken
|
'Authorization': 'Bearer ' + accessToken
|
||||||
},
|
},
|
||||||
success: function (result) {
|
success: function (result) {
|
||||||
if (result.code !== 0) {
|
if (result.code !== 0) {
|
||||||
@ -45,17 +45,17 @@
|
|||||||
$('#nicknameSpan').html(nickname);
|
$('#nicknameSpan').html(nickname);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 刷新令牌
|
* 刷新令牌
|
||||||
*/
|
*/
|
||||||
function refreshToken() {
|
function refreshToken() {
|
||||||
const refreshToken = localStorage.getItem('REFRESH-TOKEN');
|
const refreshToken = localStorage.getItem('REFRESH-TOKEN');
|
||||||
if (!refreshToken) {
|
if (!refreshToken) {
|
||||||
alert("获取不到刷新令牌");
|
alert("获取不到刷新令牌");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "http://127.0.0.1:18080/auth/refresh-token?refreshToken=" + refreshToken,
|
url: "http://127.0.0.1:18080/auth/refresh-token?refreshToken=" + refreshToken,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -72,7 +72,7 @@
|
|||||||
localStorage.setItem('REFRESH-TOKEN', result.data.refresh_token);
|
localStorage.setItem('REFRESH-TOKEN', result.data.refresh_token);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 刷新令牌
|
* 刷新令牌
|
||||||
@ -87,7 +87,7 @@
|
|||||||
url: "http://127.0.0.1:18080/auth/logout",
|
url: "http://127.0.0.1:18080/auth/logout",
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Authentication': 'Bearer ' + accessToken
|
'Authorization': 'Bearer ' + accessToken
|
||||||
},
|
},
|
||||||
success: function (result) {
|
success: function (result) {
|
||||||
if (result.code !== 0) {
|
if (result.code !== 0) {
|
||||||
@ -107,10 +107,10 @@
|
|||||||
$(function () {
|
$(function () {
|
||||||
const accessToken = localStorage.getItem('ACCESS-TOKEN');
|
const accessToken = localStorage.getItem('ACCESS-TOKEN');
|
||||||
// 情况一:未登录
|
// 情况一:未登录
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
$('#noLoginDiv').css("display", "block");
|
$('#noLoginDiv').css("display", "block");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 情况二:已登录
|
// 情况二:已登录
|
||||||
$('#yesLoginDiv').css("display", "block");
|
$('#yesLoginDiv').css("display", "block");
|
||||||
@ -120,7 +120,7 @@
|
|||||||
url: "http://127.0.0.1:18080/user/get",
|
url: "http://127.0.0.1:18080/user/get",
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Authentication': 'Bearer ' + accessToken
|
'Authorization': 'Bearer ' + accessToken
|
||||||
},
|
},
|
||||||
success: function (result) {
|
success: function (result) {
|
||||||
if (result.code !== 0) {
|
if (result.code !== 0) {
|
||||||
@ -134,17 +134,17 @@
|
|||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<!-- 情况一:未登录:1)跳转 ruoyi-vue-pro 的 SSO 登录页 -->
|
<!-- 情况一:未登录:1)跳转 ruoyi-vue-pro 的 SSO 登录页 -->
|
||||||
<div id="noLoginDiv" style="display: none">
|
<div id="noLoginDiv" style="display: none">
|
||||||
您未登录,点击 <a href="#" onclick="ssoLogin()">跳转 </a> SSO 单点登录
|
您未登录,点击 <a href="#" onclick="ssoLogin()">跳转 </a> SSO 单点登录
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- 情况二:已登录:1)展示用户信息;2)刷新访问令牌;3)退出登录 -->
|
<!-- 情况二:已登录:1)展示用户信息;2)刷新访问令牌;3)退出登录 -->
|
||||||
<div id="yesLoginDiv" style="display: none">
|
<div id="yesLoginDiv" style="display: none">
|
||||||
您已登录!<button onclick="logout()">退出登录</button> <br />
|
您已登录!<button onclick="logout()">退出登录</button> <br />
|
||||||
昵称:<span id="nicknameSpan"> 加载中... </span> <button onclick="updateNickname()">修改昵称</button> <br />
|
昵称:<span id="nicknameSpan"> 加载中... </span> <button onclick="updateNickname()">修改昵称</button> <br />
|
||||||
访问令牌:<span id="accessTokenSpan"> 加载中... </span> <button onclick="refreshToken()">刷新令牌</button> <br />
|
访问令牌:<span id="accessTokenSpan"> 加载中... </span> <button onclick="refreshToken()">刷新令牌</button> <br />
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
<style>
|
<style>
|
||||||
body { /** 页面居中 */
|
body { /** 页面居中 */
|
||||||
|
Loading…
Reference in New Issue
Block a user