Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
28397c5d
authored
Jul 19, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 修复nginx缓存导致串用户问题
parent
09401526
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
1 deletions
+44
-1
middleware/auth.go
+33
-0
web/src/helpers/api.js
+4
-1
web/src/helpers/utils.js
+7
-0
No files found.
middleware/auth.go
View file @
28397c5d
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"net/http"
"net/http"
"one-api/common"
"one-api/common"
"one-api/model"
"one-api/model"
"strconv"
"strings"
"strings"
)
)
...
@@ -15,6 +16,7 @@ func authHelper(c *gin.Context, minRole int) {
...
@@ -15,6 +16,7 @@ func authHelper(c *gin.Context, minRole int) {
role
:=
session
.
Get
(
"role"
)
role
:=
session
.
Get
(
"role"
)
id
:=
session
.
Get
(
"id"
)
id
:=
session
.
Get
(
"id"
)
status
:=
session
.
Get
(
"status"
)
status
:=
session
.
Get
(
"status"
)
useAccessToken
:=
false
if
username
==
nil
{
if
username
==
nil
{
// Check access token
// Check access token
accessToken
:=
c
.
Request
.
Header
.
Get
(
"Authorization"
)
accessToken
:=
c
.
Request
.
Header
.
Get
(
"Authorization"
)
...
@@ -33,6 +35,7 @@ func authHelper(c *gin.Context, minRole int) {
...
@@ -33,6 +35,7 @@ func authHelper(c *gin.Context, minRole int) {
role
=
user
.
Role
role
=
user
.
Role
id
=
user
.
Id
id
=
user
.
Id
status
=
user
.
Status
status
=
user
.
Status
useAccessToken
=
true
}
else
{
}
else
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
@@ -42,6 +45,36 @@ func authHelper(c *gin.Context, minRole int) {
...
@@ -42,6 +45,36 @@ func authHelper(c *gin.Context, minRole int) {
return
return
}
}
}
}
if
!
useAccessToken
{
// get header New-Api-User
apiUserIdStr
:=
c
.
Request
.
Header
.
Get
(
"New-Api-User"
)
if
apiUserIdStr
==
""
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"message"
:
"无权进行此操作,请刷新页面或清空缓存后重试"
,
})
c
.
Abort
()
return
}
apiUserId
,
err
:=
strconv
.
Atoi
(
apiUserIdStr
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"message"
:
"无权进行此操作,登录信息无效,请重新登录"
,
})
c
.
Abort
()
return
}
if
id
!=
apiUserId
{
c
.
JSON
(
http
.
StatusUnauthorized
,
gin
.
H
{
"success"
:
false
,
"message"
:
"无权进行此操作,与登录用户不匹配,请重新登录"
,
})
c
.
Abort
()
return
}
}
if
status
.
(
int
)
==
common
.
UserStatusDisabled
{
if
status
.
(
int
)
==
common
.
UserStatusDisabled
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"success"
:
false
,
...
...
web/src/helpers/api.js
View file @
28397c5d
import
{
showError
}
from
'./utils'
;
import
{
getUserIdFromLocalStorage
,
showError
}
from
'./utils'
;
import
axios
from
'axios'
;
import
axios
from
'axios'
;
export
const
API
=
axios
.
create
({
export
const
API
=
axios
.
create
({
baseURL
:
import
.
meta
.
env
.
VITE_REACT_APP_SERVER_URL
baseURL
:
import
.
meta
.
env
.
VITE_REACT_APP_SERVER_URL
?
import
.
meta
.
env
.
VITE_REACT_APP_SERVER_URL
?
import
.
meta
.
env
.
VITE_REACT_APP_SERVER_URL
:
''
,
:
''
,
headers
:
{
'New-API-User'
:
getUserIdFromLocalStorage
()
}
});
});
API
.
interceptors
.
response
.
use
(
API
.
interceptors
.
response
.
use
(
...
...
web/src/helpers/utils.js
View file @
28397c5d
...
@@ -33,6 +33,13 @@ export function getLogo() {
...
@@ -33,6 +33,13 @@ export function getLogo() {
return
logo
;
return
logo
;
}
}
export
function
getUserIdFromLocalStorage
()
{
let
user
=
localStorage
.
getItem
(
'user'
);
if
(
!
user
)
return
-
1
;
user
=
JSON
.
parse
(
user
);
return
user
.
id
;
}
export
function
getFooterHTML
()
{
export
function
getFooterHTML
()
{
return
localStorage
.
getItem
(
'footer_html'
);
return
localStorage
.
getItem
(
'footer_html'
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment