Commit 1c573158 by 杨子

登录功能

parent c4d8de4b
url="http://localhost:8080/loginScrt" url="http://localhost:8080/loginScrt"
securityServerUrl="127.0.0.1" securityServerUrl="http://192.168.19.189:48080"
\ No newline at end of file \ No newline at end of file
...@@ -3,12 +3,76 @@ ...@@ -3,12 +3,76 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Title</title> <title>Title</title>
<style>
body { /** 页面居中 */
border-radius: 20px;
height: 350px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
</style>
<script src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/2.1.2/jquery.min.js" type="application/javascript"></script>
</head> </head>
<body> <body>
<div>123123123123</div>
<h1>跳转地址:<span th:text="${jumpUrl}"></span></h1> <h1>跳转地址:<span th:text="${jumpUrl}"></span></h1>
<h1>登录地址:<span th:text="${securityServerUrl}"></span></h1> <h1>登录地址:<span th:text="${securityServerUrl}"></span></h1>
账号:<input id="username" value="admin" /> <br />
密码:<input id="password" value="admin123" > <br />
<button style="float: right; margin-top: 5px;" onclick="login()">登录</button>
<script type="application/javascript">
// 获取登录地址
const securityServerUrlElement = document.querySelector('h1:nth-of-type(2) span');
const securityServerUrl = securityServerUrlElement ? securityServerUrlElement.textContent : '';
console.log('登录地址: ', securityServerUrl);
// 获取跳转地址
const jumpUrlElement = document.querySelector('h1:nth-of-type(1) span');
const jumpUrl = jumpUrlElement? jumpUrlElement.textContent : '';
console.log('跳转地址: ', jumpUrl);
/**
* 账号密码登录
*/
function login() {
const clientId = 'yudao-sso-demo-by-password'; // 可以改写成,你的 clientId
const clientSecret = 'test'; // 可以改写成,你的 clientSecret
const grantType = 'password'; // 密码模式
// 账号 + 密码
const username = $('#username').val();
const password = $('#password').val();
if (username.length === 0 || password.length === 0) {
alert('账号或密码未输入');
return;
}
// 发起请求
$.ajax({
url: securityServerUrl+"/admin-api/system/auth/loginScrt",
data: JSON.stringify({
username: username,
password: password
}),
method: 'POST',
headers: {
'tenant-id': '1', // 多租户编号,写死
'Content-Type': 'application/json'
},
success: function (result) {
if (result.code !== 0) {
alert('登录失败,原因:' + result.msg)
return;
}
// 设置到 localStorage 中
localStorage.setItem('ACCESS-TOKEN', result.data.accessToken);
localStorage.setItem('REFRESH-TOKEN', result.data.refreshToken);
window.location.href = jumpUrl+'?token='+result.data.accessToken+'&refreshToken='+result.data.refreshToken;
}
});
}
</script>
</body> </body>
</html> </html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment