Javascript 尝试使用JSON令牌保护REST端点

Javascript 尝试使用JSON令牌保护REST端点,javascript,php,codeigniter,rest,json-web-token,Javascript,Php,Codeigniter,Rest,Json Web Token,我正在使用codeIgniter为应用程序创建服务。 我创建了一个端点,其中用户id作为参数传入,然后输出该用户的json数据。 在应用程序上,当用户登录时,我在服务器端创建一个JSON令牌。 我想在输出来自端点的json数据之前验证这个令牌。我不知道该怎么办。在我的codeIgniter控制器中加载视图之前,我应该检查令牌吗 我有一个profiles_模型,其中包括以下方法: function get_profile($user_id){ //this function tak

我正在使用codeIgniter为应用程序创建服务。 我创建了一个端点,其中用户id作为参数传入,然后输出该用户的json数据。 在应用程序上,当用户登录时,我在服务器端创建一个JSON令牌。 我想在输出来自端点的json数据之前验证这个令牌。我不知道该怎么办。在我的codeIgniter控制器中加载视图之前,我应该检查令牌吗

我有一个profiles_模型,其中包括以下方法:

function get_profile($user_id){
        //this function takes in a user_id as a parameter and gets that user's data from the profiles table in the database
        $this->db->from('users');
            $this->db->where('userID', $user_id);
        $query = $this->db->get();
                return $query->result();    //return the result 
    }
public function get_profile($user_id){
        //this method gets the basic profile info of a user depending on what user id is passed in as a parameter.
        //there are 6 profiles so user id should be between 1 to 6 to return any data 
        $this->load->model('Profiles_model');   //load our Profiles_model
        //create an empty array to store the profile info
        $data['profile'] = array(); 

        foreach($this->Profiles_model->get_profile($user_id) as $key => $value){
            array_push($data['profile'], array('user_id' => $value->userID, 
                    'username' => $value->username,
                    'profile_image' => $value->profileImage,
                    'email_address' => $value->emailAddress));
        }
        //load our json_output.php view and pass in the $data array.
        $this->load->view('json_output', $data);
    }
我有一个Profiles控制器类,它包括以下方法:

function get_profile($user_id){
        //this function takes in a user_id as a parameter and gets that user's data from the profiles table in the database
        $this->db->from('users');
            $this->db->where('userID', $user_id);
        $query = $this->db->get();
                return $query->result();    //return the result 
    }
public function get_profile($user_id){
        //this method gets the basic profile info of a user depending on what user id is passed in as a parameter.
        //there are 6 profiles so user id should be between 1 to 6 to return any data 
        $this->load->model('Profiles_model');   //load our Profiles_model
        //create an empty array to store the profile info
        $data['profile'] = array(); 

        foreach($this->Profiles_model->get_profile($user_id) as $key => $value){
            array_push($data['profile'], array('user_id' => $value->userID, 
                    'username' => $value->username,
                    'profile_image' => $value->profileImage,
                    'email_address' => $value->emailAddress));
        }
        //load our json_output.php view and pass in the $data array.
        $this->load->view('json_output', $data);
    }
json_output.php视图:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

if(isset($profile)){
    $output = $profile;
}

$this->output
->set_content_type('application/json', 'utf-8') //content type will be json
->set_output(json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));

?>
对于应用程序上的后续http请求,im将令牌作为POST发送,并在服务器端对其进行身份验证

if(isset($_POST["usertoken"])){
    $token = JWT::decode($_POST['usertoken'], 'secret_server_key');

    echo $token->userID; //this will be not available if the token has been tampered with
}
我想在我的端点中使用这段代码(在这里我检查usertoken post变量),但我不确定把它放在哪里。我应该把它放在json_output.php视图中吗?谢谢

客户端Javascript中检索Json的my函数

function generateUserProfile(user_id){
    var url = 'http://www.example.com/app_data/index.php/profiles/get_profile/' + user_id;
    $.getJSON(url ,{format: "json"}).done(function(data){
        var profile_image = "http://www.example.com/" + data[0].profile_image;
        var profile_username = data[0].username + '<i class="fa fa-pencil edit"></i>';
        var profile_email_address = data[0].email_address + '<i class="fa fa-pencil edit"></i>';
        $("#profile_pic").attr('src', profile_image);
        $("#profile_username").html(profile_username);
        $("#profile_email_address").html(profile_email_address);
    }); //end $.getJSON
}
函数生成器用户配置文件(用户id){
var url='1〕http://www.example.com/app_data/index.php/profiles/get_profile/“+用户id;
$.getJSON(url,{format:“json”}).done(函数(数据){
var profile_image=”http://www.example.com/“+数据[0]。配置文件\图像;
var profile_username=数据[0]。用户名+“”;
var profile_email_address=数据[0]。email_address+“”;
$(“#profile_pic”).attr('src',profile_image);
$(“#profile_username”).html(profile_username);
$(“#profile_email_address”).html(profile_email_address);
});//end$.getJSON
}

回显编码标记后,是否将其插入html文档的某个位置?并且可能会问,为什么要在视图中返回配置文件json数据,而不是直接从控制器返回?@DavidDomain hi。谢谢。当令牌返回时,我正在将其存储在本地存储器中。。你看,我希望用户在注销之前一直登录应用程序。因此,如果他们关闭应用程序,那么下次打开应用程序时,他们不必输入登录详细信息,但我通过将其发布到服务器来检查本地存储中的用户令牌是否有效。回答你们的下一个问题:我最近刚在大学里学过codeIgniter,几个月前我们被告知要严格按照MVC来做作业。对于这个项目,我从未想过从控制器输出它。这样做会更好吗?@DavidDomain我在这里了解了令牌身份验证:因此,在做任何其他事情之前,基本上检查本地存储中是否存在记住令牌。如果它确实使用toke发出post请求,该请求将由控制器内的方法处理,该方法负责应用程序的身份验证功能。你已经拥有了你所需要的一切。你到底有什么问题?插入用户名和电子邮件不应该是一个安全问题,只要你不插入身份验证数据,如密码,甚至不加密或哈希密码,只是不要。要在每个请求上同时发送您的remember令牌,而不必将其添加到单个ajax请求中,您可以使用
$.ajaxSetup({headers:{'token':'tokenfrom local storage'}})
查看。快乐编码(英文)回显编码标记后,是否将其插入html文档的某个位置?并且可能会问,为什么要在视图中返回配置文件json数据,而不是直接从控制器返回?@DavidDomain hi。谢谢。当令牌返回时,我正在将其存储在本地存储器中。。你看,我希望用户在注销之前一直登录应用程序。因此,如果他们关闭应用程序,那么下次打开应用程序时,他们不必输入登录详细信息,但我通过将其发布到服务器来检查本地存储中的用户令牌是否有效。回答你们的下一个问题:我最近刚在大学里学过codeIgniter,几个月前我们被告知要严格按照MVC来做作业。对于这个项目,我从未想过从控制器输出它。这样做会更好吗?@DavidDomain我在这里了解了令牌身份验证:因此,在做任何其他事情之前,基本上检查本地存储中是否存在记住令牌。如果它确实使用toke发出post请求,该请求将由控制器内的方法处理,该方法负责应用程序的身份验证功能。你已经拥有了你所需要的一切。你到底有什么问题?插入用户名和电子邮件不应该是一个安全问题,只要你不插入身份验证数据,如密码,甚至不加密或哈希密码,只是不要。要在每个请求上同时发送您的remember令牌,而不必将其添加到单个ajax请求中,您可以使用
$.ajaxSetup({headers:{'token':'tokenfrom local storage'}})
查看。快乐编码(英文)