65 lines
2.1 KiB
JavaScript
65 lines
2.1 KiB
JavaScript
// 自动设置CSRF令牌
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
// 或者从cookie中获取(如果使用CookieCsrfTokenRepository)
|
||
const csrfCookie = document.cookie.split('; ')
|
||
.find(row => row.startsWith('XSRF-TOKEN='));
|
||
if (csrfCookie) {
|
||
const token = decodeURIComponent(csrfCookie.split('=')[1]);
|
||
document.getElementById('csrfToken').value = token;
|
||
}
|
||
});
|
||
if (top != self) {
|
||
parent.location.href = '/digitalSignage/login.html';
|
||
}
|
||
var token = localStorage.getItem("token");
|
||
if (token != null && token.trim().length != 0) {
|
||
$.ajax({
|
||
type: 'get',
|
||
url: ctxPath + '/users/current?token=' + token,
|
||
success: function (data) {
|
||
location.href = ctxPath + '/index.html';
|
||
},
|
||
error: function (xhr, textStatus, errorThrown) {
|
||
console.log(xhr);
|
||
var msg = xhr.responseText;
|
||
var response = JSON.parse(msg);
|
||
var code = response.code;
|
||
var message = response.message;
|
||
if (code == 401) {
|
||
localStorage.removeItem("token");
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
function login(obj) {
|
||
$(obj).attr("disabled", true);
|
||
|
||
var username = $.trim($('#username').val());
|
||
var password = $.trim($('#password').val());
|
||
if (username == "" || password == "") {
|
||
$("#info").html('用户名或者密码不能为空');
|
||
$(obj).attr("disabled", false);
|
||
} else {
|
||
$.ajax({
|
||
type: 'post',
|
||
url: ctxPath + '/login',
|
||
data: {
|
||
username: encryptCBC(username),
|
||
password: encryptCBC(password),
|
||
_csrf: $('#csrfToken').val()
|
||
},
|
||
success: function (data) {
|
||
localStorage.setItem("token", data.token);
|
||
location.href = ctxPath + '/index.html';
|
||
},
|
||
error: function (xhr, textStatus, errorThrown) {
|
||
var msg = xhr.responseText;
|
||
var response = JSON.parse(msg);
|
||
$("#info").html(response.message);
|
||
$(obj).attr("disabled", false);
|
||
}
|
||
});
|
||
|
||
}
|
||
} |