Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lichenggang
/
web_log
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5299425c
authored
Dec 24, 2020
by
lzzzzl
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
新增登录校验
parent
1ae14b4b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
106 additions
and
0 deletions
code/login.go
code/login.go
0 → 100644
View file @
5299425c
package
code
import
(
"crypto/rand"
"encoding/base64"
"fmt"
"regexp"
)
var
userMap
=
make
(
map
[
string
]
string
)
var
sessionIdMap
=
make
(
map
[
string
]
string
)
func
init
()
{
userMap
[
"zzl"
]
=
"THhANTIwbSFAQCM="
userMap
[
"lcg"
]
=
"THhANTIwbSFAQCM="
userMap
[
"zgj"
]
=
"THhANTIwbSFAQCM="
userMap
[
"ljm"
]
=
"THhANTIwbSFAQCM="
userMap
[
"opt"
]
=
"THhANTIwbSFAQCM="
sessionIdMap
[
"S1oI0nPch9PzJ0ClGEODdzbqcq85JK7YwnLesuTWayU="
]
=
""
}
/**
* 验证用户
* 密码加密解密
* 账号密码验证
*/
func
VerifyUser
(
name
string
,
pwd
string
)
(
result
bool
,
msg
string
)
{
// 先进行校验
if
!
UserNameVerify
(
name
)
||
!
UserPwdVerify
(
pwd
)
{
return
false
,
"密码或用户名格式错误"
}
// 密码加密
pwd
=
EnPwdCode
(
pwd
)
if
mapPwd
,
ok
:=
userMap
[
name
];
ok
{
if
mapPwd
==
pwd
{
token
:=
GenerateRandomString
(
32
)
fmt
.
Println
(
token
)
sessionIdMap
[
token
]
=
name
return
ok
,
"存在用户,密码正确"
}
else
{
return
!
ok
,
"存在用户,密码不正确"
}
}
else
{
return
!
ok
,
"不存在用户"
}
}
// 验证sessionID
func
VerifySessionID
(
sessionID
string
)
(
bool
,
string
)
{
if
_
,
ok
:=
sessionIdMap
[
sessionID
];
ok
{
return
ok
,
fmt
.
Sprintf
(
"sessionID: %s 存在"
,
sessionID
)
}
else
{
return
!
ok
,
fmt
.
Sprintf
(
"sessionID: %s 不存在"
,
sessionID
)
}
}
// 删除sessionID
func
DeleteSessionID
(
sessionID
string
)
(
bool
,
string
)
{
delete
(
sessionIdMap
,
sessionID
)
return
true
,
fmt
.
Sprintf
(
"sessionID: %s 已删除"
,
sessionID
)
}
//加密base64
func
EnPwdCode
(
pwd
string
)
string
{
return
base64
.
StdEncoding
.
EncodeToString
([]
byte
(
pwd
))
}
//解密base64
func
DePwdCode
(
pwd
string
)
string
{
decoded
,
_
:=
base64
.
StdEncoding
.
DecodeString
(
pwd
)
return
string
(
decoded
)
}
//用户名校验
func
UserNameVerify
(
userName
string
)
bool
{
if
ok
,
_
:=
regexp
.
MatchString
(
"^[a-zA-Z0-9]{3,16}$"
,
userName
);
!
ok
{
return
false
}
return
true
}
//密码校验
func
UserPwdVerify
(
pwd
string
)
bool
{
if
ok
,
_
:=
regexp
.
MatchString
(
"^[a-zA-Z0-9!@#]{4,16}$"
,
pwd
);
!
ok
{
return
false
}
return
true
}
//生成随机字符数组
func
GenerateRandomBytes
(
n
int
)
([]
byte
,
error
)
{
b
:=
make
([]
byte
,
n
)
_
,
err
:=
rand
.
Read
(
b
)
// Note that err == nil only if we read len(b) bytes.
if
err
!=
nil
{
return
nil
,
err
}
return
b
,
nil
}
//生成随机字符串
func
GenerateRandomString
(
s
int
)
string
{
b
,
_
:=
GenerateRandomBytes
(
s
)
return
base64
.
URLEncoding
.
EncodeToString
(
b
)
}
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