我在Scala中使用Play Framework 2建立一个应用程序.它将纯粹是RESTful,目前从Javascript单页面应用程序调用.
什么是集成基于令牌的身份验证的最佳方式?Play2有多个身份验证库,加上原始Secured特征,但不清楚哪一个最方便.
感谢您的帮助和建议
authentication rest scala playframework http-token-authentication
我用django rest框架实现了令牌认证,我可以发布用户名和密码/api-token-auth/并获取令牌.
url(r'^api-token-auth/', token_views.obtain_auth_token)
Run Code Online (Sandbox Code Playgroud)
除了令牌,我想获得User与返回的令牌相关的对象.
如何覆盖/添加到此视图并返回实际的User对象?
overriding view django-rest-framework http-token-authentication
我基本上想打开 TokenAuthentication 但仅用于 2 个单元测试。到目前为止,我看到的唯一选项是@override_settings(...)用于替换 REST_FRAMEWORK 设置值。
REST_FRAMEWORK_OVERRIDE={
'PAGINATE_BY': 20,
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
'rest_framework_csv.renderers.CSVRenderer',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}
@override_settings(REST_FRAMEWORK=REST_FRAMEWORK_OVERRIDE)
def test_something(self):
Run Code Online (Sandbox Code Playgroud)
这不起作用。我可以在装饰器之前和之后打印设置,并看到这些值发生了变化,但 django 似乎并不尊重它们。它允许使用测试客户端或 DRF APIClient 对象发送的所有请求无需身份验证即可通过。当我预计 401 未经授权时,我会收到 200 回复。
如果我将相同的字典插入到 config 文件夹中的 test_settings.py 文件中,一切都会按预期工作。然而,就像我说的,我只想为几个单元测试打开身份验证,而不是全部。我的想法是 Django 在初始化后永远不会重新访问 DRF 的设置。因此,即使设置值正确,也不会使用它们。
有没有人遇到过这个问题并找到了解决方案?还是解决办法?
django unit-testing django-settings django-rest-framework http-token-authentication
我正在尝试使用devise_token_auth版本登录现有用户0.1.38,但我正在使用IndexError: string not matched该库sessions_controller.
IndexError (string not matched):
devise_token_auth (0.1.38) app/controllers/devise_token_auth/sessions_controller.rb:37:in `[]='
devise_token_auth (0.1.38) app/controllers/devise_token_auth/sessions_controller.rb:37:in `create'
actionpack (5.0.0) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
Run Code Online (Sandbox Code Playgroud)
相关代码sessions_controller是:
if @resource and valid_params?(field, q_value) and @resource.valid_password?(resource_params[:password]) and (!@resource.respond_to?(:active_for_authentication?) or @resource.active_for_authentication?)
# create client id
@client_id = SecureRandom.urlsafe_base64(nil, false)
@token = SecureRandom.urlsafe_base64(nil, false)
# !! Next line is line 37 with the error !!
@resource.tokens[@client_id] …Run Code Online (Sandbox Code Playgroud) ruby-on-rails devise http-token-authentication ruby-on-rails-5
我正在尝试使用Auth0进行社交登录,但我不断获得未定义引用的例外.
import { Injectable } from '@angular/core';
import { tokenNotExpired } from 'angular2-jwt';
// Avoid name not found warnings
declare var Auth0Lock: any;
@Injectable()
export class AuthService {
// Configure Auth0
lock = new Auth0Lock('I have set the ID correctly here', 'and the domain as well', {});
constructor() {
// Add callback for lock `authenticated` event
this.lock.on("authenticated", (authResult) => {
localStorage.setItem('id_token', authResult.idToken);
});
}
public login() {
// Call the show method to display the widget.
this.lock.show();
};
public …Run Code Online (Sandbox Code Playgroud) 我正在构建基于令牌的身份验证(Node.js 使用带有角度客户端的护照/JWT)。
用户输入他的凭据后,他会获得一个访问令牌,他在标头内的每个请求中发送该令牌(标头:bearer TOKEN)。
我不想在他的访问令牌每次到期时(我猜大约是每天)都提示登录请求,我听说过Refresh Tokens。刷新令牌永不过期(或很少过期)并且能够无限期更新令牌。当访问令牌即将过期时,客户端可以通过发送其刷新令牌来发送更新请求以获取新的访问令牌。
我不明白一些事情,我可能会遗漏一些东西:
长期/永不过期的刷新令牌如何不会破坏拥有短期访问令牌的安全性。
Cookie 可以被窃取和使用,直到它们过期。令牌是短暂的,所以它们更安全,但如果我提供一个长期的刷新令牌,我就失去了使用令牌的优势。
注意:我知道刷新令牌是在初始登录时发送的,因此不能在每个请求中被欺骗,但如果它们在初始请求中被欺骗,它们就很容易受到攻击。
我有一个 Node.js 端点,当使用以下命令访问时,它会正确触发任意大的文件下载:
response.setHeader('Content-disposition', 'attachment; filename=' + fileName);
response.set('Content-Type', 'text/csv');
response.status(200);
result.pipe(response);
Run Code Online (Sandbox Code Playgroud)
其中result是变换流,response是Express 对象。
当直接访问 Chrome、Firefox、Internet Explorer 等中的端点时,此方法工作正常。问题是在启用基于令牌的身份验证时尝试访问端点而产生的。
从用户的角度来看,当他们单击按钮时,就会下载文件。
如何使按钮使用请求标头中的正确身份验证令牌命中此端点并导致下载文件?
当用户单击该按钮时,它会触发由 redux-api-middleware 处理的操作,该操作GET向端点发出请求(身份验证令牌自动包含在请求中)。该中间件将响应保存在一个变量中,该变量由 React 组件获取。在此 React 组件中,如果正在使用的浏览器(即 Chrome 和 Opera)支持流,response.body则会存在,因此您可以执行如下操作。
if (response.body) {
const csvReader = response.body.getReader();
const textDecoder = new TextDecoder();
const processCsvRow = (csvRow) => {
if (csvRow.done) {
console.log('Finished downloading file.');
return Promise.resolve();
}
// Write to new file for user to download …Run Code Online (Sandbox Code Playgroud)我必须遵循哪些步骤才能在我的网页中实施令牌身份验证?
任何摘要或链接将不胜感激.
我想实现类似于Facebook或谷歌,第一次客户端登录和接收令牌,然后在下一步行动中使用它.我还阅读了关于OAuth但我不想从第三方访问我的应用程序.
感谢您的长期响应,我似乎很清楚我需要阅读更多相关信息.
我想要知道实现使用令牌认证的基本Web应用程序的"步骤".那是用户记录一次然后可以做一些动作:添加内容,编辑等.
我知道我所说的与会话类似,其中服务器在HTML标头上添加SESSION_ID,稍后请求被识别并与该会话相关联.我读过会话方式不好扩展所以我想在进入OAuth之前实现类似gmail或facebook的系统.可能我说的是类似于oauth的东西(我没有深入阅读)但是两腿而不是三条腿.
我想用Angular JS建立Web API令牌认证作为客户端.我对Web API中的令牌认证概念非常陌生.
我不想使用ASP.NET Identity默认表来添加或验证用户.我有自己的数据库和一个名为"EmployeeAccess"的表,其中包含EmployeeNumber作为用户ID和密码.我想根据此表中的值对用户进行身份验证,然后要授予令牌,以便他们获得后续调用的授权.我已经使用了所有必需的OWIN和ASP.NET引用来实现结果.这是我的不同组件的代码: -
Global.asax中
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_BeginRequest()
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
// Cache the options request.
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", HttpContext.Current.Request.Headers["Origin"]);
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, PUT, DELETE, POST, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
}
Run Code Online (Sandbox Code Playgroud)
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id …Run Code Online (Sandbox Code Playgroud) asp.net-web-api angularjs http-token-authentication asp.net-identity owin-middleware
我有一个 Django 应用程序,我将 DRF 用于带有会话和令牌身份验证的 API。我已安装的应用程序中有 rest_framework 和 rest_framework.authtoken。我已经迁移了我的数据库并且可以在 Django Admin 中为用户创建令牌。我知道所有这些都在起作用,因为我正在访问 rest_framework.auth_token 的 acquire_auth_token 视图,以便在 POST 请求中提交用户数据时返回令牌,并收到一个令牌。当我尝试向我的应用程序中的视图函数发出 GET 请求时,该视图函数在其视图集中具有 TokenAuthentication,它不断返回。
{"detail":"Authentication credentials were not provided."}
Run Code Online (Sandbox Code Playgroud)
设置文件
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# My Apps
'rest_framework',
'rest_auth',
'rest_framework.authtoken',
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
}
Run Code Online (Sandbox Code Playgroud)
网址
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from rest_framework.authtoken import views
from api.views.some_model import MyViewSet
urlpatterns = [
path('', include(router.urls)),
path('rest-auth/', include('rest_auth.urls')),
path('api-token-auth/', views.obtain_auth_token)
] …Run Code Online (Sandbox Code Playgroud) python django django-rest-framework http-token-authentication
django ×2
jwt ×2
token ×2
access-token ×1
angular2-jwt ×1
angularjs ×1
auth0 ×1
devise ×1
express ×1
node.js ×1
overriding ×1
python ×1
react-redux ×1
rest ×1
scala ×1
security ×1
unit-testing ×1
view ×1