好主意,使用REST令牌身份验证AJAX Web应用程序? [英] Good idea to use REST token authentication for AJAX web apps?

查看:250
本文介绍了好主意,使用REST令牌身份验证AJAX Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用Python / Django和优秀的Django的REST框架一个REST API网页

我与此刻的身份验证方法进行实验,并想知道,如果它是用令牌认证对AJAX的web应用程序一个很好的做法。

我包含在一个非常基本的CRUD Web应用程序的示例HTML文件,证明我如何使用目前令牌认证。它工作正常,但是否真的OK(关于安全等)只包含源代码中的认证令牌?基本身份验证似乎是一个很好的选择,但我将不得不包括源,这更糟糕的在我看来,用户名和密码?

脚注:我知道REST是通过更好的安全HTTPS协议使用,但我跑我的开发机器上这个code,效果显着。我打算在生产中使用HTTPS。

在此先感谢任何提示!

亲切的问候,
克里斯托夫

  {%延伸base.html文件%}
{%块标题%} {游戏端嵌段%%}
{%块含量%}<脚本类型=文/ JavaScript的> / *<![CDATA [* /
功能getBase(){
    回报http://api.sandbox.dev:8080/';
}
函数的getData(){
    返回{
        ID:$('#ID)VAL()。
        标题:$('#标题')VAL()。
    };
}
功能为gettoken(){
    回报b26ffd6ddb66428ce9164d606f694cd4184ce73e';
}
$(文件)。就绪(函数(){
    $('#创建')。点击(函数(五){
        $阿贾克斯({
            网址:getBase()+'游戏/',
            输入:POST,
            数据:的getData(),
            数据类型:JSON,
            标题:{
                授权:'令牌'+为gettoken()
            },
            完成:功能(XHR,textStatus){
                的console.log('textStatus ='+ textStatus);
            },
            成功:功能(数据,textStatus,XHR){
                的console.log('textStatus ='+ textStatus);
            },
            错误:功能(XHR,textStatus,errorThrown){
                的console.log('textStatus ='+ textStatus +'\\ nerrorThrown ='+ errorThrown);
            },
        });
    });
    $('#更新')。点击(函数(五){
        如果(的getData()。id)的{
            $阿贾克斯({
                网址:getBase()+'游戏/'+的getData()ID +'/'。
                输入:'把',
                数据:的getData(),
                数据类型:JSON,
                标题:{
                    授权:'令牌'+为gettoken()
                },
                完成:功能(XHR,textStatus){
                    的console.log('textStatus ='+ textStatus);
                },
                成功:功能(数据,textStatus,XHR){
                    的console.log('textStatus ='+ textStatus);
                },
                错误:功能(XHR,textStatus,errorThrown){
                    的console.log('textStatus ='+ textStatus +'\\ nerrorThrown ='+ errorThrown);
                },
            });
        }
    });
    $('#删除')。点击(函数(五){
        如果(的getData()。id)的{
            $阿贾克斯({
                网址:getBase()+'游戏/'+的getData()ID +'/'。
                输入:删除,
                数据:{},
                数据类型:JSON,
                标题:{
                    授权:'令牌'+为gettoken()
                },
                完成:功能(XHR,textStatus){
                    的console.log('textStatus ='+ textStatus);
                },
                成功:功能(数据,textStatus,XHR){
                    的console.log('textStatus ='+ textStatus);
                },
                错误:功能(XHR,textStatus,errorThrown){
                    的console.log('textStatus ='+ textStatus +'\\ nerrorThrown ='+ errorThrown);
                },
            });
        }
    });
});
/ *]]> * /< / SCRIPT><形式的行动=方法=邮报的onsubmit =返回false>
    < D​​L>
        < D​​T><标签=ID> ID< /标签>< / DT>
        < D​​D><输入类型=文本ID =IDNAME =ID大小=20MAXLENGTH =10/>< / DD>
        < D​​T><标签=标题>标题< /标签>< / DT>
        < D​​D><输入类型=文本ID =标题名称=标题大小=20MAXLENGTH =20/>< / DD>
    < / DL>
    < P><按钮式=按钮ID =创造>创建< /按钮>< / P>
    < P><按钮式=按钮ID =更新>更新和LT; /按钮>< / P>
    < P><按钮式=按钮ID =删除>删除< /按钮>< / P>
< /形式为GT; {%端块%}


解决方案

TokenAuthentication 大多与连接到服务器,诸如此类的iOS,Android或窗口应用等本地客户端使用..

在使用Ajax处理(使用Web应用程序),你应该使用 SessionAuthentication 。这样可以节省你提供任何额外的数据。唯一的要求是为用户在登录

I created a REST web API using Python/Django and the excellent Django REST Framework.

I'm experimenting with authentication methods at the moment and was wondering if it's a good practice to use "token authentication" for AJAX web apps.

I included a sample HTML file with a very basic CRUD web app, demonstrating how I'm using token authentication at the moment. It works fine, but is it really OK (regarding security and so) to just include the authentication token in the source? Basic authentication seems like a good alternative, but then I would have to include both the username AND password in the source, which seems even worse to me?

Footnote: I know REST is better used through the HTTPS protocol for security, but I'm running this code on my development machine, obviously. I'm planning on using HTTPS in production.

Thanks in advance for any tips!

Kind regards, Kristof

{% extends 'base.html' %}
{% block title %}Games{% endblock %}
{% block content %}<script type="text/javascript">/*<![CDATA[*/
function getBase() {
    return 'http://api.sandbox.dev:8080/';
}
function getData() {
    return {
        id: $('#id').val(),
        title: $('#title').val(),
    };
}
function getToken() {
    return 'b26ffd6ddb66428ce9164d606f694cd4184ce73e';
}
$(document).ready(function(){
    $('#create').click(function(e){
        $.ajax({
            url: getBase() + 'games/',
            type: 'POST',
            data: getData(),
            dataType: 'json',
            headers: {
                authorization: 'token ' + getToken(),
            },
            complete: function(xhr, textStatus) {
                console.log('textStatus = ' + textStatus);
            },
            success: function(data, textStatus, xhr) {
                console.log('textStatus = ' + textStatus);
            },
            error: function(xhr, textStatus, errorThrown) {
                console.log('textStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown);
            },
        });
    });
    $('#update').click(function(e){
        if(getData().id) {
            $.ajax({
                url: getBase() + 'games/' + getData().id + '/',
                type: 'PUT',
                data: getData(),
                dataType: 'json',
                headers: {
                    authorization: 'token ' + getToken(),
                },
                complete: function(xhr, textStatus) {
                    console.log('textStatus = ' + textStatus);
                },
                success: function(data, textStatus, xhr) {
                    console.log('textStatus = ' + textStatus);
                },
                error: function(xhr, textStatus, errorThrown) {
                    console.log('textStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown);
                },
            });
        }
    });
    $('#delete').click(function(e){
        if(getData().id) {
            $.ajax({
                url: getBase() + 'games/' + getData().id + '/',
                type: 'DELETE',
                data: {},
                dataType: 'json',
                headers: {
                    authorization: 'token ' + getToken(),
                },
                complete: function(xhr, textStatus) {
                    console.log('textStatus = ' + textStatus);
                },
                success: function(data, textStatus, xhr) {
                    console.log('textStatus = ' + textStatus);
                },
                error: function(xhr, textStatus, errorThrown) {
                    console.log('textStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown);
                },
            });
        }
    });
});
/*]]>*/</script><form action="" method="post" onsubmit="return false">
    <dl>
        <dt><label for="id">ID</label></dt>
        <dd><input type="text" id="id" name="id" size="20" maxlength="10" /></dd>
        <dt><label for="title">Title</label></dt>
        <dd><input type="text" id="title" name="title" size="20" maxlength="20" /></dd>
    </dl>
    <p><button type="button" id="create">Create</button></p>
    <p><button type="button" id="update">Update</button></p>
    <p><button type="button" id="delete">Delete </button></p>
</form>{% endblock %}

解决方案

TokenAuthentication is mostly used with native clients connecting to server, things like iOS, Android or windows apps etc..

When dealing with ajax (using web app), you should use SessionAuthentication. This saves you from providing any additional data. The only requirement is for user to be logged in.

这篇关于好主意,使用REST令牌身份验证AJAX Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆