문제

Rails 앱에 RESTFUL_Authentication 플러그인이 설치되어 있으며 Sessions_Controller가 다음과 같은 파괴 방법이 있습니다.

def destroy
  self.current_user.forget_me if logged_in?
  cookies.delete :auth_token
  reset_session
  flash[:notice] = "You have been logged out."
  redirect_back_or_default('/')
end

응용 프로그램 컨트롤러에는 다음과 같습니다.

before_filter :login_required

그리고 sessions_controller에는 다음과 같습니다.

skip_before_filter :login_required

내 문제는 사용자가 HTTP 기본 인증을 인증 할 때 로그 아웃하지 않는다는 것입니다. 세션은 파괴되었지만 사용자는 문제없이 제한된 페이지로 이동할 수 있습니다. 이 문제는 플러그인을 통한 세션 인증에서는 발생하지 않습니다. 이 메소드가 기본 인증을 제거하도록하려면 어떻게해야합니까?

도움이 되었습니까?

해결책

이 상황에서 사용자를 "로그 아웃"하기 위해 서버 측을 수행 할 수있는 것은 없습니다. 사용자가 기본 인증을 통해 로그인하면 브라우저는 인증 정보를 저장하고 모든 요청과 함께 HTTP 헤더를 통해 인증 매개 변수를 보냅니다. 사용자가 기본 인증으로 로그인하면 브라우저 창을 닫아 로그 아웃해야합니다.

다른 팁

세션 변수를 사용하여 어떤 사용자가 로그인했는지 기억하여이를 극복하는 매우 흥미로운 방법을 찾았습니다. 아이디어는 브라우저가 여전히 인증 데이터를 보내더라도 사용자가 로그 아웃하기로 선택했기 때문에이를 무시하고 있다는 것입니다. 새 로그인 요청이 브라우저로 전송 될 때마다 모든 인증 데이터가 지워지므로 사용자는 언제든지 로그인 할 수 있습니다.

class ApplicationController < ActionController::Base
  # ...

  before_filter :authenticate

  protected

  def authenticate
    authenticate_with_http_basic do |username, password|
      @current_user = User.find_by_name_and_crypted_password(username, User.digest(password))
      @current_user = nil if @current_user && session[:logged_out] == @current_user.id
      !@current_user.nil?
    end
  end

  def authenticate!
    return if @current_user
    session[:authenticate_uri] = request.request_uri
    redirect_to('/login')
  end
end

그런 다음 이벤트 컨트롤러에서 다음을 수행합니다.

class EventsController < ApplicationController
  before_filter :authenticate!, :only => [ :new, :create, :edit, :update ]
  #...
end

그리고 마지막으로 내 세션 컨트롤러는 다음과 같습니다.

class SessionController < ApplicationController
  before_filter :authenticate!, :only => [ :create ]

  def create
    if session[:authenticate_uri]
      redirect_to(session[:authenticate_uri])
      session[:authenticate_uri] = nil
    else
      redirect_to(new_event_path)
    end
  end

  def destroy
    session[:logged_out] = @current_user.id
    redirect_to '/'
  end

  protected

  def authenticate!
    authenticate_or_request_with_http_basic("Rankings") do |username, password|
      @current_user = User.find_by_name_and_crypted_password(username, User.digest(password))
      if @current_user && session[:logged_out] == @current_user.id
        @current_user = nil
        session[:logged_out] = nil
      end
      !@current_user.nil?
    end
  end

end

그리고 당신의 경로를 잊지 마십시오!

  map.logout 'login', :controller => 'session', :action => 'create'
  map.logout 'logout', :controller => 'session', :action => 'destroy'

이것은 IE 6 SP1+에만 작동합니다.

javascript:void(document.execCommand('ClearAuthenticationCache', false)); 

http://msdn.microsoft.com/en-us/library/ms536979(vs.85).aspx

이렇게하면 사용자가 현재 로그인 한 모든 사이트의 캐시가 지워집니다 (IE 인스턴스 내에서).

흠, 클라이언트 브라우저가 HTTP 기본 인증 자격 증명을 캐싱하고 매번 재 중지하는 것처럼 들립니다. 이 경우에는 통제 할 수 없습니다. 보호를 받고자하는 조치는 RESTFUL_Authentication 플러그인을 위해 적절한 Prever_Filter로 보호되어야합니다.

require_authentication

그래서 당신의 컨트롤러에서 당신은 가질 것입니다

before_filter :require_authentication

HTTP 인증은 상태가없는 것입니다. 즉, 서버는 인증 된 "세션"을 추적하지 않습니다. 따라서 클라이언트는 매번이를 공급해야합니다 (따라서 자주 확인란 '이 자격 증명을 저장'). 따라서 서버에 대한 방법이 없습니다. 클라이언트 자격 증명을 지우려면 이것은 사양의 일부입니다. Wikipedia 항목을 참조하십시오

http://en.wikipedia.org/wiki/basic_access_authentication

구체적으로, "단점"섹션을보십시오.

Authenticated_sytem에서 login_from_basic_auth를 읽었습니다.

    def login_from_basic_auth
      false
#      authenticate_with_http_basic do |login, password|
#        self.current_user = User.authenticate(login, password)
#      end
    end

이것을 해결하는 한 가지 방법은 "기본 HTTP 인증"을 완전히 비활성화하는 것입니다.

그러나 AJAX 작업 중에 좋은 사용자 경험을 위해서는이 문제가 필요했기 때문에 AJAX 작업에 대해서만이 인증을 활성화했습니다.

def login_from_basic_auth

  return false unless request.xhr?

   authenticate_with_http_basic do |login, password|
     self.current_user = User.authenticate(login, password)
   end
end

파티를 조금만 알고 있지만 로그 아웃을 원한다면 401을 렌더링 할 수 있습니다.

따라서 로그 아웃 방법은 다음과 같이 보일 수 있습니다.

def logout
  render :logout, status: 401
end

당신의 편안한 행동은 다음과 같이 보일 수 있습니다.

def destroy
  self.current_user.forget_me if logged_in?
  cookies.delete :auth_token
  reset_session
  redirect_to '/', status: 401, flash: "You have been logged out."
end

브라우저는 HTTP 기본 인증을 다시 제기합니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top