# REST API
pac4j 允许你通过 REST API 验证用户。
# 1)依赖
你需要使用以下模块:pac4j-http。
示例(Maven依赖项):
<dependency>
    <groupId>org.pac4j</groupId>
    <artifactId>pac4j-http</artifactId>
    <version>${pac4j.version}</version>
</dependency>
 1
2
3
4
5
2
3
4
5
# 2)RestAuthenticator
 RestAuthenticator (opens new window) 通过将提供的用户名/密码作为基本认证传送到 URL 来验证,该 URL 必须返回:
- 如果用户名/密码凭据有效,则将用户配置文件作为 JSON 的 
200HTTP 响应 - 如果用户名/密码凭证无效,则使用任何其他 HTTP 状态代码(最好是 
401)。 
如果认证成功,则返回 RestProfile (opens new window)。
正确的服务器响应示例:
{
  "id": "1234",
  "attributes": {
    "firstName": "Jerome"
  }
}
 1
2
3
4
5
6
2
3
4
5
6
客户端示例:
RestAuthenticator authenticator = new RestAuthenticator("http://rest-api-url");
DirectBasicAuthClient directBasicAuthClient = new DirectBasicAuthClient(authenticator);
 1
2
2