# OAuth

pac4j 允许你使用 OAuth v1.0 和 v2.0 协议认证提供者(identity provider)。

# 1)依赖

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

示例(Maven dependency)

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

# 2)可用的客户端

# a)通用客户端

你可以使用 OAuth10Client (opens new window)OAuth20Client (opens new window) 客户端登录 OAuth1.0 或 2.0 服务器。

模拟 BitbucketClient (OAuth v1.0)的示例

OAuth10Configuration config = new OAuth10Configuration();
config.setKey("bjEt8BMpLwFDqZUvp6");
config.setSecret("NN6fVXRTcV2qYVejVLZqxBRqHgn3ygD4");
config.setApi(new BitBucketApi());
config.setProfileDefinition(new BitbucketProfileDefinition());
OAuth10Client client = new OAuth10Client();
client.setCallbackUrl(PAC4J_BASE_URL);
client.setConfiguration(config);
1
2
3
4
5
6
7
8

模拟 GithubClient (OAuth v2.0)的示例

OAuth20Configuration config = new OAuth20Configuration();
config.setApi(GitHubApi.instance());
config.setProfileDefinition(new GitHubProfileDefinition());
config.setScope("user");
config.setKey("62374f5573a89a8f9900");
config.setSecret("01dd26d60447677ceb7399fb4c744f545bb86359");
OAuth20Client client = new OAuth20Client();
client.setConfiguration(config);
client.setCallbackUrl(PAC4J_BASE_URL);
1
2
3
4
5
6
7
8
9

对于 OAuth v2.0,你也可以使用 GenericApi20 (opens new window) 或直接使用 GenericOAuth20Client

示例

GenericOAuth20Client client = new GenericOAuth20Client();
Map map = new HashMap();
map.put(AGE, "Integer|age");
map.put(IS_ADMIN, "Boolean|is_admin");
map.put(BG_COLOR, "Color|bg_color");
map.put(GENDER, "Gender|gender");
map.put(BIRTHDAY, "Locale|birthday");
map.put(ID, "Long|id");
map.put(BLOG, "URI|blog");
map.put("name", "name");  //default String
client.setProfileAttrs(map);
1
2
3
4
5
6
7
8
9
10
11

你需要定义要从用户配置文件获取的所有属性。你可以只定义属性名称(name)或属性名称以及关联的转换器(Boolean|is_admin)。 目前,支持以下转换器:IntegerBooleanColorGenderLocaleLongURIString(默认)。

# b)特定客户端

默认情况下,已有许多客户端可以用于很多认证提供者登录:

认证提供者 客户端 用户配置文件
BitBucket (opens new window) BitbucketClient (opens new window) BitbucketProfile (opens new window)
a CAS server using the OAuth protocol (opens new window) CasOAuthWrapperClient (opens new window) CasOAuthWrapperProfile (opens new window)
DropBox (opens new window) DropBoxClient (opens new window) DropBoxProfile (opens new window)
Facebook (opens new window) FacebookClient (opens new window) FacebookProfile (opens new window)
Foursquare (opens new window) FoursquareClient (opens new window) FoursquareProfile (opens new window)
Github (opens new window) GitHubClient (opens new window) GitHubProfile (opens new window)
Google (opens new window) Google2Client (opens new window) Google2Profile (opens new window)
HiOrg-Server (opens new window) HiOrgServerClient (opens new window) HiOrgServerProfile (opens new window)
LinkedIn (opens new window) LinkedIn2Client (opens new window) LinkedIn2Profile (opens new window)
Odnoklassniki (opens new window) OkClient (opens new window) OkProfile (opens new window)
Paypal (opens new window) PayPalClient (opens new window) PayPalProfile (opens new window)
QQ (opens new window) QQClient (opens new window) QQProfile (opens new window)
Strava (opens new window) StravaClient (opens new window) StravaProfile (opens new window)
Twitter (opens new window) TwitterClient (opens new window) TwitterProfile (opens new window)
Vk (opens new window) VkClient (opens new window) VkProfile (opens new window)
Wechat (opens new window) WechatClient (opens new window) WechatProfile (opens new window)
Weibo (opens new window) WeiboClient (opens new window) WeiboProfile (opens new window)
Windows Live (opens new window) WindowsLiveClient (opens new window) WindowsLiveProfile (opens new window)
Word Press (opens new window) WordPressClient (opens new window) WordPressProfile (opens new window)
Yahoo (opens new window) YahooClient (opens new window) YahooProfile (opens new window)
Cronofy (opens new window) CronofyClient (opens new window) CronofyProfile (opens new window)

示例

FacebookClient facebookClient = new FacebookClient("145278422258960", "be21409ba8f39b5dae2a7de525484da8");
TwitterClient twitterClient = new TwitterClient("CoxUiYwQOSFDReZYdjigBA", "2kAzunH5Btc4gRSaMr7D7MkyoJ5u1VzbOOzE8rBofs");
1
2

原文链接 (opens new window)