Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
符节科技
Jap
提交
a739ca37
Jap
项目概览
符节科技
/
Jap
12 个月 前同步成功
通知
91
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Jap
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a739ca37
编写于
5月 21, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
增加facebook授权登录
上级
880a7d41
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
165 addition
and
0 deletion
+165
-0
README.md
README.md
+5
-0
src/main/java/me/zhyd/oauth/authorization/AuthorizationFactory.java
...ava/me/zhyd/oauth/authorization/AuthorizationFactory.java
+1
-0
src/main/java/me/zhyd/oauth/authorization/FacebookAuthorization.java
...va/me/zhyd/oauth/authorization/FacebookAuthorization.java
+19
-0
src/main/java/me/zhyd/oauth/consts/ApiUrl.java
src/main/java/me/zhyd/oauth/consts/ApiUrl.java
+29
-0
src/main/java/me/zhyd/oauth/model/AuthSource.java
src/main/java/me/zhyd/oauth/model/AuthSource.java
+1
-0
src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
+72
-0
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+38
-0
未找到文件。
README.md
浏览文件 @
a739ca37
...
@@ -33,6 +33,7 @@
...
@@ -33,6 +33,7 @@
<td
align=
"center"
width=
"200"
><a
href=
"#授权微信"
><img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png"
width=
"20"
></a></td>
<td
align=
"center"
width=
"200"
><a
href=
"#授权微信"
><img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png"
width=
"20"
></a></td>
<td
align=
"center"
width=
"200"
><a
href=
"#授权淘宝"
><img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/taobao.png"
width=
"20"
></a></td>
<td
align=
"center"
width=
"200"
><a
href=
"#授权淘宝"
><img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/taobao.png"
width=
"20"
></a></td>
<td
align=
"center"
width=
"200"
><a
href=
"#授权Google"
><img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/google.png"
width=
"20"
></a></td>
<td
align=
"center"
width=
"200"
><a
href=
"#授权Google"
><img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/google.png"
width=
"20"
></a></td>
<td
align=
"center"
width=
"200"
><a
href=
"#授权Facebook"
><img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/facebook.png"
width=
"20"
></a></td>
</tr>
</tr>
</table>
</table>
</center>
</center>
...
@@ -90,6 +91,7 @@ authRequest.login("code");
...
@@ -90,6 +91,7 @@ authRequest.login("code");
|
<img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png"
width=
"20"
>
|
[
AuthWeChatRequest
](
https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
)
|
<a
href=
"https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN"
target=
"_blank"
>
参考文档
</a>
|
|
<img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/wechats.png"
width=
"20"
>
|
[
AuthWeChatRequest
](
https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
)
|
<a
href=
"https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN"
target=
"_blank"
>
参考文档
</a>
|
|
<img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/taobao.png"
width=
"20"
>
|
[
AuthTaobaoRequest
](
https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java
)
|
<a
href=
"https://open.taobao.com/doc.htm?spm=a219a.7386797.0.0.4e00669acnkQy6&source=search&docId=105590&docType=1"
target=
"_blank"
>
参考文档
</a>
|
|
<img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/taobao.png"
width=
"20"
>
|
[
AuthTaobaoRequest
](
https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthTaobaoRequest.java
)
|
<a
href=
"https://open.taobao.com/doc.htm?spm=a219a.7386797.0.0.4e00669acnkQy6&source=search&docId=105590&docType=1"
target=
"_blank"
>
参考文档
</a>
|
|
<img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/google.png"
width=
"20"
>
|
[
AuthGoogleRequest
](
https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
)
|
<a
href=
"https://developers.google.com/identity/protocols/OpenIDConnect"
target=
"_blank"
>
参考文档
</a>
|
|
<img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/google.png"
width=
"20"
>
|
[
AuthGoogleRequest
](
https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
)
|
<a
href=
"https://developers.google.com/identity/protocols/OpenIDConnect"
target=
"_blank"
>
参考文档
</a>
|
|
<img
src=
"https://gitee.com/yadong.zhang/static/raw/master/JustAuth/facebook.png"
width=
"20"
>
|
[
AuthFacebookRequest
](
https://gitee.com/yadong.zhang/JustAuth/blob/master/src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
)
|
<a
href=
"https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow"
target=
"_blank"
>
参考文档
</a>
|
## 后续开发计划
## 后续开发计划
...
@@ -162,6 +164,9 @@ authRequest.login("code");
...
@@ -162,6 +164,9 @@ authRequest.login("code");
#### 授权Google
#### 授权Google
![
授权Google登录
](
https://images.gitee.com/uploads/images/2019/0521/190650_85c5f1c7_784199.png
"授权Google登录"
)
![
授权Google登录
](
https://images.gitee.com/uploads/images/2019/0521/190650_85c5f1c7_784199.png
"授权Google登录"
)
#### 授权Facebook
![
授权Facebook登录
](
https://images.gitee.com/uploads/images/2019/0521/190650_85c5f1c7_784199.png
"授权Facebook登录"
)
# 交流
# 交流
...
...
src/main/java/me/zhyd/oauth/authorization/AuthorizationFactory.java
浏览文件 @
a739ca37
...
@@ -66,6 +66,7 @@ public class AuthorizationFactory {
...
@@ -66,6 +66,7 @@ public class AuthorizationFactory {
AuthorizationFactory
.
register
(
AuthSource
.
TENCEN_CLOUD
,
new
TencentCloudAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
TENCEN_CLOUD
,
new
TencentCloudAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
WECHAT
,
new
WeChatAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
WECHAT
,
new
WeChatAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
WEIBO
,
new
WeiboAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
WEIBO
,
new
WeiboAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
FACEBOOK
,
new
FacebookAuthorization
());
loader
=
true
;
loader
=
true
;
}
}
...
...
src/main/java/me/zhyd/oauth/authorization/FacebookAuthorization.java
0 → 100644
浏览文件 @
a739ca37
package
me.zhyd.oauth.authorization
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
* Facebook授权
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.3
* @since 1.3
*/
public
class
FacebookAuthorization
implements
Authorization
{
@Override
public
String
getAuthorizeUrl
(
AuthConfig
config
)
{
return
UrlBuilder
.
getFacebookAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
());
}
}
src/main/java/me/zhyd/oauth/consts/ApiUrl.java
浏览文件 @
a739ca37
...
@@ -412,6 +412,35 @@ public enum ApiUrl {
...
@@ -412,6 +412,35 @@ public enum ApiUrl {
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
}
@Override
public
String
refresh
()
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
},
/**
* Facebook
*/
FACEBOOK
{
@Override
public
String
authorize
()
{
return
"https://www.facebook.com/v3.3/dialog/oauth"
;
}
@Override
public
String
accessToken
()
{
return
"https://graph.facebook.com/v3.3/oauth/access_token"
;
}
@Override
public
String
userInfo
()
{
return
"https://graph.facebook.com/v3.3/me"
;
}
@Override
public
String
revoke
()
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
@Override
@Override
public
String
refresh
()
{
public
String
refresh
()
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
...
...
src/main/java/me/zhyd/oauth/model/AuthSource.java
浏览文件 @
a739ca37
...
@@ -25,4 +25,5 @@ public enum AuthSource {
...
@@ -25,4 +25,5 @@ public enum AuthSource {
* 谷歌登录,参考文档:https://developers.google.com/identity/protocols/OpenIDConnect
* 谷歌登录,参考文档:https://developers.google.com/identity/protocols/OpenIDConnect
*/
*/
GOOGLE
,
GOOGLE
,
FACEBOOK
,
}
}
src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
0 → 100644
浏览文件 @
a739ca37
package
me.zhyd.oauth.request
;
import
cn.hutool.http.HttpRequest
;
import
cn.hutool.http.HttpResponse
;
import
com.alibaba.fastjson.JSONObject
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.exception.AuthException
;
import
me.zhyd.oauth.model.AuthSource
;
import
me.zhyd.oauth.model.AuthToken
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.model.AuthUserGender
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
* Facebook登录
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @since 1.8
*/
public
class
AuthFacebookRequest
extends
BaseAuthRequest
{
public
AuthFacebookRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
FACEBOOK
);
}
@Override
protected
AuthToken
getAccessToken
(
String
code
)
{
String
accessTokenUrl
=
UrlBuilder
.
getFacebookAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
code
,
config
.
getRedirectUri
());
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getJSONObject
(
"error"
).
getString
(
"message"
));
}
return
AuthToken
.
builder
()
.
accessToken
(
object
.
getString
(
"access_token"
))
.
expireIn
(
object
.
getIntValue
(
"expires_in"
))
.
tokenType
(
object
.
getString
(
"token_type"
))
.
build
();
}
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
String
accessToken
=
authToken
.
getAccessToken
();
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getFacebookUserInfoUrl
(
accessToken
)).
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
if
(
object
.
containsKey
(
"error"
))
{
throw
new
AuthException
(
object
.
getJSONObject
(
"error"
).
getString
(
"message"
));
}
String
picture
=
null
;
if
(
object
.
containsKey
(
"picture"
))
{
JSONObject
pictureObj
=
object
.
getJSONObject
(
"picture"
);
pictureObj
=
pictureObj
.
getJSONObject
(
"data"
);
if
(
null
!=
pictureObj
)
{
picture
=
pictureObj
.
getString
(
"url"
);
}
}
return
AuthUser
.
builder
()
.
username
(
object
.
getString
(
"name"
))
.
nickname
(
object
.
getString
(
"name"
))
.
avatar
(
picture
)
.
location
(
object
.
getString
(
"locale"
))
.
email
(
object
.
getString
(
"email"
))
.
gender
(
AuthUserGender
.
getRealGender
(
object
.
getString
(
"gender"
)))
.
token
(
authToken
)
.
source
(
AuthSource
.
FACEBOOK
)
.
build
();
}
}
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
浏览文件 @
a739ca37
...
@@ -68,6 +68,10 @@ public class UrlBuilder {
...
@@ -68,6 +68,10 @@ public class UrlBuilder {
private
static
final
String
TAOBAO_AUTHORIZE_PATTERN
=
"{0}?response_type=code&client_id={1}&redirect_uri={2}&state=&view=web"
;
private
static
final
String
TAOBAO_AUTHORIZE_PATTERN
=
"{0}?response_type=code&client_id={1}&redirect_uri={2}&state=&view=web"
;
private
static
final
String
TAOBAO_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
private
static
final
String
TAOBAO_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
private
static
final
String
FACEBOOK_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&redirect_uri={2}&state={3}&response_type=code&scope="
;
private
static
final
String
FACEBOOK_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
private
static
final
String
FACEBOOK_USER_INFO_PATTERN
=
"{0}?access_token={1}&fields=id,name,birthday,gender,hometown,email,devices,picture.width(400)"
;
/**
/**
* 获取githubtoken的接口地址
* 获取githubtoken的接口地址
*
*
...
@@ -530,4 +534,38 @@ public class UrlBuilder {
...
@@ -530,4 +534,38 @@ public class UrlBuilder {
public
static
String
getGoogleUserInfoUrl
(
String
token
)
{
public
static
String
getGoogleUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
GOOGLE_USER_INFO_PATTERN
,
ApiUrl
.
GOOGLE
.
userInfo
(),
token
);
return
MessageFormat
.
format
(
GOOGLE_USER_INFO_PATTERN
,
ApiUrl
.
GOOGLE
.
userInfo
(),
token
);
}
}
/**
* 获取Facebook授权地址
*
* @param clientId Facebook 应用的Client ID
* @param redirectUrl Facebook 应用授权成功后的回调地址
* @return full url
*/
public
static
String
getFacebookAuthorizeUrl
(
String
clientId
,
String
redirectUrl
)
{
return
MessageFormat
.
format
(
FACEBOOK_AUTHORIZE_PATTERN
,
ApiUrl
.
FACEBOOK
.
authorize
(),
clientId
,
redirectUrl
,
System
.
currentTimeMillis
());
}
/**
* 获取Facebook token的接口地址
*
* @param clientId Facebook 应用的Client ID
* @param clientSecret Facebook 应用的Client Secret
* @param code Facebook 授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public
static
String
getFacebookAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
FACEBOOK_ACCESS_TOKEN_PATTERN
,
ApiUrl
.
FACEBOOK
.
accessToken
(),
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取Facebook用户详情的接口地址
*
* @param token Facebook 应用的token
* @return full url
*/
public
static
String
getFacebookUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
FACEBOOK_USER_INFO_PATTERN
,
ApiUrl
.
FACEBOOK
.
userInfo
(),
token
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录