demo增加统一登出

This commit is contained in:
zlt2000
2021-11-13 10:06:47 +08:00
parent 9789a0a971
commit 9ce4f65387
9 changed files with 109 additions and 50 deletions
@@ -1,5 +1,5 @@
server:
port: 8081
port: 8082
spring:
application:
@@ -7,8 +7,8 @@ spring:
zlt:
sso:
client-id: app
client-secret: app
redirect-uri: http://127.0.0.1:8081/callback.html
client-id: webApp
client-secret: webApp
redirect-uri: http://127.0.0.1:8082/callback.html
access-token-uri: http://127.0.0.1:9900/api-uaa/oauth/token
jwt-key-uri: http://127.0.0.1:9900/api-uaa/tokens/key
@@ -24,12 +24,9 @@
let code = getQueryVariable('code');
//获取token和用户信息
$.ajax({url:'http://127.0.0.1:8081/token/'+code, success:function(result) {
$.ajax({url:'http://127.0.0.1:'+port+'/token/'+code, success:function(result) {
console.log(result);
sessionStorage.setItem('access_token', result.tokenInfo.access_token);
sessionStorage.setItem('username', result.userInfo.name);
sessionStorage.setItem('loginName', result.userInfo.loginName);
sessionStorage.setItem('picture', result.userInfo.picture);
window.location = sessionStorage.getItem('visitUri');
}});
};
@@ -17,31 +17,36 @@
<p><input type="button" value="登出" onclick="logout()"/></p>
</div>
<script>
function login() {
let state = getState();
sessionStorage.setItem("visitUri", window.location.href);
sessionStorage.setItem("state", state);
window.location = getAuthorizeUri(state);
}
window.onload = function() {
let accessToken = sessionStorage.getItem('access_token');
if (accessToken) {//已登录
let username = sessionStorage.getItem('username');
let loginName = sessionStorage.getItem("loginName");
let picture = sessionStorage.getItem("picture");
$('#accessToken').html(accessToken);
$('#userName').html(username);
$('#loginName').html(loginName);
$('#picture').html(picture);
$('#clientId').html(clientId);
//获取token和用户信息
$.ajax({url:'http://127.0.0.1:'+port+'/user?access_token='+accessToken, success:function(result) {
if (result.name) {
$('#accessToken').html(accessToken);
$('#userName').html(result.name);
$('#loginName').html(result.loginName);
$('#picture').html(result.picture);
$('#clientId').html(clientId);
} else {
login();
}
}});
} else {//未登录
let state = getState();
sessionStorage.setItem("visitUri", window.location.href);
sessionStorage.setItem("state", state);
window.location = getAuthorizeUri(state);
login();
}
};
function logout() {
let accessToken = sessionStorage.getItem('access_token');
sessionStorage.removeItem('access_token');
sessionStorage.removeItem('username');
sessionStorage.removeItem("loginName");
sessionStorage.removeItem("picture");
window.location = getLogoutUri(accessToken);
}
</script>
@@ -1,15 +1,17 @@
const FULL_CHARTER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopgrstuvwxyz';
//应用id
let clientId = 'app';
let clientId = 'webApp';
//授权中心地址
let uaaUri = 'http://127.0.0.1:9900/api-uaa/oauth/';
//端口
let port = 8082;
function getAuthorizeUri(state) {
return uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:8081/callback.html&response_type=code%20id_token&state='+state;
return uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:'+port+'/callback.html&response_type=code%20id_token&state='+state;
}
function getLogoutUri(accessToken) {
return uaaUri+'remove/token?redirect_uri=http://127.0.0.1:8081/index.html&access_token='+accessToken;
return uaaUri+'remove/token?redirect_uri=http://127.0.0.1:'+port+'/index.html&access_token='+accessToken;
}
function getState() {