当前位置:网站首页>Generating access keys using JSON webtoken
Generating access keys using JSON webtoken
2022-04-23 17:15:00 【MiMenge】
Back end
const {
response } = require('express');
const express = require('express');
const fs = require('fs');
const jwt = require('jsonwebtoken');
const app = express();
app.use(express.urlencoded({
extended: false }));
app.use(express.json());
app.use('*', (request, response, next) => {
// // Gets the of the header token
// let token = request.headers.authorization;
// // check token
// jwt.verify(token, 'MiMenge001018', (err, data) => {
// if (err && err.message === 'invalid token') {
// return response.json({ message: ' Invalid token', code: 0 });
// } else if (err && err.message === 'jwt expired') {
// return response.json({ message: ' invalid token', code: 0 })
// }
// });
next();
});
app.get('/', (request, response) => {
fs.readFile('./public/index.html', (err, data) => {
if (err) {
response.json({
message: ' Page resource not found ', code: 0 });
} else {
response.send(data.toString());
}
});
});
app.get('/demo', (request, response) => {
fs.readFile('./public/demo.html', (err, data) => {
if (err) {
response.json({
message: ' Page resource not found ', code: 0 });
} else {
response.send(data.toString());
}
});
});
app.post('/login', (request, response) => {
let {
uname, password } = request.body;
if (uname === 'Tom' && password === '1234') {
let token = jwt.sign({
uname: 'MiMenge',
iSRoot: true
}, 'MiMenge001018', {
expiresIn: 60
});
response.json({
message: 'OK', code: 0, token, });
}
});
app.post('/data', (request, response) => {
let token = request.headers.authorization;
jwt.verify(token, 'MiMenge001018', (err, data) => {
if (err) {
if (err.message === 'invalid token') {
return response.json({
message: ' Invalid token', code: 0 });
} else {
return response.json({
message: ' invalid token', code: 0 })
}
} else {
response.json({
message: ' success ', code: 1 });
}
})
});
app.listen(3300, err => {
if (err) {
console.error(err);
return;
}
console.log('success');
});
front end
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href=""> obtain </a>
<script> let a = document.querySelector('a'); a.addEventListener('click', (e) => {
e.preventDefault(); let token = localStorage.getItem('token'); let headers = new Headers({
'Authorization': token }); fetch('http://localhost:3300/data', {
method: 'post', headers }).then(data => data.text()).then(value => console.log(value)); // // Create request object // let xml = new XMLHttpRequest(); // // Initialization request // xml.open('post', 'http://localhost:3300/data', true); // // Set request header // xml.setRequestHeader('Content-Type', 'application/json;charset=utf8'); // // console.log(JSON.stringify(new AddO(uname, pwd))); // // Send request data // xml.send(JSON.stringify({ 'token': token })); // // Set request delay // xml.timeout = 3000; // xml.addEventListener('timeout', () => {
// xml.abort(); // }); // xml.addEventListener('error', () => {
// alert(' Wrong request ') // }); // xml.addEventListener('readystatechange', () => {
// if (xml.readyState === 4) {
// if (xml.status >= 200 && xml.status < 300 || xml.status === 304) {
// console.log(xml.responseText); // // console.log(xml.responseURL); // // console.log(xml.status); // // console.log(xml.statusText); // } // } // }); }); </script>
</body>
</html>
版权声明
本文为[MiMenge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553027505.html
边栏推荐
- Use between nodejs modules
- Using quartz under. Net core -- a simple trigger of [7] operation and trigger
- Clickhouse table engine
- Use of shell awk command
- MySQL modify master database
- JSON deserialize anonymous array / object
- Shell脚本——Shell编程规范及变量
- Devexpress GridView add select all columns
- XTask与Kotlin Coroutine的使用对比
- Conversion between hexadecimal numbers
猜你喜欢
Webapi + form form upload file
Devexpress GridView add select all columns
Deep understanding of control inversion and dependency injection
JS, entries(), keys(), values(), some(), object Assign() traversal array usage
1-4 configuration executable script of nodejs installation
RPC核心概念理解
1-1 NodeJS
On lambda powertools typescript
oracle 中快速获取表的列名列表
Nacos + aspnetcore + Ocelot actual combat code
随机推荐
Milvus 2.0 质量保障系统详解
El date picker limits the selection range from the current time to two months ago
Paging SQL
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
Using quartz under. Net core - [1] quick start
Website_ Collection
Abnormal resolution of Xiaomi camera
2.Electron之HelloWorld
ClickHouse-表引擎
Decimal format decimal / datetime conversion processing
Lock锁
Milvus 2.0 détails du système d'assurance de la qualité
C# Task. Delay and thread The difference between sleep
JS failed to change all variables and changed to the return method. Finally, the problem was solved
Feign report 400 processing
Net standard
Collection of common SQL statements
. net cross platform principle (Part I)
Baidu Map 3D rotation and tilt angle adjustment
[markdown notes]