grpc 拦截器【 go 和 asp.net core的实现】

在 asp.ner core 5.0 Grpc HttpApi 和jwt的集成 和跨域【https双向认证】和  Go Grpc Jwt身份认证和Gateway集成以及HTTPS双向认证 我们 获取用户名都是通过方法的,在这里我们来用拦截器实现一下。

GO

大类可分为两种 RPC 方法,与拦截器的对应关系是:普通方法:一元拦截器(grpc.UnaryInterceptor);流方法:流拦截器(grpc.StreamInterceptor)。

1.拦截器的实现   我们来看看他的实现。在\gogrpcjwt\api\authtoken.go文件我们增加方法 AuthInterceptor【我们的值 是通过context来存储的】

复制代码
 
/*
ctx context.Context:请求上下文
req interface{}:RPC 方法的请求参数
info *UnaryServerInfo:RPC 方法的所有信息
handler UnaryHandler:RPC 方法本身
*/
func AuthInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
    userName := CheckAuth(ctx)
    log.Printf("gRPC method: %s, %v", info.FullMethod, req)
    newCtx := ctx
    if len(userName) > 0 {
        //使用context.WithValue添加了值后,可以用Value(key)方法获取值
        newCtx = context.WithValue(ctx, "username", userName)
        //log.Println(newCtx.Value("username"))
    }
    return handler(newCtx, req)
}
复制代码

2.注册拦截器 servertls\main.go

//rpcServer := grpc.NewServer()
    opts := []grpc.ServerOption{
        grpc_middleware.WithUnaryServerChain(
            api.AuthInterceptor,
        )}
    rpcServer := grpc.NewServer(opts...)

3.获取用户信息 修改api/handler.go的SayHello 方法:

    //userName := CheckAuth(ctx)
    userName := fmt.Sprintf("%v", ctx.Value("username"))

4运行结果正常

D:\GoProject\src\gogrpcjwt\clienttls>go run main.go
2021/01/06 16:45:24 Response from server: bar gavin
http call.....
2021/01/06 16:45:24 {"greeting":"bar gavin"}

asp.net core 5.0

在C# 里面分客户端【个人觉得 意义不大,比如我用html 只能访问httpapi】 和服务端

1.创建拦截器AuthInterceptor.cs ,继承拦截器基类 Interceptor 

复制代码
using Grpc.Core;
using Grpc.Core.Interceptors;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
 
namespace grpcserver
{
    public class AuthInterceptor:Interceptor
    {
        const string tokenSchema = "Bearer";
        const string tokenIssuer = "https://localhost:5001";
        const string tokenAudience = "grpc";
        const string tokenSecurityKey = "asp.netcore5.0grpcjwt";
        public override Task<TResponse> UnaryServerHandler<TRequest, TResponse>(
        TRequest request,
        ServerCallContext context,
        UnaryServerMethod<TRequest, TResponse> continuation)
        {
            var userContext = context.GetHttpContext().User;
            string userName = userContext.FindFirstValue(ClaimTypes.Name);
            if (string.IsNullOrEmpty(userName)) {
                string tokenStr = context.GetHttpContext().Request?.Headers["Authorization"];
                if (!string.IsNullOrEmpty(tokenStr))
                {
 
                    if (tokenStr.StartsWith(tokenSchema))
                    {
                        tokenStr = tokenStr.Split(' ')[1];
                    }
                    SecurityToken token;
                    var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenSecurityKey));
                    ClaimsPrincipal claims = new JwtSecurityTokenHandler().ValidateToken(tokenStr, new TokenValidationParameters
                    {
                        ValidAudience = tokenAudience,
                        ValidIssuer = tokenIssuer,
                        IssuerSigningKey = key
 
                    }, out token);
                    context.GetHttpContext().User = claims;
                }
               
            }
            return continuation(request, context);
        }
    }
}
复制代码

2.注册拦截器 Startup.cs的ConfigureServices 方法

 services.AddGrpc(x=>x.Interceptors.Add<AuthInterceptor>());

3.修改调用程序GreeterService.cs的SayHello

 // var userName= CheckAuth(context)??string.Empty;
 var userName = context.GetHttpContext().User.FindFirstValue(ClaimTypes.Name);

4.运行结果:

备注,像这种获取信息的, 我个人还是推荐 直接用方法 调用来的实惠,拦截器 类似管道 我一般 用于处理系统级的问题, 开发感觉不到, 比如 统一日志

下载地址: https://github.com/dz45693/asp.netgrpccert.git  【C#】  https://github.com/dz45693/gogrpcjwt.git 【Go】

参考 

https://www.cnblogs.com/stulzq/p/11840535.html

 

posted on   dz45693  阅读(2252)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2014-01-06 sharepoint 2010 自定义页面布局

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示