2016-03-09 20 views
3

我有一個Grails應用程序,需要訪問Spring的AuthenticationManager/ProviderManager實例,所以我可以將它提供給我的RemoteAuthenticationProviderImpl實例。 我在我的ApiController.groovy中有以下代碼,我想驗證遠程客戶端,這些客戶端正在登錄到另一個Grails應用程序併發回他們授予的權限。如何獲取內部Spring對象的實例

def remoteAuthentication() { 
    def authHeader = request.getHeader('Authorization') 
    if (authHeader && apiService.validate(authHeader)) { 
     try { 
      def remoteAuthenticationManager = new RemoteAuthenticationManagerImpl() 
      def response = remoteAuthenticationManager.attemptAuthentication("admin", "password1") 
     } catch (Exception) {} 
     render response as JSON 
    } 
    response.status = 401 
    render(status: 401, text: 'Failed to authenticate.') 
} 

RemoteAuthenticationManagerImpl記錄在here。可以看出,我需要提供一個AuthenticationManager。我想我需要通過一個bean注入,但是我無法讓它工作。

+0

'RemoteAuthenticationManager(Impl)'用於RPC。它在這方面看起來錯位。你想達到什麼目的? – holmis83

+0

噢,謝謝。我有一個使用用戶名和密碼的grails應用程序,另一個沒有它們。我試圖將該服務器上的登錄嘗試轉發給其他服務器,以便可以對其進行身份驗證。然後它應該返回授予的權限並在缺少憑證的服務器上登錄用戶。 – nhaesler

+0

我正在努力的主要部分是如何在給定用戶名和密碼的情況下對我的api中的用戶進行身份驗證。因爲我實際上並沒有使用Spring的內部工作方式,所以我不知道該怎麼做,也沒有親自實現整個過程。這似乎不是正確的做法。你可能有一個想法,我怎麼能做到這一點? – nhaesler

回答

0

我自己解決了這個問題。我的錯誤是,我把 <ref local="authenticationManager" />而不是<ref bean="authenticationManager" />放到我的resources.xml中。