• WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制


    .NET WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制

    项目背景是一个社区类的APP(求轻吐...),博主主要负责后台业务及接口。以前没玩过webAPI,但是领导要求必须用这个(具体原因鬼知道),只好硬着头皮上了。

    最近刚做完权限这一块,分享出来给大家。欢迎各种吐槽批判践踏...

    先说说用户身份的识别,简单的做了一个token机制。用户登录,后台产生令牌,发放令牌,用户携带令牌访问...

    1.cache管理类,由于博主使用的HttpRuntime.Cache来存储token,IIS重启或者意外关闭等情况会造成cache清空,只好在数据库做了cache的备份,在cache为空的时候查询数据库是否有cache数据,有则是cache被意外清空,需要重新放在cache中。

    复制代码
        /// <summary>
        /// 缓存管理
        /// 将令牌、用户凭证以及过期时间的关系数据存放于Cache中
        /// </summary>
        public class CacheManager
        {
    private static readonly ITempCacheService tempCacheService = ServiceLocator.Instance.GetService<ITempCacheService>(); /// <summary> /// 初始化缓存数据结构 /// </summary> /// token 令牌 /// uuid 用户ID凭证 /// userType 用户类别 /// timeout 过期时间 /// <remarks> /// </remarks> private static void CacheInit() { if (HttpRuntime.Cache["PASSPORT.TOKEN"] == null) { DataTable dt = new DataTable(); dt.Columns.Add("token", Type.GetType("System.String")); dt.Columns["token"].Unique = true; dt.Columns.Add("uuid", Type.GetType("System.Object")); dt.Columns["uuid"].DefaultValue = null; dt.Columns.Add("userType", Type.GetType("System.String")); dt.Columns["userType"].DefaultValue = null; dt.Columns.Add("timeout", Type.GetType("System.DateTime")); dt.Columns["timeout"].DefaultValue = DateTime.Now.AddDays(7); DataColumn[] keys = new DataColumn[1]; keys[0] = dt.Columns["token"]; dt.PrimaryKey = keys; var tempCaches = tempCacheService.GetAllCaches(); if (tempCaches.Any()) { foreach (var tempCacheDTOShow in tempCaches) { DataRow dr = dt.NewRow(); dr["token"] = tempCacheDTOShow.UserToken; dr["uuid"] = tempCacheDTOShow.UserAccountId; dr["userType"] = tempCacheDTOShow.UserType.ToString(); dr["timeout"] = tempCacheDTOShow.EndTime; dt.Rows.Add(dr); } } //Cache的过期时间为 令牌过期时间*2 HttpRuntime.Cache.Insert("PASSPORT.TOKEN", dt, null, DateTime.MaxValue, TimeSpan.FromDays(7 * 2)); } } /// <summary> /// 获取用户UUID标识 /// </summary> /// <param name="token"></param> /// <returns></returns> public static Guid GetUUID(string token) { CacheInit(); DataTable dt = (DataTable)HttpRuntime.Cache["PASSPORT.TOKEN"]; DataRow[] dr = dt.Select("token = '" + token + "'"); if (dr.Length > 0) { return new Guid(dr[0]["uuid"].ToString()); } return Guid.Empty; } /// <summary> /// 获取用户类别(分为员工、企业、客服、管理员等,后期做权限验证使用) /// </summary> /// <param name="token"></param> /// <returns></returns> public static string GetUserType(string token) { CacheInit(); DataTable dt = (DataTable)HttpRuntime.Cache["PASSPORT.TOKEN"]; DataRow[] dr = dt.Select("token = '" + token + "'"); if (dr.Length > 0) { return dr[0]["userType"].ToString(); } return null; } /// <summary> /// 判断令牌是否存在 /// </summary> /// <param name="token">令牌</param> /// <returns></returns> public static bool TokenIsExist(string token) { CacheInit(); DataTable dt = (DataTable)HttpRuntime.Cache["PASSPORT.TOKEN"]; DataRow[] dr = dt.Select("token = '" + token + "'"); if (dr.Length > 0) { var timeout = DateTime.Parse(dr[0]["timeout"].ToString()); if (timeout > DateTime.Now) { return true; } else { RemoveToken(token); return false; } } return false; } /// <summary> /// 移除某令牌 /// </summary> /// <param name="token"></param> /// <returns></returns> public static bool RemoveToken(string token) { CacheInit(); DataTable dt = (DataTable)HttpRuntime.Cache["PASSPORT.TOKEN"]; DataRow[] dr = dt.Select("token = '" + token + "'"); if (dr.Length > 0) { dt.Rows.Remove(dr[0]); } return true; } /// <summary> /// 更新令牌过期时间 /// </summary> /// <param name="token">令牌</param> /// <param name="time">过期时间</param> public static void TokenTimeUpdate(string token, DateTime time) { CacheInit(); DataTable dt = (DataTable)HttpRuntime.Cache["PASSPORT.TOKEN"]; DataRow[] dr = dt.Select("token = '" + token + "'"); if (dr.Length > 0) { dr[0]["timeout"] = time; } } /// <summary> /// 添加令牌 /// </summary> /// <param name="token">令牌</param> /// <param name="uuid">用户ID凭证</param> /// <param name="userType">用户类别</param> /// <param name="timeout">过期时间</param> public static void TokenInsert(string token, object uuid, string userType, DateTime timeout) { CacheInit(); // token不存在则添加 if (!TokenIsExist(token)) { DataTable dt = (DataTable)HttpRuntime.Cache["PASSPORT.TOKEN"]; DataRow dr = dt.NewRow(); dr["token"] = token; dr["uuid"] = uuid; dr["userType"] = userType; dr["timeout"] = timeout; dt.Rows.Add(dr); HttpRuntime.Cache["PASSPORT.TOKEN"] = dt; tempCacheService.Add_TempCaches(new List<TempCacheDTO_ADD>() { new TempCacheDTO_ADD() { EndTime = timeout, UserAccountId = new Guid(uuid.ToString()), UserToken = new Guid(token), UserType = (UserType)Enum.Parse(typeof(UserType),userType) } }); } // token存在则更新过期时间 else { TokenTimeUpdate(token, timeout); tempCacheService.Update_TempCaches(new Guid(token), timeout); } } }
    复制代码

    2.接下来就是对用户携带的token进行验证了,通过继承ActionFilterAttribute来实现,在这里还需要考虑到匿名访问API,对于部分API,是允许匿名访问(不登录访问)的。所以,先写一个代表匿名的Attribute:

    复制代码
        /// <summary>
        /// 匿名访问标记
        /// </summary>
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
        public class AnonymousAttribute : Attribute
        {
        }
    复制代码

    然后给允许匿名访问的Action打上[Anonymous]标签就OK,再来看我们的token验证代码:

    复制代码
        /// <summary>
        /// 用户令牌验证/// </summary>
        public class TokenProjectorAttribute : ActionFilterAttribute
        {
            private const string UserToken = "token";
            private readonly IAccountInfoService accountInfoService = ServiceLocator.Instance.GetService<IAccountInfoService>();
            private readonly ITempCacheService tempCacheService = ServiceLocator.Instance.GetService<ITempCacheService>();
    
            public override void OnActionExecuting(HttpActionContext actionContext)
            {
                // 匿名访问验证
                var anonymousAction = actionContext.ActionDescriptor.GetCustomAttributes<AnonymousAttribute>();
                if (!anonymousAction.Any())
                {
                    // 验证token
                    var token = TokenVerification(actionContext);
                }
    
                base.OnActionExecuting(actionContext);
            }
    
            /// <summary>
            /// 身份令牌验证
            /// </summary>
            /// <param name="actionContext"></param>
            protected virtual string TokenVerification(HttpActionContext actionContext)
            {
                // 获取token
                var token = GetToken(actionContext.ActionArguments, actionContext.Request.Method);
    
                // 判断token是否有效
                if (!CacheManager.TokenIsExist(token))
                {
                    throw new UserLoginException("Token已失效,请重新登陆!");
                }
    
                // 判断用户是否被冻结
                if (accountInfoService.Exist_User_IsForzen(AccountHelper.GetUUID(token)))
                {
                    CacheManager.RemoveToken(token);
                    tempCacheService.Delete_OneTempCaches(new Guid(token));
                    throw new UserLoginException("此用户已被冻结,请联系客服!");
                }
    
                return token;
            }
    
            private string GetToken(Dictionary<string, object> actionArguments, HttpMethod type)
            {
                var token = "";
    
                if (type == HttpMethod.Post)
                {
                    foreach (var value in actionArguments.Values)
                    {
                        token = value.GetType().GetProperty(UserToken) == null
                            ? GetToken(actionArguments, HttpMethod.Get)
                            : value.GetType().GetProperty(UserToken).GetValue(value).ToString();
                    }
                }
                else if (type == HttpMethod.Get)
                {
                    if (!actionArguments.ContainsKey(UserToken))
                    {
                        throw new Exception("未附带token!");
                    }
    
                    if (actionArguments[UserToken] != null)
                    {
                        token = actionArguments[UserToken].ToString();
                    }
                    else
                    {
                        throw new Exception("token不能为空!");
                    }
                }
                else
                {
                    throw new Exception("暂未开放其它访问方式!");
                }
    
                return token;
            }
        }
    复制代码

    这里对GetToken方法做一下解释:

    1.博主只做了POST与GET方法的验证,其他请求未使用也就没做,欢迎大家补充

    2.POST方式里面的回调是解决POST请求接口只有一个简单参数的情况,例如下面的接口:

    复制代码
            /// <summary>
            /// 手动向新用户推送短信
            /// </summary>
            /// <returns></returns>
            [HttpPost]
            [Route("api/Common/PushNewUserSMS")]public PushNewWorkSMSResult PushNewUserSMS([FromBody]string token)
            {
                sendMessagesService.PushNewUserSMS();
                return new PushNewWorkSMSResult() { Code = 0 };
            }
    复制代码

    当然,POST方式一般都会把参数写进一个类里,对于一个参数的情况,博主不喜欢那么干。这么写,需要AJAX提交时空变量名才能获取:

    复制代码
        // 推送新用户营销短信
        function pushNewUserSMS() {
            $(".tuiguang").unbind("click");
    // 注意下面的参数为空“” $.post(Config.Api.Common.PushNewUserSMS, { "": $.cookie("MPCBtoken") }, function (data) { if (data.Code == 0) { alert("发送成功!"); _initDatas(); } else { Config.Method.JudgeCode(data, 1); } }); }
    复制代码

    这样,我们就只需要在每个controller上打上[TokenProjector]标签,再在允许匿名的Action上打上[Anonymous]标签就能轻松的搞定token验证了。

    3.除了token验证外呢,我门还想对Action进行用户角色的控制,比如一个获取登录用户钱包余额的Action(A),肯定只有员工、企业才能访问,管理员、客服没有钱包,所以不允许访问,从业务上应该是去访问另外一个获取指定用户钱包余额的Action(B),当然这个Action又不能对员工、企业开放权限。这就涉及到需要实现一个控制Action访问权限的功能,上面我们对用户的token进行了验证,那么拿到token就拿到了用户基本信息(包括角色),那就只需要做一个对Action的权限标注就能解决问题了,我们先写一个代表权限控制的Attribute:

    复制代码
        /// <summary>
        /// 权限控制标记
        /// </summary>
        [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
        public class ModuleAuthorizationAttribute : Attribute
        {
            public ModuleAuthorizationAttribute(params string[] authorization)
            {
                this.Authorizations = authorization;
            }
    
            /// <summary>
            /// 允许访问角色
            /// </summary>
            public string[] Authorizations { get; set; }
        }
    复制代码

    在每个需要权限控制的Action上打上[ModuleAuthorization]标签,并注明访问角色:

    复制代码
            /// <summary>
            /// 获取钱包余额
            /// </summary>
            /// <param name="token"></param>
            /// <returns></returns>
            [HttpGet]
            [Route("api/Account/GetWalletBalance")]
            [ModuleAuthorization(new[] { "Staff", "Enterprise" })]
            public GetWalletBalanceResult GetWalletBalance(string token)
            {
                var result = this.walletService.Get_Wallet_Balance(AccountHelper.GetUUID(token));
                return new GetWalletBalanceResult()
                {
                    Code = 0,
                    Balance = result
                };
            }
    
            /// <summary>
            /// 管理员
            /// 处理提现申请
            /// </summary>
            /// <param name="handleTempWithdrawalsModel"></param>
            /// <returns></returns>
            [HttpPost]
            [Route("api/Account/HandleTempWithdrawals")]
            [ModuleAuthorization(new[] { "PlatformCustomer" })]
            public HandleTempWithdrawalsResult HandleTempWithdrawals(
                [FromBody] HandleTempWithdrawalsModel handleTempWithdrawalsModel)
            {
                walletService.Handle_TempWithdrawals(AccountHelper.GetUUID(handleTempWithdrawalsModel.token),
                    handleTempWithdrawalsModel.message, handleTempWithdrawalsModel.tempID,
                    handleTempWithdrawalsModel.isSuccess);
                return new HandleTempWithdrawalsResult() { Code = 0 };
            }
    复制代码

    然后我们修改TokenProjectorAttribute这个类,在验证token后做权限验证,权限验证方法如下:

    复制代码
            /// <summary>
            /// Action 访问权限验证
            /// </summary>
            /// <param name="token">身份令牌</param>
            /// <param name="actionContext"></param>
            /// <returns></returns>
            protected virtual void AuthorizeCore(string token, HttpActionContext actionContext)
            {
                // 权限控制Action验证
                var moduleAuthorizationAction = actionContext.ActionDescriptor.GetCustomAttributes<ModuleAuthorizationAttribute>();
                if (moduleAuthorizationAction.Any())
                {
                    var userRole = AccountHelper.GetUserType(token);
                    if (!moduleAuthorizationAction[0].Authorizations.Contains(userRole.ToString()))
                    {
                        throw new Exception("用户非法跨权限访问,token:" + token);
                    }
                }
            }
    复制代码

    OK,终于实现了webAPI对用户令牌与Action权限的验证。

    当然,博主也是刚接触webAPI,再者业务需求较简单,如有不对之处,欢迎大家指出,必定虚心求教。

     
    分类: Web API
  • 相关阅读:
    MySQL_Sql_打怪升级_进阶篇_进阶1:基础查询
    [20210218]shared latch spin count 5.txt
    springboot逻辑删除
    springboot整合mybatis-plus的两种分页查询方式--自定义的结果集分页返回浏览器
    spring图片上传并返回绝对路径
    springboot+mybatisplus 逻辑删除
    mybatisplus枚举类
    Could not find resource com/meng/dao/UserMapper.xml
    jsp页面表单提交后mysql未更新原因
    springMVC拦截器的使用
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4868846.html
Copyright © 2020-2023  润新知