添加state参数验证
This commit is contained in:
@@ -5,23 +5,24 @@
|
|||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<title>zlt</title>
|
<title>zlt</title>
|
||||||
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
|
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
|
||||||
|
<script type="text/javascript" src="js/sso.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script>
|
<script>
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
//获取url参数
|
//url获取state
|
||||||
function getQueryVariable(variable) {
|
let state = getQueryVariable('state');
|
||||||
var query = window.location.search.substring(1);
|
let localState = sessionStorage.getItem("state");
|
||||||
var vars = query.split("&");
|
//判断state防止CSRF攻击
|
||||||
for (var i=0;i<vars.length;i++) {
|
if (localState !== state) {
|
||||||
var pair = vars[i].split("=");
|
alert('state参数无效!');
|
||||||
if(pair[0] == variable){return pair[1];}
|
let state = getState();
|
||||||
}
|
sessionStorage.setItem("state", state);
|
||||||
return '';
|
window.location = getAuthorizeUri(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
//url获取code
|
//url获取code
|
||||||
let code = getQueryVariable('code');
|
let code = getQueryVariable('code');
|
||||||
|
|
||||||
//获取token和用户信息
|
//获取token和用户信息
|
||||||
$.ajax({url:'http://127.0.0.1:8081/token/'+code, success:function(result) {
|
$.ajax({url:'http://127.0.0.1:8081/token/'+code, success:function(result) {
|
||||||
console.log(result);
|
console.log(result);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<title>zlt</title>
|
<title>zlt</title>
|
||||||
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
|
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
|
||||||
|
<script type="text/javascript" src="js/sso.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div>
|
<div>
|
||||||
@@ -15,11 +16,6 @@
|
|||||||
<p><input type="button" value="登出" onclick="logout()"/></p>
|
<p><input type="button" value="登出" onclick="logout()"/></p>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
//应用id
|
|
||||||
let clientId = 'app';
|
|
||||||
//授权中心地址
|
|
||||||
let uaaUri = 'http://127.0.0.1:9900/api-uaa/oauth/';
|
|
||||||
|
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
let accessToken = sessionStorage.getItem('access_token');
|
let accessToken = sessionStorage.getItem('access_token');
|
||||||
if (accessToken) {//已登录
|
if (accessToken) {//已登录
|
||||||
@@ -30,8 +26,10 @@
|
|||||||
$('#roles').html(roles);
|
$('#roles').html(roles);
|
||||||
$('#clientId').html(clientId);
|
$('#clientId').html(clientId);
|
||||||
} else {//未登录
|
} else {//未登录
|
||||||
|
let state = getState();
|
||||||
sessionStorage.setItem("visitUri", window.location.href);
|
sessionStorage.setItem("visitUri", window.location.href);
|
||||||
window.location = uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:8081/callback.html&response_type=code';
|
sessionStorage.setItem("state", state);
|
||||||
|
window.location = getAuthorizeUri(state);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,7 +38,7 @@
|
|||||||
sessionStorage.removeItem('access_token');
|
sessionStorage.removeItem('access_token');
|
||||||
sessionStorage.removeItem('username');
|
sessionStorage.removeItem('username');
|
||||||
sessionStorage.removeItem("roles");
|
sessionStorage.removeItem("roles");
|
||||||
window.location = uaaUri+'remove/token?redirect_uri=http://127.0.0.1:8081/index.html&access_token='+accessToken;
|
window.location = getLogoutUri(accessToken);
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
const FULL_CHARTER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopgrstuvwxyz';
|
||||||
|
//应用id
|
||||||
|
let clientId = 'app';
|
||||||
|
//授权中心地址
|
||||||
|
let uaaUri = 'http://127.0.0.1:9900/api-uaa/oauth/';
|
||||||
|
|
||||||
|
function getAuthorizeUri(state) {
|
||||||
|
return uaaUri+'authorize?client_id='+clientId+'&redirect_uri=http://127.0.0.1:8081/callback.html&response_type=code&state='+state;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLogoutUri(accessToken) {
|
||||||
|
return uaaUri+'remove/token?redirect_uri=http://127.0.0.1:8081/index.html&access_token='+accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getState() {
|
||||||
|
let state='';
|
||||||
|
for (let i = 0; i < 6; i++) {
|
||||||
|
state += FULL_CHARTER[Math.floor(Math.random() * 52)];
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取url参数
|
||||||
|
*/
|
||||||
|
function getQueryVariable(variable) {
|
||||||
|
var query = window.location.search.substring(1);
|
||||||
|
var vars = query.split("&");
|
||||||
|
for (var i=0;i<vars.length;i++) {
|
||||||
|
var pair = vars[i].split("=");
|
||||||
|
if(pair[0] == variable){return pair[1];}
|
||||||
|
}
|
||||||
|
return '';
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user