我的 Mojolicious 应用程序模块中有以下代码。当我使用 Test::Mojo $t->post_ok('hosts', encode_json( $json_string ) )->status_is(200) 运行它时,应用程序运行良好。当我使用 Firefox 休息客户端时,我从未通过 add_condition 路由修饰符“访问”。我转储了 $can 的结果(在 add_condition 块中),它是“1”。所以,在我看来,这应该继续到控制器上(就像我运行测试时所做的那样)。使用 FF 休息客户端时,这将返回 404,因为在访问条件返回 1 后没有任何反应。
知道为什么应用程序在这两种情况下会以不同的方式运行吗?
$self->plugin(
'json_rpc_dispatcher',
services => {
'/auth' => Package::Controller::Auth->new,
'/user' => Package::Controller::User->new,
'/hosts' => Package::Controller::Hosts->new,
}
);
$self->app->routes->add_condition(
access => sub {
my ($rt, $cntrlr, $stuff) = @_;
my $sess = $cntrlr->stash('mojo.session');
my $path = $cntrlr->req->url->path->parts;
my $can = $self->can_access({
route => $path->[0],
args => $sess,
});
$can && return 1;
$cntrlr->render_json({
error => 'Not Authorized',
ok => 0,
});
}
);
$self->hook( before_dispatch => sub {
my $this = shift;
my $params = $this->tx->req->json;
if( !$this->session('auth') ) {
my $login = $self->login({
user => $params->{params}{user},
password => $params->{params}{password},
});
return { error => 'Invalid Username or password' } if $login->{error};
my $access = $self->has_access(
$params->{params}{user}, $params->{params}{password}
);
return { error => 'Invalid credentials' } unless keys %{ $access };
$this->session( auth => { dn => $params->{params}{user}, perms => $access } );
}
else {
$r->find('auth' )->over('access');
$r->find('user' )->over('access');
$r->find('hosts')->over('access');
}
} ); # hook