2014-04-10 49 views
-1

我有一個接口錯誤傳遞變量參數

import javax.naming.AuthenticationException; 

    import com.unboundid.ldap.sdk.LDAPException; 


    public interface AuthenticationServiceManager { 

/** 
* Creates the Connection for the specific user logging in, and binds the 
* user's credentials to object. 
* 
* @param userName 
*   The user name to authenticate . 
* @param password 
*   The user's password to check . 
* @param args 
*   is an object that holds variable arguments which can be used 
*   to authenticate using both LDAP and OpenId 
* @return boolean The connection status showing whether the user has been 
*   successfully authenticated or not. 
* @throws AuthenticationException 
*    If there is an error authenticating with the passed 
*    parameters 
**/ 

boolean authenticate(String username, String password, Object... args) 
     throws AuthenticationException, LDAPException; 

/** 
* Disconnects the connection. 
*/ 
void disconnect(); 

}

實現此接口

package com.cerner.jira.plugins.esig.servicemanagerimpl; 

    import javax.naming.AuthenticationException; 

    import com.cerner.jira.plugins.esig.servicemanager.AuthenticationServiceManager; 
    import com.unboundid.ldap.sdk.LDAPException; 

    public class AuthenticationManagerImpl implements AuthenticationServiceManager { 

@Override 
public boolean authenticate(String username, String password) 
     throws AuthenticationException, LDAPException { 

    return false; 
} 

@Override 
public boolean authenticate(String username, String password, String url) 
{ 
    return false; 


} 

@Override 
public void disconnect() { 
    // TODO Auto-generated method stub 

} 

}

我試圖創建一個接口的類可用於實現連接類。我應該能夠使用這個不同的認證,如LDAP,OpenId等;所以我想通過用戶名,密碼(如果它的LDAP)和可變數量的參數(如果OpenId)。我怎麼做?我試過這個。這是拋出錯誤。我如何初始化對象以保存可變參數?

錯誤:該方法的authenticate類型AuthenticationManagerImpl的(字符串,字符串)必須重寫或實現的超類型方法

+1

*「這是拋出錯誤。」* ***什麼***錯誤?總是,總是*包含這樣的基本信息。你爲什麼認爲這是可選的? –

+0

我認爲你在這裏重新發明了一下輪子。你有沒有檢查過呢? http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#ns-openid – Jackie

+0

我很抱歉,這是錯誤「The method authenticate(String ,字符串)的類型AuthenticationManagerImpl必須重寫或實現一個超類型方法「 – user3438489

回答

0

幾個可能性。

一種是通過多次聲明方法並使用不同的參數集來重載接口中的方法。

另一種方法是將參數列爲可變長度數據結構,如數組。命令行界面通過public static void main(String[] args)執行此操作,您可以傳遞多個命令行參數。你可以這樣做如下:

boolean authenticate(String username, String password, Object[] args) 
    throws AuthenticationException, LDAPException; 
0
@Override 
public boolean authenticate(String username, String password) 
     throws AuthenticationException, LDAPException { 

    return false; 
} 

@Override 
public boolean authenticate(String username, String password, String url) { 
    return false; 
} 

有一個在AuthenticationServiceManager接口接收String, String論點或論據String, String, String沒有方法。相反,你必須接收一個可變參數的單一authenticate的方法,所以,在你的類實現AuthenticationServiceManager界面,你應該實現的方法:

@Override 
public boolean authenticate(String username, String password, Object ... args) { 
    //handle args as you want/need 
    //if you don't need it for this specific case, then do nothing with the variable 
    //and yes, it is an ugly design =\ 
    return false; 
} 

另一種設計方案是有AuthenticationServiceManager接口boolean authenticate(String username, String password)方法其他方法,根據您的需要添加更多參數:

public interface AuthenticationServiceManager { 
    boolean authenticate(String username, String password) 
     throws AuthenticationException, LDAPException; 
    boolean authenticate(String username, String password, String url) 
     throws AuthenticationException, LDAPException; 
    boolean authenticate(String username, String password, String url, String anotherParam) 
     throws AuthenticationException, LDAPException; 
    //and on and on... 
    boolean authenticate(String username, String password, Object... args) 
     throws AuthenticationException, LDAPException; 

    /** 
    * Disconnects the connection. 
    */ 
    void disconnect(); 
} 
+0

我明白,但重點是我想讓方法通用...接受變量參數。那可能嗎。? – user3438489

+0

@ user3438489「varargs」聲明已經爲您處理。你還有什麼期望? –