Back

关于 RUBY CAS client 和 single sign out (about ruby-cas-client and single sign out )

发布时间: 2012-12-20 06:55:00

昨天搭建了ruby cas server, 今天使用了一下,效果良好。   ( I setup my CAS server yesterday, and it's running well) 

下面是使用ruby CAS CLIENT的步骤:  (参考:http://sg552.iteye.com/blog/1297538 )  ( below is about how to use rubycas-client ) 

0. 在 Gemfile中,加入:   
gem 'rubycas-client'
然后 $sudo bundle
注意: 不要使用 'rubycas-client-rails' 这个GEM。    ( notice: don't use 'rubycas-client-rails' gem, it's unstable and unnecessary  )

1. environment.rb: 的结尾,加上:   ( add your cas server url to this file) 
CASClient::Frameworks::Rails::Filter.configure(
:cas_base_url => "http://192.168.56.10:8080/cas-server-webapp-3.4.11 "
)

2. application_controller.rb
before_filter CASClient::Frameworks::Rails::Filter

3. 进入某个controller : (例如 fish_controller.rb) , 定义退出的action  (define the logout method ) 

 
def class fish_controller
  def logout
     CASClient::Frameworks::Rails::Filter.logout(self)
  end
end

4. 修改 route.rb ,加入退出的链接: (add the naming route)

Dudufish::Application.routes.draw do
   resources :fish
   match '/logout' => "fish#logout", :as => :logout
end 

5. 修改 application.html.erb 布局文件,增加这个链接。 关键的一点是: 在经典的rails authorization 框架用,都是用 current_user 这个变量取得当前登陆的用户。 在CAS CLIENT中,用的是 session[:cas_user] , 只取得当前用户名,是一个string. 而不是一个model.   ( NOTICE:  use session[:cas_user] to get the current_user information.   ) 

<div style="border: 1px solid green">
<%= session[:cas_user] %>
</div>
<% if session[:cas_user] %>
<%= link_to "logout" , logout_path %>
<% end %>

至于single sign out, 暂时就不设置了。  不过有需要的同学可以参考一下:  (about single sign on, I decide to leave it there and don't touch it . but if you insist on using it, follow the steps below ) 

1. 在服务器端(ruby cas server) ,修改 config.yml,    (change the config file from server side: )
enable_single_sign_out = true

2. 在客户端, (from client side) 
  2.1 使用 数据库保存  session     
    config.action_controller.session_store = :active_record_store
  2.2 修改application.rb 
    self.allow_forgery_protection = false
  2.3 在 CAS client的设置中,  
    :enable_single_sign_out => true

我本来想这样弄,但是考虑到:   (considering the factors below, I don't want to use single-sign-on ) 
1. 这样太麻烦。 我怕让其他应用该起来太麻烦。  ( complicated, for both ruby and other language applications )
2. 可以把 session有效时间缩短些,比如 闲置15分钟,就需要重新登录(默认是5分钟)  ( to shorten the unused-session-time could satisfy our need ) 一样可以
3. 去掉了 forgery protection 安全性下降太多。 ( it's dangerous to disable the forgery protection) 

所以 我不打算使用 single sign on 这个特性。

Back