# HTTP

pac4j 允许你通过 HTTP 机制登录(如基本认证或表单传递)。

HTTP 客户端需要定义一个认证器来处理凭据验证。

除了具有默认 X509AuthenticatorX509Client,它从 X509 证书的 subjectDN 中提取标识符。

# 1)依赖

你需要使用以下模块:pac4j-http

<dependency>
    <groupId>org.pac4j</groupId>
    <artifactId>pac4j-http</artifactId>
    <version>${pac4j.version}</version>
</dependency>
1
2
3
4
5

# 2)客户端

你可以使用以下客户端,具体取决于凭据是什么以及它们在 HTTP 请求中的传递方式:

凭据 客户端
通过表单发送用户名/密码 FormClient (opens new window)(间接客户端)
DirectFormClient (opens new window)(直接客户端)
通过基本认证发送用户名/密码 IndirectBasicAuthClient (opens new window)(间接客户端)
DirectBasicAuthClient (opens new window)(直接客户端)
作为 cookie 值发送 CookieClient (opens new window)(直接客户端)
作为 HTTP 头的值发送 HeaderClient (opens new window)(直接客户端)
作为 Authorization 头的值发送,值以 “Bearer ” 开头 DirectBearerAuthClient (opens new window)(直接客户端)
作为 HTTP 参数值发送 ParameterClient (opens new window)(直接客户端)
IP 地址 IpClient (opens new window)(直接客户端)
X509 证书 X509Client (opens new window)(直接客户端)
通过摘要认证发送的用户名/令牌 DirectDigestAuthClient (opens new window)(直接客户端)

示例

// REST authentication with JWT token passed in the url as the "token" parameter
ParameterClient parameterClient = new ParameterClient("token", new JwtAuthenticator(salt));
parameterClient.setSupportGetRequest(true);
parameterClient.setSupportPostRequest(false);

// if the 'Authorization' header is passed with the 'Basic token' value
HeaderClient client = new HeaderClient("Authorization", "Basic ", (credentials, ctx) -> {
    String token = ((TokenCredentials) credentials).getToken();
    // check the token and create a profile
    if ("goodToken".equals(token)) {
        CommonProfile profile = new CommonProfile();
        profile.setId("myId");
        // save in the credentials to be passed to the default AuthenticatorProfileCreator
        credentials.setUserProfile(profile);
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

原文链接 (opens new window)