Skip to content

Commit 2fe2216

Browse files
committed
feat: doc auth
1 parent 9750c6d commit 2fe2216

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# coding=utf-8
2+
"""
3+
@project: maxkb
4+
@Author:虎
5+
@file: static_headers_middleware.py
6+
@date:2024/3/13 18:26
7+
@desc:
8+
"""
9+
from django.http import HttpResponse
10+
from django.utils.deprecation import MiddlewareMixin
11+
12+
content = """
13+
<!doctype html>
14+
<html lang="en">
15+
<head>
16+
<meta charset="UTF-8" />
17+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
18+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
19+
<title>Document</title>
20+
<script>
21+
window.onload = () => {
22+
var xhr = new XMLHttpRequest()
23+
xhr.open('GET', '/api/user', true)
24+
25+
xhr.setRequestHeader('Content-Type', 'application/json')
26+
const token = localStorage.getItem('token')
27+
const pathname = window.location.pathname
28+
if (token) {
29+
xhr.setRequestHeader('Authorization', token)
30+
xhr.onreadystatechange = function () {
31+
if (xhr.readyState === 4) {
32+
if (xhr.status === 200) {
33+
window.location.href = pathname
34+
}
35+
if (xhr.status === 401) {
36+
window.location.href = '/ui/login'
37+
}
38+
}
39+
}
40+
41+
xhr.send()
42+
} else {
43+
window.location.href = '/ui/login'
44+
}
45+
}
46+
</script>
47+
</head>
48+
<body></body>
49+
</html>
50+
51+
"""
52+
53+
54+
class DocHeadersMiddleware(MiddlewareMixin):
55+
def process_response(self, request, response):
56+
if request.path.startswith('/doc/') or request.path.startswith('/doc/chat/'):
57+
HTTP_REFERER = request.META.get('HTTP_REFERER')
58+
if HTTP_REFERER is None:
59+
return HttpResponse(content)
60+
if HTTP_REFERER == request._current_scheme_host + request.path:
61+
return response
62+
return response

apps/smartdoc/settings/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
'django.contrib.messages.middleware.MessageMiddleware',
5858
'common.middleware.gzip.GZipMiddleware',
5959
'common.middleware.static_headers_middleware.StaticHeadersMiddleware',
60-
'common.middleware.cross_domain_middleware.CrossDomainMiddleware'
61-
60+
'common.middleware.cross_domain_middleware.CrossDomainMiddleware',
61+
'common.middleware.doc_headers_middleware.DocHeadersMiddleware'
6262
]
6363

6464
JWT_AUTH = {

ui/vite.config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ const envDir = './env'
99
// https://vitejs.dev/config/
1010
export default defineConfig(({ mode }) => {
1111
const ENV = loadEnv(mode, envDir)
12-
const prefix = process.env.VITE_DYNAMIC_PREFIX || ENV.VITE_BASE_PATH;
12+
const prefix = process.env.VITE_DYNAMIC_PREFIX || ENV.VITE_BASE_PATH
1313
const proxyConf: Record<string, string | ProxyOptions> = {}
1414
proxyConf['/api'] = {
1515
target: 'http://127.0.0.1:8080',
1616
changeOrigin: true,
1717
rewrite: (path) => path.replace(ENV.VITE_BASE_PATH, '/')
1818
}
19+
proxyConf['/doc'] = {
20+
target: 'http://127.0.0.1:8080',
21+
changeOrigin: true,
22+
rewrite: (path) => path.replace(ENV.VITE_BASE_PATH, '/')
23+
}
24+
proxyConf['/static'] = {
25+
target: 'http://127.0.0.1:8080',
26+
changeOrigin: true,
27+
rewrite: (path) => path.replace(ENV.VITE_BASE_PATH, '/')
28+
}
1929
return {
2030
preflight: false,
2131
lintOnSave: false,

0 commit comments

Comments
 (0)