我正在尝试使用Owin实施OAuth承载认证.传递无效或过期的令牌时,默认实现是将其记录为警告,并且不要设置标识.但是,我希望在这种情况下拒绝整个请求并出现错误.但是我该怎么做呢?
在深入研究代码之后,我发现在提供的代码没有解析任何故障单时(如默认实现),OAuthBearerAuthenticationHandler它将使用回退机制解析令牌AuthenticationTokenProvider.当令牌无法解析为任何故障单或过期时,此处理程序将记录警告.
但我无法找到任何地方来插入我自己的逻辑,以便当令牌无效或过期时会发生什么.理论上我可以自己检查一下AuthenticationTokenProvider,但是我必须重新实现逻辑(=复制它)来创建和读取令牌.这似乎也不合适,因为这个类似乎只负责创建和解析令牌.我也没有看到插入我自己的实现OAuthBearerAuthenticationHandler的方法OAuthBearerAuthenticationMiddleware.
显然,我最好和最干净的镜头将是重新实现整个中间件,但这似乎也非常矫枉过正.
我有什么看法?我怎么会继续这个最好的?
编辑:
为了澄清.我知道,如果不设置身份,请求将在稍后的Web API中被401 Unauthorized拒绝.但我个人认为这是一种非常糟糕的风格,在没有任何通知的情况下默默地吞下一个错误的访问令牌.这样你就不会知道你的令牌是垃圾,你只是知道你没有被授权.
我在使用纯TypeScript编写角度http拦截器时遇到了麻烦.我试图转换的代码如下:
.config(['$httpProvider', function ($httpProvider) {
var interceptor = ['$rootScope', '$q', 'httpBuffer', function ($rootScope, $q, httpBuffer) {
function success(response) {
return response;
}
function error(response) {
if (response.status === 401 && !response.config.ignoreAuthModule) {
var deferred = $q.defer();
httpBuffer.append(response.config, deferred);
$rootScope.$broadcast('event:auth-loginRequired', response);
return deferred.promise;
}
// otherwise, default behaviour
return $q.reject(response);
}
return function (promise) {
return promise.then(success, error);
};
}];
$httpProvider.responseInterceptors.push(interceptor);
}])
Run Code Online (Sandbox Code Playgroud)
第一部分是清楚的,写一个带有构造函数的类,它接受三个依赖项$rootScope,$q以及httpBuffer.还写了两个私有方法success和response.
class MyInterceptorClass {
constructor(private $rootScope: ng.IRootScopeService, private $q: …Run Code Online (Sandbox Code Playgroud)