Mojolicious会话不会过期

Pet*_*tru 6 session mojolicious perl5

我正在使用mojolicious构建一个Web应用程序.注销功能仅在本地计算机上运行应用程序时有效.当我尝试注销服务器上运行的应用程序时,会话不会过期,我仍然保持登录状态.

当我们通过POST请求更改注销而不是get时,就会发生这种情况.

我们调用logout的方式是来自前端的AJAX调用:

function do_logout() {
   $.post( "<%= url_for('on_logout') %>", function() {});
}
Run Code Online (Sandbox Code Playgroud)

退出路线:

$if_login->post('/logout')->name('on_logout')->to('user#on_logout');
Run Code Online (Sandbox Code Playgroud)

注销控制器:

sub on_logout {
  my $self = shift;
  $self->session(expires => 1);
  return $self->redirect_to('home');
}
Run Code Online (Sandbox Code Playgroud)

将会话设置为过期的行被调用,但在重定向之后,会话仍包含登录的用户名.

Bia*_*nca 1

我们终于发现了错误,请求是使用

<a href="" onclick="do_logout()"></a>
Run Code Online (Sandbox Code Playgroud)

这基本上是同时执行 2 个操作并创建竞争条件。这是相关的代码片段

# Relevant routes
my $if_login = $r->under('/')->to('user#is_logged_in');
$if_login->post('/logout')->name('on_logout')->to('user#on_logout');

# Controller functions
sub on_logout {
  my $self = shift;
  $self->session(expires => 1);

  return $self->render(json => '{success: "true"}');
}

sub is_logged_in {
  my $self = shift;

  say $self->session('username');  # Sometimes after on_logout this is still
                                   # defined and equal to the username.
  return 1 if($self->session('username'));

  $self->render(
    template => 'permission/not_logged_in',
    status => 403
  );
  return;
}

# Front end
<a href="" onclick='do_logout();'>
 <%= l('Log out') %>
</a>

<script>
function do_logout() {
  $.post( "<%= url_for('on_logout') %>", function() {
}).fail(function() {
  alert( "error logging out" );
}).done(function( data ) {
  alert( "Data: " + data );
}).always(function() {
  alert( "finished" );
});
}
</script>
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!