Redmine 支持 Apache HTTP Server 的认证

默认情况下 Redmine 是不支持从 header 中获取登录信息,这样就无法使用 Apache HTTP Server 的认证信息。
因此在我们对站点进行了LDAP认证后,还需要再次输入用户名和密码,导致重复登陆,影响用户体验。
我们可以通过简单的修改几个方法达到这个目的,从而做到统一登录。
思路
登录增加一层判断,使得如果没有登录信息,则 header 中去获取登录信息,进行模拟登陆。
修改办法
需要修改的文件为 apps/redmine/htdocs/app/controllers/account_controller.rb

从 header 中获取验证信息,验证信息获取后需要 base64 解密,解密后是 `username:password` 的内容。

      def httpbase_authentication
        require 'base64'
        auth = request.headers['Authorization'].split(' ')
        userpass= Base64.decode64(auth[1]).split(':')
        
        user = User.try_to_login(userpass[0], userpass[1])
        
        if user.nil?
          password_authentication
        elsif user.new_record?
          onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
        else
          successful_authentication(user)
        end
      end

* 修改 login 方法
  增加如果没有登录情况下,从 header 中获取登录信息的逻辑

      def login
        if request.get?
          if User.current.logged?
            redirect_to home_url
          elsif request.headers['Authorization'].nil?
            httpbase_authentication
          end
        else
          authenticate_user
        end
      rescue AuthSourceException => e
        logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
        render_error :message => e.message
      end
 

猜你喜欢

转载自gogo1217.iteye.com/blog/2424046