java - 策略设计模式

标签 java url authentication design-patterns

我有一个支持 URLConnection 的 AuthenticationHandler 接口(interface),但现在我使用的是 Apache HTTP 客户端。我想为两种连接类型(URLConnection 和 HTTP 客户端)提供一个通用的身份验证接口(interface),但它们都有不同的参数和不同的功能。

我将如何设计这个?策略模式是要走的路吗?

import java.net.URLConnection;
import java.util.List;

public interface AuthenticationHandler {

/**
 * this needs to be called by everyone that needs direct access to a link which may have
 * security access rules.
 */
void trustAll();

/**
 *
 * @param URLconnection where you set access state parameters or anything access related
 * @param slice where you could get access config
 * @param initializeSlice is true if you want the proxy to hibernate initialize all hibernated objects
 * @return
 * @throws ConnectionException
 */
void authenticate(URLConnection conn) throws ConnectionException;

List<String> getSingleCookie();

void setSingleCookie(List<String> singleCookies);

CookieManager getCookieManager();

void setCookieManager(CookieManager cookieManager);

boolean isKeepGeneratedCookie();

void setKeepGeneratedCookie(boolean keepGeneratedCookie);

}

我主要担心的是

void authenticate(URLConnection conn) throws ConnectionException;

它最初采用 URLConnection conn,但现在我们也想添加对 HTTP 客户端的支持。

最佳答案

对于策略模式,你应该使用这样的东西:

public class AuthenticationHandlerImpl implements AuthenticationHandler {

    private Authenticator authenticator;

    void authenticate() throws ConnectionException {
        authenticator.authenticate();
    };

    public void setAuthenticator(final Authenticator  authenticator){
        this.authenticator = authenticator;
    }

}

interface Authenticator {
    void authenticate();
    void setLogin(String login);
    void setPassword(String password);
}

class URLAuthenticator implements Authenticator {
    public void authenticate() {
        //use URLConnection
    };
}

class HTTPClientAuthenticator implements Authenticator {
    public void authenticate() {
        //use HTTPClient
    };
}

使用示例:

AuthenticationHandler handler = new AuthenticationHandlerImpl();
Authenticator authenticator = new HTTPClientAuthenticator();
//or
//Authenticator authenticator = new URLAuthenticator();
authenticator.setLogin(...);
authenticator.setPassword(...);
handler.setAuthenticator(authenticator)

handler.authenticate();

要创建 Authenticator,您可以使用模式 FactoryMathod:

class AuthenticatorFactory {

    private AuthenticatorFactory(){}

    //type of param may be enum or other
    public static Authenticator createAuthenticator(... param) {
        if (param == ...) {
            return new URLAuthenticator();
        } else if (param == ...) {
            return new HTTPClientAuthenticator();
        }
    }
}

关于java - 策略设计模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19858689/

相关文章:

java - Saml 2.0 请求编码

java - 如何用 Java 发布 https 帖子?

url - 将编码字符添加到 url 会破坏 htaccess

java - 如何在 SQL 表中只插入一列?

http - 如果 http ://is missing,使用 url.ResolveReference() 解析错误的 URL

java - Selenium 操作方法 : Get url from an <a> tag that has no href attribute

authentication - 如何在 Rails 3.2 中使用回形针毫无错误地检索 facebook、twitter 和 google 个人资料图像

c# - ASP.NET Core 2.2 中的 Azure AD 身份验证

java - HTTP代理可以修改TCP Java字节流吗?

java - Java 中的 UTF-8 和 UTF-16