允许sdk支持多种身份验证方案的Java设计模式

允许sdk支持多种身份验证方案的Java设计模式,java,oop,design-patterns,Java,Oop,Design Patterns,我正在为http api设计一个sdk/客户端库。api支持多种身份验证机制:基本身份验证、oauth、摘要等 我的客户端库目前很简单,如下所示: public MyAPIRestClient implements MyAPIClient { public MyAPIRestClient(String endpoint, String user, String pass){ login(user, pass) } public void login(String u

我正在为http api设计一个sdk/客户端库。api支持多种身份验证机制:基本身份验证、oauth、摘要等

我的客户端库目前很简单,如下所示:

public MyAPIRestClient implements MyAPIClient {

   public MyAPIRestClient(String endpoint, String user, String pass){
    login(user, pass)
   }

   public void login(String user, String pass){
    //http code to login and get a cookie etc.
   }

   public Book getBook(String name){
    // http code to get a book
   }

}

使我能够拥有多个身份验证机制,而不必在同一客户机类中编写所有可能的身份验证方法的最佳设计模式是什么?为了使将来的身份验证机制能够轻松地被注入?

当这个问题非常复杂、困难,并且有非常好的成熟解决方案(包括ApacheShiro和Spring安全性)时,您自己做这件事是很奇怪的。但我认为这只是设计模式中的一个练习

我可能会选择责任链模式。有一个通用接口,如:

interface Authenticator {
    /**
    * Analyze the request and return an Authentication object
    * upon success, or null otherwise
    */
    Authentication authenticate(HttpServletRequest request);
}
为每个身份验证机制实现它,检查cookies、POST数据或您拥有的任何东西

然后,像这样称呼他们:

public Authentication authenticateRequest(HttpServletRequest request) {
    for (Authenticator ator : supportedAuthenticators) {
        Authentication a = ator.authenticate(request);
        if (a != null) {
            // Logged in successfully!
            return a;
        }
    }
    throw new LoginFailed();
}

当这个问题非常复杂、困难,并且有非常好的成熟解决方案(包括ApacheShiro和Spring安全性)时,独自做这件事是一件很奇怪的事情。但我认为这只是设计模式中的一个练习

我可能会选择责任链模式。有一个通用接口,如:

interface Authenticator {
    /**
    * Analyze the request and return an Authentication object
    * upon success, or null otherwise
    */
    Authentication authenticate(HttpServletRequest request);
}
为每个身份验证机制实现它,检查cookies、POST数据或您拥有的任何东西

然后,像这样称呼他们:

public Authentication authenticateRequest(HttpServletRequest request) {
    for (Authenticator ator : supportedAuthenticators) {
        Authentication a = ator.authenticate(request);
        if (a != null) {
            // Logged in successfully!
            return a;
        }
    }
    throw new LoginFailed();
}

当这个问题非常复杂、困难,并且有非常好的成熟解决方案(包括ApacheShiro和Spring安全性)时,独自做这件事是一件很奇怪的事情。但我认为这只是设计模式中的一个练习

我可能会选择责任链模式。有一个通用接口,如:

interface Authenticator {
    /**
    * Analyze the request and return an Authentication object
    * upon success, or null otherwise
    */
    Authentication authenticate(HttpServletRequest request);
}
为每个身份验证机制实现它,检查cookies、POST数据或您拥有的任何东西

然后,像这样称呼他们:

public Authentication authenticateRequest(HttpServletRequest request) {
    for (Authenticator ator : supportedAuthenticators) {
        Authentication a = ator.authenticate(request);
        if (a != null) {
            // Logged in successfully!
            return a;
        }
    }
    throw new LoginFailed();
}

当这个问题非常复杂、困难,并且有非常好的成熟解决方案(包括ApacheShiro和Spring安全性)时,独自做这件事是一件很奇怪的事情。但我认为这只是设计模式中的一个练习

我可能会选择责任链模式。有一个通用接口,如:

interface Authenticator {
    /**
    * Analyze the request and return an Authentication object
    * upon success, or null otherwise
    */
    Authentication authenticate(HttpServletRequest request);
}
为每个身份验证机制实现它,检查cookies、POST数据或您拥有的任何东西

然后,像这样称呼他们:

public Authentication authenticateRequest(HttpServletRequest request) {
    for (Authenticator ator : supportedAuthenticators) {
        Authentication a = ator.authenticate(request);
        if (a != null) {
            // Logged in successfully!
            return a;
        }
    }
    throw new LoginFailed();
}

我建议有一个接口,它将具有基本身份验证方法的签名,而实现类可以具有这些身份验证方法的不同定义。

我建议有一个接口,它将具有基本身份验证方法的签名,实现类可以对这些身份验证方法有不同的定义。

我建议有一个接口,该接口将具有基本身份验证方法的签名,实现类可以对这些身份验证方法有不同的定义。

我建议有一个接口,该接口将具有基本身份验证方法的签名,实现类可以对这些身份验证方法有不同的定义。

我首先想到的是策略模式:

public abstract class Authenticator{ //or interface
    public abstract boolean login(String user, String password);
}
验证器是模式。您可以使用各种身份验证方法来扩展/实现它。在主类中,您在成员中持有特定模式,然后直接调用该模式:

public class Main{
    private Authenticator auth;

    //choose auth in constructor or by choosing it in a List in some GUI
    //and create an Object that extends/implements Authenticator

    public void login(String user, String password){
        if(auth.login(user, password)){
            //do whatever is needed to complete login
        }
    }

}
通过这种方式,您可以随时控制使用哪些身份验证方法


如果您将
auth
更改为数组或ArrayList,它本质上与@Konrad Garus answer相同。

我首先想到的是策略模式:

public abstract class Authenticator{ //or interface
    public abstract boolean login(String user, String password);
}
验证器是模式。您可以使用各种身份验证方法来扩展/实现它。在主类中,您在成员中持有特定模式,然后直接调用该模式:

public class Main{
    private Authenticator auth;

    //choose auth in constructor or by choosing it in a List in some GUI
    //and create an Object that extends/implements Authenticator

    public void login(String user, String password){
        if(auth.login(user, password)){
            //do whatever is needed to complete login
        }
    }

}
通过这种方式,您可以随时控制使用哪些身份验证方法


如果您将
auth
更改为数组或ArrayList,它本质上与@Konrad Garus answer相同。

我首先想到的是策略模式:

public abstract class Authenticator{ //or interface
    public abstract boolean login(String user, String password);
}
验证器是模式。您可以使用各种身份验证方法来扩展/实现它。在主类中,您在成员中持有特定模式,然后直接调用该模式:

public class Main{
    private Authenticator auth;

    //choose auth in constructor or by choosing it in a List in some GUI
    //and create an Object that extends/implements Authenticator

    public void login(String user, String password){
        if(auth.login(user, password)){
            //do whatever is needed to complete login
        }
    }

}
通过这种方式,您可以随时控制使用哪些身份验证方法


如果您将
auth
更改为数组或ArrayList,它本质上与@Konrad Garus answer相同。

我首先想到的是策略模式:

public abstract class Authenticator{ //or interface
    public abstract boolean login(String user, String password);
}
验证器是模式。您可以使用各种身份验证方法来扩展/实现它。在主类中,您在成员中持有特定模式,然后直接调用该模式:

public class Main{
    private Authenticator auth;

    //choose auth in constructor or by choosing it in a List in some GUI
    //and create an Object that extends/implements Authenticator

    public void login(String user, String password){
        if(auth.login(user, password)){
            //do whatever is needed to complete login
        }
    }

}
通过这种方式,您可以随时控制使用哪些身份验证方法


如果您将
auth
更改为数组或ArrayList,它本质上与@Konrad Garus answer相同。

听起来工厂模式可能会有所帮助。我会考虑从你的问题中删除“最佳”一词,因为你可能会面临“基于意见”的理由。我会考虑从你的问题中删除“最佳”一词,因为你可能会面临“基于意见”的理由。我会考虑从你的问题中删除“最佳”一词,因为你可能会面临“基于意见”的理由。我会考虑从你的问题中删除“最佳”一词,因为你可能会面临“基于意见”的理由。谢谢,如果我错了请原谅,但是我认为你的例子更多的是服务器端,你可以分析请求来查看使用什么类型的Auth。我希望它用于客户端,客户端的用户可以说new RestClient().authentication(BASIC).build()类型的东西。所以