【问题标题】:Can anyone recommend an authentication library for CodeIgniter 1.7.x? [closed]任何人都可以推荐 CodeIgniter 1.7.x 的身份验证库吗? [关闭]
【发布时间】:2010-03-06 13:55:41
【问题描述】:

我刚开始使用 CodeIgniter 1.7.2 并立即注意到没有用于用户身份验证的内置库。

我不需要任何花哨的东西。它只是对我的后台系统中的用户进行身份验证。我不需要用户和组或权限等。我只需要一个脚本来允许用户登录。如果用户试图访问一个页面并且他们没有登录,他们应该被拒绝。但它必须是安全的,如果它使用 CI 验证库会很好。

谁能推荐一个用于 CI 1.7.2 的库?

我确实注意到 StackOverflow 上有一篇类似的帖子,但它已经过时了,而且大多数推荐的库要么不支持 1.7.2,要么不再维护。

最重要的是它必须安全且简单。

【问题讨论】:

    标签: php security authentication codeigniter


    【解决方案1】:

    DX Auth 或 Freak Auth 是非常棒的库,几乎可以满足您的所有需求

    http://codeigniter.com/wiki/DX_Auth/

    http://codeigniter.com/wiki/FreakAuth/

    【讨论】:

    • 谢谢。我决定尝试 DX_Auth。它看起来非常全面且易于使用。
    【解决方案2】:

    我倾向于推出自己的简单身份验证库。

    首先,这是身份验证库。它在会话中保留一个用户 ID 令牌。在进行身份验证时,它会检查此令牌是否存在。

    应用程序/库/Auth.php

    class Auth
    {
        var $ci;
        var $user_id;
    
        function Auth()
        {
            // Get CodeIgniter instance
            $this->ci = get_instance();
    
            // Fetch token from the session
            $this->user_id = $this->ci->session->userdata('user_id');
        }
    
        function check()
        {
            return $this->user_id != null;
        }
    
        function login($user_id)
        {
            // Set token in the session
            $this->ci->session->set_userdata('user_id', $user_id);
    
            $this->user_id = $user_id;
        }
    
        function logout()
        {
            // Remove token from the session
            $this->ci->session->unset_userdata('user_id');
    
            $this->user_id = null;
        }
    }
    

    我创建自己的基本控制器并在那里进行身份验证。为方便起见,如果经过身份验证,基本控制器会加载并存储当前用户。

    application/libraries/MY_Controller.php

    class MY_Controller extends Controller
    {
        var $user;
    
        function MY_Controller()
        {
            parent::Controller();
        }
    
        function do_auth()
        {
            if ($this->auth->check())
            {
                // Authenticated. Fetch current user
                $this->user = $this->user_model->get_user($this->auth->user_id);
            }
            else
            {
                // Not authenticated. Redirect to login page
                redirect('users/login');
            }
        }
    }
    

    然后在任何操作中我都可以调用基本控制器的身份验证函数。

    class Items extends MY_Controller
    {
        function Items()
        {
            parent::MY_Controller();
        }
    
        function create()
        {
            // Do authentication
            $this->do_auth();
    
            // Continue with handling request
        }
    }
    

    如果我愿意,我还可以保护整个控制器。

    class Items extends MY_Controller
    {
        function Items()
        {
            parent::MY_Controller();
    
            // Secure entire controller
            $this->do_auth();
        }
    }
    

    我将登录和注销操作放在用户控制器中。在登录操作中,我验证用户的凭据并登录用户。

    class Users extends MY_Controller
    {
        function Users()
        {
            parent::MY_Controller();
        }
    
        function login()
        {
            // Verify form input
            $this->load->library('form_validation');
            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
    
            if ($this->form_validation->run())
            {
                // Fetch the user based on credentials supplied
                $user = $this->user_model->get_user_by_credentials($this->input->post('username', true), $this->input->post('password', true));
    
                if ($user != null)
                {
                    // Credentials verified. Log the user in.
                    $this->auth->login($user->user_id);
                    redirect('');
                }
                else
                {
                    // Login failed. Show the login page.
                    $this->load->view('users/login', array('login_failed' => true));
                }
            }
            else
            {
                // Yet to authenticate. Show the login page.
                $this->load->view('users/login', array('login_failed' => false));
            }
        }
    
        function logout()
        {
            $this->auth->logout();
            redirect('users/login');
        }
    }
    

    【讨论】:

    • 你为什么要扩展控制器来完成应该在登录函数或库中完成的事情?
    • 我本来可以那样做的。但是随后身份验证库将需要重定向到我的特定登录页面。我认为这更多是我的应用程序控制器的关注点。我认为身份验证库应该只关心检查令牌的存在。
    • 哦,好吧,我通常采取的方法是记录他们尝试访问的页面,将他们重定向到登录/注册,成功登录后他们会被引导回原始页面。但是您的解决方案也有效......请记住它:)
    • 是的 - 我通常会这样做 :-) 我会通过查询字符串参数传递他们尝试访问登录页面的 URL,然后在他们登录后将它们重定向回来但我只是想让这里的代码尽可能简单。
    【解决方案3】:

    嗯,不是很特定于 CI 的是 .htaccess 用户/密码系统。特别是如果您只有几个用户。

    设置简单,并且内置于每台 Apache 服务器中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 2016-01-23
      • 2011-02-11
      相关资源
      最近更新 更多