-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtGeneratorAndDecoder.html
More file actions
403 lines (353 loc) · 11.8 KB
/
Copy pathJwtGeneratorAndDecoder.html
File metadata and controls
403 lines (353 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JWT生成器/解码器 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.jwt-tabs {
display: flex;
margin-bottom: 15px;
border-bottom: 1px solid var(--bg-tertiary);
}
.jwt-tab {
padding: 10px 20px;
cursor: pointer;
background-color: var(--bg-secondary);
border: 1px solid var(--bg-tertiary);
border-bottom: none;
border-radius: 5px 5px 0 0;
margin-right: 5px;
}
.jwt-tab.active {
background-color: var(--accent-color);
color: white;
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
color: var(--accent-color);
}
.payload-item {
display: flex;
gap: 10px;
margin-bottom: 10px;
align-items: center;
}
.payload-item input {
flex: 1;
}
.payload-item button {
background-color: #ff6b6b;
color: white;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
}
.jwt-part {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
word-break: break-all;
margin-bottom: 10px;
font-family: monospace;
}
.jwt-part-header {
font-weight: bold;
color: var(--accent-color);
margin-bottom: 5px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
.decoded-jwt {
margin-top: 20px;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>JWT生成器/解码器</h2>
<p>生成或解码JSON Web Token</p>
<div class="info-box">
<p><strong>功能说明:</strong></p>
<p>• 生成JWT - 创建新的JSON Web Token</p>
<p>• 解码JWT - 解析现有的JWT令牌</p>
<p>• 注意:这是一个客户端工具,不会向服务器发送任何敏感信息</p>
</div>
<div class="jwt-tabs">
<div class="jwt-tab" :class="{ active: activeTab === 'generate' }" @click="activeTab = 'generate'">生成JWT</div>
<div class="jwt-tab" :class="{ active: activeTab === 'decode' }" @click="activeTab = 'decode'">解码JWT</div>
</div>
<div v-if="activeTab === 'generate'">
<div class="input-group">
<label for="secretKey">密钥</label>
<input
type="text"
id="secretKey"
v-model="secretKey"
placeholder="输入用于签名的密钥"
>
</div>
<div class="input-group">
<label>头部 (Header)</label>
<div class="jwt-part">
<div class="jwt-part-header">alg: "HS256", typ: "JWT"</div>
<pre>{{ header }}</pre>
</div>
</div>
<div class="input-group">
<label>载荷 (Payload)</label>
<div v-for="(item, index) in payloadItems" :key="index" class="payload-item">
<input type="text" v-model="item.key" placeholder="键名">
<input type="text" v-model="item.value" placeholder="值">
<button @click="removePayloadItem(index)">删除</button>
</div>
<button @click="addPayloadItem">添加键值对</button>
</div>
<div class="controls">
<button @click="generateToken">生成JWT</button>
<button @click="clearFields">清空</button>
<button @click="copyToken">复制令牌</button>
</div>
<div class="input-group" v-if="generatedToken">
<label>生成的JWT</label>
<textarea
v-model="generatedToken"
placeholder="生成的JWT将显示在这里..."
rows="4"
readonly
></textarea>
</div>
</div>
<div v-if="activeTab === 'decode'" class="decoded-jwt">
<div class="input-group">
<label for="jwtInput">JWT令牌</label>
<textarea
id="jwtInput"
v-model="jwtInput"
placeholder="粘贴JWT令牌..."
rows="4"
></textarea>
</div>
<div class="controls">
<button @click="decodeToken">解码JWT</button>
<button @click="clearDecodeFields">清空</button>
</div>
<div v-if="decodedHeader || decodedPayload" class="input-group">
<label>解码结果</label>
<div v-if="decodedHeader" class="jwt-part">
<div class="jwt-part-header">头部 (Header)</div>
<pre>{{ decodedHeader }}</pre>
</div>
<div v-if="decodedPayload" class="jwt-part">
<div class="jwt-part-header">载荷 (Payload)</div>
<pre>{{ decodedPayload }}</pre>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const activeTab = ref('generate');
const secretKey = ref('');
const jwtInput = ref('');
const generatedToken = ref('');
const decodedHeader = ref(null);
const decodedPayload = ref(null);
const payloadItems = ref([
{ key: 'sub', value: '1234567890' },
{ key: 'name', value: 'John Doe' },
{ key: 'iat', value: Math.floor(Date.now() / 1000) }
]);
const header = computed(() => {
return JSON.stringify({ alg: 'HS256', typ: 'JWT' });
});
const payload = computed(() => {
const obj = {};
payloadItems.value.forEach(item => {
if (item.key && item.value) {
// 尝试将值解析为数字或布尔值
let value = item.value;
if (value === 'true') value = true;
else if (value === 'false') value = false;
else if (!isNaN(value) && !isNaN(parseFloat(value))) value = parseFloat(value);
obj[item.key] = value;
}
});
return JSON.stringify(obj);
});
const addPayloadItem = () => {
payloadItems.value.push({ key: '', value: '' });
};
const removePayloadItem = (index) => {
if (payloadItems.value.length > 1) {
payloadItems.value.splice(index, 1);
}
};
// Base64 URL安全编码
const base64UrlEncode = (str) => {
return btoa(str)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
};
// Base64 URL安全解码
const base64UrlDecode = (str) => {
// 补齐必要的等号
str = str.replace(/-/g, '+').replace(/_/g, '/');
while (str.length % 4) {
str += '=';
}
return atob(str);
};
// 简单的HMAC-SHA256实现(仅用于演示,实际应用中应使用专业库)
const signJwt = (header, payload, secret) => {
// 仅作演示用途,实际上需要实现完整的HMAC-SHA256算法
// 这里我们简单地返回一个模拟的签名
const encodedHeader = base64UrlEncode(header);
const encodedPayload = base64UrlEncode(payload);
const signature = base64UrlEncode('fake-signature-' + encodedHeader + encodedPayload + secret);
return `${encodedHeader}.${encodedPayload}.${signature}`;
};
const generateToken = () => {
if (!secretKey.value) {
alert('请输入密钥');
return;
}
generatedToken.value = signJwt(header.value, payload.value, secretKey.value);
};
const decodeToken = () => {
if (!jwtInput.value) {
alert('请输入JWT令牌');
return;
}
const parts = jwtInput.value.split('.');
if (parts.length !== 3) {
alert('无效的JWT令牌格式');
return;
}
try {
decodedHeader.value = JSON.parse(base64UrlDecode(parts[0]));
decodedPayload.value = JSON.parse(base64UrlDecode(parts[1]));
} catch (error) {
alert('无法解码JWT: ' + error.message);
}
};
const copyToken = async () => {
if (!generatedToken.value) {
alert('没有可复制的令牌');
return;
}
try {
await navigator.clipboard.writeText(generatedToken.value);
alert('JWT已复制到剪贴板!');
} catch (err) {
console.error('复制失败:', err);
alert('复制失败,请手动复制令牌');
}
};
const clearFields = () => {
secretKey.value = '';
generatedToken.value = '';
payloadItems.value = [
{ key: 'sub', value: '1234567890' },
{ key: 'name', value: 'John Doe' },
{ key: 'iat', value: Math.floor(Date.now() / 1000) }
];
};
const clearDecodeFields = () => {
jwtInput.value = '';
decodedHeader.value = null;
decodedPayload.value = null;
};
return {
activeTab,
secretKey,
jwtInput,
generatedToken,
decodedHeader,
decodedPayload,
payloadItems,
header,
payload,
addPayloadItem,
removePayloadItem,
generateToken,
decodeToken,
copyToken,
clearFields,
clearDecodeFields
};
}
}).mount('#app');
</script>
</body>
</html>