Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
im-microservice
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Li Feifei
im-microservice
Commits
7f257399
Commit
7f257399
authored
Jul 03, 2020
by
Li Feifei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
结构修改
parent
008a4b79
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
8 deletions
+30
-8
auth.go
middleware/auth.go
+5
-8
interceptor.go
middleware/interceptor.go
+25
-0
No files found.
helper
/auth.go
→
middleware
/auth.go
View file @
7f257399
package
helper
package
middleware
import
(
import
(
"context"
"context"
...
@@ -9,7 +9,8 @@ import (
...
@@ -9,7 +9,8 @@ import (
"strconv"
"strconv"
"time"
"time"
"golang.org/x/time/rate"
"im-microservice/helper"
"google.golang.org/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/grpc/status"
...
@@ -25,7 +26,7 @@ func permissionError(msg string) error {
...
@@ -25,7 +26,7 @@ func permissionError(msg string) error {
func
checksum
(
req
map
[
string
]
string
)
error
{
func
checksum
(
req
map
[
string
]
string
)
error
{
err
,
AppSecret
:=
GetSecretByKey
(
req
[
"Appkey"
])
err
,
AppSecret
:=
helper
.
GetSecretByKey
(
req
[
"Appkey"
])
if
err
!=
nil
{
if
err
!=
nil
{
return
status
.
Error
(
codes
.
NotFound
,
"appkey不存在"
)
return
status
.
Error
(
codes
.
NotFound
,
"appkey不存在"
)
}
}
...
@@ -53,8 +54,7 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
...
@@ -53,8 +54,7 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler
grpc
.
UnaryHandler
)
(
interface
{},
error
)
{
handler
grpc
.
UnaryHandler
)
(
interface
{},
error
)
{
if
info
.
FullMethod
!=
health_path
{
if
info
.
FullMethod
!=
health_path
{
limiter
:=
rate
.
NewLimiter
(
rate
.
Every
(
100
*
time
.
Millisecond
),
1
)
if
limiter
.
Allow
()
{
request_map
:=
make
(
map
[
string
]
string
)
request_map
:=
make
(
map
[
string
]
string
)
s
:=
fmt
.
Sprintf
(
"%v"
,
req
)
s
:=
fmt
.
Sprintf
(
"%v"
,
req
)
...
@@ -87,9 +87,6 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
...
@@ -87,9 +87,6 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
if
err
:=
checksum
(
request_map
);
err
!=
nil
{
if
err
:=
checksum
(
request_map
);
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
}
else
{
return
nil
,
status
.
Error
(
codes
.
Unauthenticated
,
"请求超过限制,请稍后再试"
)
}
}
}
return
handler
(
ctx
,
req
)
return
handler
(
ctx
,
req
)
...
...
middleware/interceptor.go
0 → 100644
View file @
7f257399
package
middleware
import
(
"context"
"time"
"golang.org/x/time/rate"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func
Interceptor
(
ctx
context
.
Context
,
req
interface
{},
info
*
grpc
.
UnaryServerInfo
,
handler
grpc
.
UnaryHandler
)
(
interface
{},
error
)
{
if
info
.
FullMethod
!=
health_path
{
limiter
:=
rate
.
NewLimiter
(
rate
.
Every
(
100
*
time
.
Millisecond
),
1
)
if
limiter
.
Allow
()
{
return
handler
(
ctx
,
req
)
}
else
{
return
nil
,
status
.
Error
(
codes
.
Unauthenticated
,
"请求超过限制,请稍后再试"
)
}
}
}
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