123 lines
3.8 KiB
HTML
123 lines
3.8 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>登录测试</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
line-height: 1.6;
|
|
max-width: 600px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
}
|
|
.form-group {
|
|
margin-bottom: 15px;
|
|
}
|
|
label {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
}
|
|
input[type="text"],
|
|
input[type="password"] {
|
|
width: 100%;
|
|
padding: 8px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
}
|
|
button {
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
padding: 10px 15px;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
}
|
|
button:hover {
|
|
background-color: #45a049;
|
|
}
|
|
#result {
|
|
margin-top: 20px;
|
|
padding: 15px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
background-color: #f9f9f9;
|
|
min-height: 100px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>JWT登录测试</h1>
|
|
|
|
<div class="form-group">
|
|
<label for="username">用户名:</label>
|
|
<input type="text" id="username" value="admin">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="password">密码:</label>
|
|
<input type="password" id="password" value="123456">
|
|
</div>
|
|
|
|
<button id="loginBtn">登录</button>
|
|
<button id="infoBtn">获取用户信息</button>
|
|
|
|
<div id="result">
|
|
<p>响应结果将显示在这里</p>
|
|
</div>
|
|
|
|
<script>
|
|
document.getElementById('loginBtn').addEventListener('click', login);
|
|
document.getElementById('infoBtn').addEventListener('click', getUserInfo);
|
|
|
|
let token = '';
|
|
|
|
async function login() {
|
|
const username = document.getElementById('username').value;
|
|
const password = document.getElementById('password').value;
|
|
|
|
try {
|
|
const response = await fetch('/api/auth/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
|
|
const data = await response.json();
|
|
document.getElementById('result').innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
|
|
|
|
if (data.code === 200 && data.data.token) {
|
|
token = data.data.token;
|
|
console.log('Token stored:', token);
|
|
}
|
|
} catch (error) {
|
|
document.getElementById('result').innerHTML = `<p>Error: ${error.message}</p>`;
|
|
}
|
|
}
|
|
|
|
async function getUserInfo() {
|
|
if (!token) {
|
|
document.getElementById('result').innerHTML = '<p>请先登录获取Token</p>';
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('/api/auth/info', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
|
|
const data = await response.json();
|
|
document.getElementById('result').innerHTML = `<pre>${JSON.stringify(data, null, 2)}</pre>`;
|
|
} catch (error) {
|
|
document.getElementById('result').innerHTML = `<p>Error: ${error.message}</p>`;
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |