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
6c742843
authored
Dec 28, 2020
by
陈森彬
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
更改为MD5加密,并从配置文件读取用户名和密码
parent
f87c8290
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
7 deletions
code/login.go
code/login.go
View file @
6c742843
package
code
import
(
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"regexp"
"sync"
)
var
userMap
=
make
(
map
[
string
]
string
)
//
var userMap = make(map[string]string)
var
sessionIdMap
=
make
(
map
[
string
]
string
)
var
configPath
string
=
"E:
\\
gotest
\\
test.json"
type
Configs
map
[
string
]
json
.
RawMessage
type
MainConfig
struct
{
}
var
conf
*
MainConfig
var
confs
Configs
var
instanceOnce
sync
.
Once
func
init
()
{
userMap
[
"weblog"
]
=
"MTIzNDU2"
//
userMap["weblog"] = "MTIzNDU2"
sessionIdMap
[
"S1oI0nPch9PzJ0ClGEODdzbqcq85JK7YwnLesuTWayU="
]
=
""
}
//初始化读取配置文件
func
Init
(
path
string
)
*
MainConfig
{
if
conf
!=
nil
&&
path
!=
configPath
{
log
.
Printf
(
"the config is already initialized, oldPath=%s, path=%s"
,
configPath
,
path
)
}
instanceOnce
.
Do
(
func
()
{
allConfigs
,
mainConfig
:=
LoadConfig
(
path
)
configPath
=
path
conf
=
mainConfig
confs
=
allConfigs
})
return
conf
}
//获取配置文件路径
func
ConfigPath
()
string
{
return
configPath
}
/**
* 验证用户
* 密码加密解密
...
...
@@ -22,15 +58,18 @@ func init() {
*/
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
{
path
:=
ConfigPath
()
//初始化配置文件
Init
(
path
)
// 密码md5加密
pwd
=
EnMD5PwdCode
(
pwd
)
if
mapPwd
,
ok
:=
confs
[
name
];
ok
{
if
CutWord
(
string
(
mapPwd
))
==
pwd
{
token
:=
GenerateRandomString
(
32
)
fmt
.
Println
(
token
)
sessionIdMap
[
token
]
=
name
return
true
,
token
}
else
{
...
...
@@ -61,6 +100,17 @@ func EnPwdCode(pwd string) string {
return
base64
.
StdEncoding
.
EncodeToString
([]
byte
(
pwd
))
}
//加密md5
func
EnMD5PwdCode
(
str
string
)
string
{
m
:=
md5
.
New
()
_
,
err
:=
io
.
WriteString
(
m
,
str
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
arr
:=
m
.
Sum
(
nil
)
return
fmt
.
Sprintf
(
"%x"
,
arr
)
}
//解密base64
func
DePwdCode
(
pwd
string
)
string
{
decoded
,
_
:=
base64
.
StdEncoding
.
DecodeString
(
pwd
)
...
...
@@ -100,3 +150,33 @@ func GenerateRandomString(s int) string {
b
,
_
:=
GenerateRandomBytes
(
s
)
return
base64
.
URLEncoding
.
EncodeToString
(
b
)
}
//去掉双引号
func
CutWord
(
s
string
)
string
{
if
len
(
s
)
>=
2
{
if
c
:=
s
[
len
(
s
)
-
1
];
s
[
0
]
==
c
&&
(
c
==
'"'
||
c
==
'\'
'
)
{
return
s
[
1
:
len
(
s
)
-
1
]
}
}
return
s
}
//从配置文件中载入json字符串
func
LoadConfig
(
path
string
)
(
Configs
,
*
MainConfig
)
{
buf
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
log
.
Panicln
(
"load config conf failed: "
,
err
)
}
mainConfig
:=
&
MainConfig
{}
err
=
json
.
Unmarshal
(
buf
,
mainConfig
)
if
err
!=
nil
{
log
.
Panicln
(
"decode config file failed:"
,
string
(
buf
),
err
)
}
allConfigs
:=
make
(
Configs
,
0
)
err
=
json
.
Unmarshal
(
buf
,
&
allConfigs
)
if
err
!=
nil
{
log
.
Panicln
(
"decode config file failed:"
,
string
(
buf
),
err
)
}
return
allConfigs
,
mainConfig
}
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