补充认证中间件

This commit is contained in:
yumaojun03 2024-12-08 18:10:55 +08:00
parent c537d965e6
commit 78b6dd2049
5 changed files with 79 additions and 14 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/infraboard/mcube/v2/http/gin/response"
"gitlab.com/go-course-project/go17/vblog/apps/blog"
"gitlab.com/go-course-project/go17/vblog/apps/blog/impl"
"gitlab.com/go-course-project/go17/vblog/middleware"
)
func NewBlogApiHandler() *BlogApiHandler {
@ -21,6 +22,7 @@ type BlogApiHandler struct {
// book := server.Group("/api/tokens")
func (h *BlogApiHandler) Registry(r *gin.Engine) {
router := r.Group("/vblog/api/v1/blogs")
router.Use(middleware.Auth)
router.POST("", h.CreateBlog)
router.GET("", h.QueryBlog)
}

View File

@ -6,6 +6,7 @@ import (
"github.com/infraboard/mcube/v2/ioc/config/datasource"
"gitlab.com/go-course-project/go17/vblog/apps/blog"
"gitlab.com/go-course-project/go17/vblog/middleware"
)
var BlogService blog.Service = &BlogServiceImpl{}
@ -22,6 +23,10 @@ func (b *BlogServiceImpl) CreateBlog(ctx context.Context, in *blog.CreateBlogReq
return nil, err
}
// 冲上下文中补充 创建人
tk := middleware.GetTokenFromCtx(ctx)
ins.CreateBy = tk.RefUserName
err = datasource.DBFromCtx(ctx).Create(ins).Error
if err != nil {
return nil, err

View File

@ -69,5 +69,14 @@ func (t *TokenServiceImpl) ValidateToken(ctx context.Context, in *token.Validate
return nil, err
}
// 1. 补充用户信息
u, err := t.user.DescribeUser(ctx, &user.DescribeUserRequest{
DescribeBy: user.DESCRIBE_BY_ID,
Value: tk.RefUserId,
})
if err != nil {
return nil, err
}
tk.RefUserName = u.Username
return tk, nil
}

View File

@ -1,45 +1,45 @@
<mxfile host="65bd71144e">
<diagram id="GIsrK_2abnF8TOuNHqx3" name="第 1 页">
<mxGraphModel dx="937" dy="537" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<mxGraphModel dx="937" dy="484" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="7" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.3333333333333333;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1" source="2" target="6">
<mxCell id="7" style="edgeStyle=none;html=1;exitX=0;exitY=0.5;exitDx=0;exitDy=0;entryX=1;entryY=0.3333333333333333;entryDx=0;entryDy=0;entryPerimeter=0;" parent="1" source="2" target="6" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="9" value="Resp" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="7">
<mxCell id="9" value="Resp" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="7" vertex="1" connectable="0">
<mxGeometry x="-0.0947" y="-1" relative="1" as="geometry">
<mxPoint as="offset"/>
</mxGeometry>
</mxCell>
<mxCell id="2" value="Handle Func" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxCell id="2" value="Handle Func" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="670" y="250" width="120" height="110" as="geometry"/>
</mxCell>
<mxCell id="3" value="打印访问日志" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxCell id="3" value="打印访问日志" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="120" y="90" width="90" height="390" as="geometry"/>
</mxCell>
<mxCell id="5" style="edgeStyle=none;html=1;exitX=1;exitY=0.3333333333333333;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" edge="1" parent="1" source="4" target="2">
<mxCell id="5" style="edgeStyle=none;html=1;exitX=1;exitY=0.3333333333333333;exitDx=0;exitDy=0;exitPerimeter=0;entryX=0;entryY=0.5;entryDx=0;entryDy=0;" parent="1" source="4" target="2" edge="1">
<mxGeometry relative="1" as="geometry"/>
</mxCell>
<mxCell id="8" value="Req" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" vertex="1" connectable="0" parent="5">
<mxCell id="8" value="Req" style="edgeLabel;html=1;align=center;verticalAlign=middle;resizable=0;points=[];" parent="5" vertex="1" connectable="0">
<mxGeometry x="0.0637" y="-2" relative="1" as="geometry">
<mxPoint x="-1" as="offset"/>
</mxGeometry>
</mxCell>
<mxCell id="4" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1">
<mxCell id="4" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" parent="1" vertex="1">
<mxGeometry x="30" y="110" width="30" height="60" as="geometry"/>
</mxCell>
<mxCell id="6" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1">
<mxCell id="6" value="Actor" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" parent="1" vertex="1">
<mxGeometry x="30" y="400" width="30" height="60" as="geometry"/>
</mxCell>
<mxCell id="10" value="认证&lt;br&gt;&lt;br&gt;将用户的信息方到上下文中" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="230" y="90" width="90" height="390" as="geometry"/>
<mxCell id="10" value="认证&lt;br&gt;&lt;br&gt;将用户的信息方到上下文中" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="240" y="90" width="90" height="390" as="geometry"/>
</mxCell>
<mxCell id="11" value="鉴权&lt;br&gt;&lt;br&gt;用户有没有权限访问该接口" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxCell id="11" value="鉴权&lt;br&gt;&lt;br&gt;用户有没有权限访问该接口" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="400" y="90" width="90" height="390" as="geometry"/>
</mxCell>
<mxCell id="14" value="链路追踪&lt;br&gt;trace Id" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="500" y="90" width="90" height="390" as="geometry"/>
<mxCell id="14" value="链路追踪&lt;br&gt;trace Id" style="rounded=0;whiteSpace=wrap;html=1;" parent="1" vertex="1">
<mxGeometry x="510" y="90" width="90" height="390" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>

49
vblog/middleware/auth.go Normal file
View File

@ -0,0 +1,49 @@
package middleware
import (
"context"
"fmt"
"strings"
"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/v2/exception"
"github.com/infraboard/mcube/v2/http/gin/response"
"gitlab.com/go-course-project/go17/vblog/apps/token"
"gitlab.com/go-course-project/go17/vblog/apps/token/impl"
)
// // HandlerFunc defines the handler used by gin middleware as return value.
// type HandlerFunc func(*Context)
// 补充我们的鉴权逻辑
// 1. 首先要获取Token, Cookie, Header, Authorization: 用于存放用户认证信息, Authorization: <token_type> <token_value>, Bearer xxxxxx
// 2. 校验Token
// 3. 注入用户信息
func Auth(c *gin.Context) {
// 1. 获取Token
accessToken := c.GetHeader("Authorization")
tkList := strings.Split(accessToken, " ")
accessToken = ""
if len(tkList) == 2 {
accessToken = tkList[1]
}
// 2. 校验Token
tk, err := impl.TokenService.ValidateToken(c.Request.Context(), token.NewValidateTokenRequest(accessToken))
if err != nil {
response.Failed(c, exception.NewUnauthorized("令牌校验失败: %s", err))
c.Abort()
return
}
// 3. 注入用户信息
ctx := context.WithValue(c.Request.Context(), TokenCtxKey{}, tk)
fmt.Println(tk)
c.Request = c.Request.WithContext(ctx)
}
type TokenCtxKey struct{}
func GetTokenFromCtx(ctx context.Context) *token.Token {
return ctx.Value(TokenCtxKey{}).(*token.Token)
}