使用Devise和devise_ldap_authenticatable时如何捕获Ldap错误并重定向到指定的页面 [英] How to catch Ldap error and redirect to specified page when using Devise and devise_ldap_authenticatable

查看:209
本文介绍了使用Devise和devise_ldap_authenticatable时如何捕获Ldap错误并重定向到指定的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在登录过程中出现ldap错误时显示我的登录页面,其中包含系统目前无法登录,请稍后重试。



目前,我已经使用custom_failure.rb方法,如
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to- a-specific-page-when-the-user-can-not-the-authenticated
,它能够捕获错误,如无效的用户名/密码,我已将其重定向到登录页面
根据需要显示错误消息没有问题。
但是这种方法无法捕获ldap错误。



这些ldap错误显示为一个非常原始的HTML页面,如下所示:



Errno :: Devise中的ETIMEDOUT :: SessionsController#create



连接尝试失败,因为连接方在一段时间后没有正确响应时间或建立的连接失败,因为连接的主机无法响应。 - 连接(2)



Rails.root:C:/extratxt/2.2.0/source/ruby/messaging
应用程序跟踪|框架跟踪|完整追踪



请求



参数:

  {utf8=>✓,
user=> {username=>administrator,
password=> ;[FILTERED],
remember_me=>0},
commit=>登录}
pre>

显示会话转储



显示env转储
响应



标题:





如何防止这一点,而是显示我指定的页面(例如登录页面和我自己的错误信息)?
任何想法?感谢

解决方案

一种方法是覆盖创建会话操作,但是需要几个步骤才能使其工作。首先,复制设计视图(如果您还没有这样做)与

  rails g devise:views 

然后,您需要创建一个会话_controller.rb文件,扩展Devise :: SessionsController:

 #/app/controllers/sessions_controller.rb 
class SessionsController< Devise :: SessionsController

#复制自[...] / gems / devise-1.4.5 / app / controllers / sessions_controller.rb
#要找到宝石的路径,请使用'bundle show devise'
def create

begin
resource = warden.authenticate!(:scope => resource_name,:recall =>#{controller_path} #new )
set_flash_message(:notice,:signed_in)if is_navigational_format?
sign_in(resource_name,resource)
respond_with resource,:location => redirect_location(resource_name,resource)
rescue Net :: LDAP :: LdapError
#处理你的错误
respond_to do | format |
format.html {redirect_to new_user_session_path,:notice => $ !. to_s}
format.json {render json:$ !. to_s}
end
end

end
end

最后,在您的config / routes.rb中,您需要告诉设计使用您的自定义控制器:

  devise_for:users,:controllers => {:sessions => :会话} 


I would like to show my login page with error message like "The system cannot log you on at this time. Please try again later" when there is ldap error during the login process.

Currently, I have used the custom_failure.rb approach as described in https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated and it is able to catch error like invalid username / password and I have redirected it to the login page showing the error messages as desired without problem. But this approach cannot catch the ldap errors.

These ldap errors are shown as a very raw html page like the following:

Errno::ETIMEDOUT in Devise::SessionsController#create

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. - connect(2)

Rails.root: C:/extratxt/2.2.0/source/ruby/messaging Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"utf8"=>"✓",
 "user"=>{"username"=>"administrator",
 "password"=>"[FILTERED]",
 "remember_me"=>"0"},
 "commit"=>"Sign in"}

Show session dump

Show env dump Response

Headers:

None

How can I prevent this and instead show my specified page (e.g. login page with my own error message) ? Any idea ? Thanks

解决方案

One way is to override the create session action, but there are a couple steps required to get it working. First, copy the devise views (if you haven't already done so) with

rails g devise:views

Then you need to create a sessions_controller.rb file that extends Devise::SessionsController:

# /app/controllers/sessions_controller.rb
class SessionsController < Devise::SessionsController

  # copied from [...]/gems/devise-1.4.5/app/controllers/sessions_controller.rb
  # To find the path to your gem, use 'bundle show devise' 
  def create

    begin
      resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
      set_flash_message(:notice, :signed_in) if is_navigational_format?
      sign_in(resource_name, resource)
      respond_with resource, :location => redirect_location(resource_name, resource)
    rescue Net::LDAP::LdapError
      # handle your error
      respond_to do |format|
        format.html { redirect_to new_user_session_path, :notice => $!.to_s }
        format.json { render json: $!.to_s }
      end
    end

  end
end

Finally, in your config/routes.rb, you need to tell devise to use your customized controller:

devise_for :users, :controllers => { :sessions => :sessions }

这篇关于使用Devise和devise_ldap_authenticatable时如何捕获Ldap错误并重定向到指定的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆