服务器响应401,如果服务器返回401响应,则重定向到登录页面(Redirect to login page if server returns 401 response)...

如果服务器返回401响应,则重定向到登录页面(Redirect to login page if server returns 401 response)

如果是401响应,我如何拦截服务器端点响应并将aurelia应用程序重定向到登录页面?

我尝试了aurelia-fetch-client配置的“withInterceptor(responseError(){...})”方法,但我无法返回“new Redirect(loginPage)”...

任何人都知道如何做到这一点?

How can I intercept the server endpoint response and redirect an aurelia application to login page if is a 401 response?

I tried "withInterceptor(responseError() {...})" method of aurelia-fetch-client config, but I cannot return a "new Redirect(loginPage)"...

Anyone has an idea how to do it?

原文:https://stackoverflow.com/questions/38611321

更新时间:2019-12-01 11:30

最满意答案

这是一个例子:

import { HttpClient } from 'aurelia-fetch-client';

import { inject } from 'aurelia-framework'

import { Router } from 'aurelia-router'

@inject(HttpClient, Router)

export class UserService {

http

router

constructor(http, router) {

this.http = http

this.router = router

this.http.configure(config => {

var self = this;

config

.withInterceptor({

responseError(response) {

if (response.status === 401) {

self.router.navigateToRoute('login')

}

return response; // you can return a modified Response

},

});

});

}

Here's an example:

import { HttpClient } from 'aurelia-fetch-client';

import { inject } from 'aurelia-framework'

import { Router } from 'aurelia-router'

@inject(HttpClient, Router)

export class UserService {

http

router

constructor(http, router) {

this.http = http

this.router = router

this.http.configure(config => {

var self = this;

config

.withInterceptor({

responseError(response) {

if (response.status === 401) {

self.router.navigateToRoute('login')

}

return response; // you can return a modified Response

},

});

});

}

相关问答

这是一个例子: import { HttpClient } from 'aurelia-fetch-client';

import { inject } from 'aurelia-framework'

import { Router } from 'aurelia-router'

@inject(HttpClient, Router)

export class UserService {

http

router

constructor(http, router) {

th

...

401是HTTP状态代码“Unauthorized”,当提供的凭证无效(用户名/密码不正确)时,将从服务器发回。 检查您是否正在发送正确的信息。 此外,正确配置AFHTTPClient会自动提供JSON序列化。 请参阅AFNetworking存储库中的示例应用程序,了解如何完成此操作。 401 is the HTTP status code "Unauthorized", which is sent back from the server when the provided credential

...

网络社区有一些关于证书失败的正确答案的争论。 (例如,这里是关于从200切换到401的Wordpress票据 。)Django选择返回200响应以及重新呈现的表单。 在我看来,这是正确的做法。 401或403响应表示用户没有权限访问资源(URL)。 但是在这种情况下,资源是登录点,并且您不需要凭据即可访问该资源; 根据定义,它可供所有用户使用。 因此,基本上这种情况与其他任何表单验证没有区别 - 服务器正在检查它发送的输入,如果它们无效,它将返回200响应以及表单和错误消息。 There's so

...

catch (WebException e)

{

resp = (HttpWebResponse)e.Response;

}

catch (WebException e)

{

resp = (HttpWebResponse)e.Response;

}

您可以指定应用程序如何处理异常或HTTP状态码,方法是在web.xml的错误页面元素中指定它 例如:web.xml

401

/login.html

与处理页面未找到页面的其他HTTP状态代码即404相同。 I solved this issue by adding the following elements to my Spring

...

这应该工作,传递您希望用户重定向到的特定路径: var reservedPaths = ['/explore','/teachers','/reports'];

if (rejection.status === 401 && _.contains(reservedPaths,$location.path().trim()) {

$location.path('/welcome');

}

或者如果你想使用ui-router's状态,你可以使用$state.includes() 。

...

有一种简单的方法可以禁止重定向到登录页面以获取非托管请求。 只需在ConfigureApplicationCookie添加以下对ConfigureApplicationCookie扩展方法的调用: services.ConfigureApplicationCookie(options =>

{

options.Events.OnRedirectToLogin = context =>

{

context.Response.StatusCode = (int)Http

...

我发现了我的问题。 Twitter似乎已经开始强制https,重定向停止工作。 我更新了我的Twitter消费者,将其从http更改为https。 我的消费者是从样本中提取的。 ///

/// The description of Twitter's OAuth protocol URIs for use with actually reading/writing

/// a user's private Twitter data.

...

终于搞清楚了 - 使用Timothy在链接问题和本博文中建议的自定义授权标签的组合,我能够暂时将状态代码设置为418,然后在Global.End_Request中将其更改回401。 我要做的最后一件事是将418添加到我的web.config并使用PassThrough模式:

...

如果不看脚本,真的很难告诉你什么是错的。 所以我会猜测并给你一些指示。 您可能希望将cookie管理器保持在块的其余部分之上。 这就是Thread控制器( 即线程组级别 )之下。 这可能不是问题,因为我可以在失败的步骤中看到_auth_session cookie,但这仍然是一个很好的做法。 如果您认为这可能是因为服务器未识别浏览器,您可以使用标头管理器并通过它发送浏览器(user_agent)信息。 看例子 尝试摆弄cookie管理器中的Cookie策略和实现选项。 我希望这有帮助。 I fin

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值