Skip to content

Commit

Permalink
feat: 优化安全登录
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Dec 2, 2024
1 parent 7bc716c commit fc97709
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
3 changes: 1 addition & 2 deletions internal/http/middleware/must_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func MustLogin(next http.Handler) http.Handler {
if safeLogin {
safeClientHash := cast.ToString(sess.Get("safe_client"))
ip, _, _ := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
ua := r.Header.Get("User-Agent")
clientHash := fmt.Sprintf("%x", sha3.Sum256([]byte(ip+"|"+ua)))
clientHash := fmt.Sprintf("%x", sha3.Sum256([]byte(ip)))
if safeClientHash != clientHash || safeClientHash == "" {
render := chix.NewRender(w)
render.Status(http.StatusUnauthorized)
Expand Down
3 changes: 1 addition & 2 deletions internal/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,8 @@ func (s *UserService) Login(w http.ResponseWriter, r *http.Request) {
return
}
if req.SafeLogin && !app.Conf.Bool("http.tls") {
ua := r.Header.Get("User-Agent")
sess.Put("safe_login", true)
sess.Put("safe_client", fmt.Sprintf("%x", sha3.Sum256([]byte(ip+"|"+ua))))
sess.Put("safe_client", fmt.Sprintf("%x", sha3.Sum256([]byte(ip))))
}

sess.Put("user_id", user.ID)
Expand Down
5 changes: 3 additions & 2 deletions web/src/api/panel/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ export default {
// 公钥
key: () => http.Get('/user/key'),
// 登录
login: (username: string, password: string) =>
login: (username: string, password: string, safe_login: boolean) =>
http.Post('/user/login', {
username,
password
password,
safe_login
}),
// 登出
logout: () => http.Post('/user/logout'),
Expand Down
8 changes: 6 additions & 2 deletions web/src/views/login/IndexView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const loging = ref<boolean>(false)
const isRemember = useStorage('isRemember', false)
async function handleLogin() {
const { username, password } = loginInfo.value
const { username, password, safe_login } = loginInfo.value
if (!username || !password) {
window.$message.warning('请输入用户名和密码')
return
Expand All @@ -47,7 +47,11 @@ async function handleLogin() {
}
try {
user
.login(rsaEncrypt(username, String(unref(key))), rsaEncrypt(password, String(unref(key))))
.login(
rsaEncrypt(username, String(unref(key))),
rsaEncrypt(password, String(unref(key))),
safe_login
)
.then(async () => {
loging.value = true
window.$notification?.success({ title: '登录成功!', duration: 2500 })
Expand Down

0 comments on commit fc97709

Please sign in to comment.