Journal(2005) | Blog(2006) | RandomLink | WhoAmI | LiveBookmark | HomePage

<<Previous: RSS date  >>Next: 用 Perl6::Rules 来写 Formatter

Catalyst Authenticate

Category: Catalyst   Keywords: Authenticate Catalyst

针对不同的情况,Catalyst 提供了十几种验证用户的方法(详细的 svn catalyst)。
一般来说,我们都将用户名和密码存进一个 table 中(如表 users, 字段 usr/pwd 例子 fayland/123456)。
这次我使用 Catalyst::Plugin::Authenticate::CDBI 来验证用户。这个 plugin 非常简单。很容易就看懂源代码。
CPAN 中可能没有,不过 Catalystsvn 目录中有。

假设我有一个 catalyst FForum 工程。根据 Catalyst::Plugin::Authenticate::CDBI 的指导,我们先在 FForum.pm 中的 config 下(或里) setup 上加进

# Authenticate::CDBI
FForum->config->{authenticate}->{cdbi} = {
    class    => 'FForum::M::CDBI::Users',
    username => 'usr',
    password => 'pwd'
};

这里我们得生成一个 model CDBI::Users.
然后我们生成一个 perl script/fforum_server.pl controller Login, 然后在 Login.pm 中:

sub in : Local {
    my ( $self, $c ) = @_;
    
    # Retrieve params of username/password
    my $username = $c->req->param('username');
    my $password = $c->req->param('password');
    
    if ( $c->authenticate_cdbi($username, $password) ) {
        # successful authentication
        $c->res->output('Login!');
    } else {
        $c->res->output('Failed!');
    }
}

这样当我们访问 http://fayland:3000/login/in?username=fayland&password=123456 时就会显示 Login!
而 http://fayland:3000/login/in?username=fayland&password=12345 时就会显示 Failed!
在我们的命令行调式器中会显示错误的原因。比如这次为 User 'fayland' credentials is invalid'.

这里只是用最简单的直接调用 URL, 一般要详细添加用户登陆界面。
写本文并无其他用意,只是告诉各位如果碰到这种情况还可以考虑这种方法。

如果有时间我会介绍下 Session, 这个或许更有趣一些。

<<Previous: RSS date  >>Next: 用 Perl6::Rules 来写 Formatter

Options: +Del.icio.us

Related items Created on 2005-09-26 12:50:39, Last modified on 2005-09-26 12:59:14
Copyright 2004-2005 All Rights Reserved. Powered by Eplanet && Catalyst 5.62.