Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
My_csdo
JustAuth
提交
7ed24868
J
JustAuth
项目概览
My_csdo
/
JustAuth
与 Fork 源项目一致
Fork自
justauth / JustAuth
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JustAuth
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
未验证
提交
7ed24868
编写于
5月 21, 2019
作者:
智布道
👁
提交者:
GitHub
5月 21, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5 from xkcoding/feature-google
✨
去除重复代码、新增支持Google登录
上级
2abca4f1
d24733e0
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
176 addition
and
3 deletion
+176
-3
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/GoogleAuthorization.java
...java/me/zhyd/oauth/authorization/GoogleAuthorization.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
+3
-0
src/main/java/me/zhyd/oauth/model/AuthToken.java
src/main/java/me/zhyd/oauth/model/AuthToken.java
+8
-0
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
+63
-0
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
+2
-3
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+38
-0
src/test/java/me/zhyd/oauth/AuthRequestTest.java
src/test/java/me/zhyd/oauth/AuthRequestTest.java
+13
-0
未找到文件。
src/main/java/me/zhyd/oauth/authorization/AuthorizationFactory.java
浏览文件 @
7ed24868
...
...
@@ -59,6 +59,7 @@ public class AuthorizationFactory {
AuthorizationFactory
.
register
(
AuthSource
.
DINGTALK
,
new
DingTalkAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
GITEE
,
new
GiteeAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
GITHUB
,
new
GithubAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
GOOGLE
,
new
GoogleAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
OSCHINA
,
new
OschinaAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
QQ
,
new
QqAuthorization
());
AuthorizationFactory
.
register
(
AuthSource
.
TAOBAO
,
new
TaobaoAuthorization
());
...
...
src/main/java/me/zhyd/oauth/authorization/GoogleAuthorization.java
0 → 100644
浏览文件 @
7ed24868
package
me.zhyd.oauth.authorization
;
import
me.zhyd.oauth.config.AuthConfig
;
import
me.zhyd.oauth.utils.UrlBuilder
;
/**
* Google授权
*
* @author yangkai.shen (https://xkcoding.com)
* @version 1.3
* @since 1.3
*/
public
class
GoogleAuthorization
implements
Authorization
{
@Override
public
String
getAuthorizeUrl
(
AuthConfig
config
)
{
return
UrlBuilder
.
getGoogleAuthorizeUrl
(
config
.
getClientId
(),
config
.
getRedirectUri
());
}
}
src/main/java/me/zhyd/oauth/consts/ApiUrl.java
浏览文件 @
7ed24868
...
...
@@ -383,6 +383,35 @@ public enum ApiUrl {
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
@Override
public
String
refresh
()
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
},
/**
* Google
*/
GOOGLE
{
@Override
public
String
authorize
()
{
return
"https://accounts.google.com/o/oauth2/v2/auth"
;
}
@Override
public
String
accessToken
()
{
return
"https://www.googleapis.com/oauth2/v4/token"
;
}
@Override
public
String
userInfo
()
{
return
"https://oauth2.googleapis.com/tokeninfo"
;
}
@Override
public
String
revoke
()
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
@Override
public
String
refresh
()
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
...
...
src/main/java/me/zhyd/oauth/model/AuthSource.java
浏览文件 @
7ed24868
...
...
@@ -21,5 +21,8 @@ public enum AuthSource {
TAOBAO
,
QQ
,
WECHAT
,
/**
* 谷歌登录,参考文档:https://developers.google.com/identity/protocols/OpenIDConnect
*/
GOOGLE
,
}
src/main/java/me/zhyd/oauth/model/AuthToken.java
浏览文件 @
7ed24868
...
...
@@ -17,4 +17,12 @@ public class AuthToken {
private
String
uid
;
private
String
openId
;
private
String
accessCode
;
/**
* Google附带属性
*/
private
String
scope
;
private
String
tokenType
;
private
String
idToken
;
}
src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
0 → 100644
浏览文件 @
7ed24868
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.utils.UrlBuilder
;
/**
* Google登录
*
* @author yangkai.shen (https://xkcoding.com)
* @version 1.3
* @since 1.3
*/
public
class
AuthGoogleRequest
extends
BaseAuthRequest
{
public
AuthGoogleRequest
(
AuthConfig
config
)
{
super
(
config
,
AuthSource
.
GOOGLE
);
}
@Override
protected
AuthToken
getAccessToken
(
String
code
)
{
String
accessTokenUrl
=
UrlBuilder
.
getGoogleAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
code
,
config
.
getRedirectUri
());
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
if
(
object
.
containsKey
(
"error"
)
||
object
.
containsKey
(
"error_description"
))
{
throw
new
AuthException
(
"get google access_token has error:["
+
object
.
getString
(
"error"
)
+
"], error_description:["
+
object
.
getString
(
"error_description"
)
+
"]"
);
}
return
AuthToken
.
builder
()
.
accessToken
(
object
.
getString
(
"access_token"
))
.
expireIn
(
object
.
getIntValue
(
"expires_in"
))
.
scope
(
object
.
getString
(
"scope"
))
.
tokenType
(
object
.
getString
(
"token_type"
))
.
idToken
(
object
.
getString
(
"id_token"
))
.
build
();
}
@Override
protected
AuthUser
getUserInfo
(
AuthToken
authToken
)
{
String
accessToken
=
authToken
.
getIdToken
();
HttpResponse
response
=
HttpRequest
.
get
(
UrlBuilder
.
getGoogleUserInfoUrl
(
accessToken
)).
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
return
AuthUser
.
builder
()
.
username
(
object
.
getString
(
"name"
))
.
avatar
(
object
.
getString
(
"picture"
))
.
nickname
(
object
.
getString
(
"name"
))
.
location
(
object
.
getString
(
"locale"
))
.
email
(
object
.
getString
(
"email"
))
.
token
(
authToken
)
.
source
(
AuthSource
.
GOOGLE
)
.
build
();
}
}
src/main/java/me/zhyd/oauth/request/AuthWeChatRequest.java
浏览文件 @
7ed24868
...
...
@@ -30,12 +30,11 @@ public class AuthWeChatRequest extends BaseAuthRequest {
protected
AuthToken
getAccessToken
(
String
code
)
{
String
accessTokenUrl
=
UrlBuilder
.
getWeChatAccessTokenUrl
(
config
.
getClientId
(),
config
.
getClientSecret
(),
code
);
HttpResponse
response
=
HttpRequest
.
get
(
accessTokenUrl
).
execute
();
JSONObject
accessTokenO
bject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(!
accessTokenObject
.
containsKey
(
"access_token"
)
||
!
accessTokenObject
.
containsKey
(
"openid"
)
||
!
accessTokenO
bject
JSONObject
o
bject
=
JSONObject
.
parseObject
(
response
.
body
());
if
(!
object
.
containsKey
(
"access_token"
)
||
!
object
.
containsKey
(
"openid"
)
||
!
o
bject
.
containsKey
(
"refresh_token"
))
{
throw
new
AuthException
(
"Unable to get access_token or openid or refresh_token from wechat using code ["
+
code
+
"]"
);
}
JSONObject
object
=
JSONObject
.
parseObject
(
response
.
body
());
return
AuthToken
.
builder
()
.
accessToken
(
object
.
getString
(
"access_token"
))
.
refreshToken
(
object
.
getString
(
"refresh_token"
))
...
...
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
浏览文件 @
7ed24868
...
...
@@ -17,6 +17,10 @@ public class UrlBuilder {
private
static
final
String
GITHUB_USER_INFO_PATTERN
=
"{0}?access_token={1}"
;
private
static
final
String
GITHUB_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&state=1&redirect_uri={2}"
;
private
static
final
String
GOOGLE_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&scope=openid%20email%20profile&redirect_uri={2}&state={3}"
;
private
static
final
String
GOOGLE_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}&grant_type=authorization_code"
;
private
static
final
String
GOOGLE_USER_INFO_PATTERN
=
"{0}?id_token={1}"
;
private
static
final
String
WEIBO_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"
;
private
static
final
String
WEIBO_USER_INFO_PATTERN
=
"{0}?{1}"
;
private
static
final
String
WEIBO_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}"
;
...
...
@@ -492,4 +496,38 @@ public class UrlBuilder {
public
static
String
getTaobaoAuthorizeUrl
(
String
clientId
,
String
redirectUrl
)
{
return
MessageFormat
.
format
(
TAOBAO_AUTHORIZE_PATTERN
,
ApiUrl
.
TAOBAO
.
authorize
(),
clientId
,
redirectUrl
);
}
/**
* 获取Google授权地址
*
* @param clientId google 应用的Client ID
* @param redirectUrl google 应用授权成功后的回调地址
* @return full url
*/
public
static
String
getGoogleAuthorizeUrl
(
String
clientId
,
String
redirectUrl
)
{
return
MessageFormat
.
format
(
GOOGLE_AUTHORIZE_PATTERN
,
ApiUrl
.
GOOGLE
.
authorize
(),
clientId
,
redirectUrl
,
System
.
currentTimeMillis
());
}
/**
* 获取Google token的接口地址
*
* @param clientId google应用的Client ID
* @param clientSecret google应用的Client Secret
* @param code google授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return full url
*/
public
static
String
getGoogleAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
GOOGLE_ACCESS_TOKEN_PATTERN
,
ApiUrl
.
GOOGLE
.
accessToken
(),
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取Google用户详情的接口地址
*
* @param token google 应用的token
* @return full url
*/
public
static
String
getGoogleUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
GOOGLE_USER_INFO_PATTERN
,
ApiUrl
.
GOOGLE
.
userInfo
(),
token
);
}
}
src/test/java/me/zhyd/oauth/AuthRequestTest.java
浏览文件 @
7ed24868
...
...
@@ -141,4 +141,17 @@ public class AuthRequestTest {
// 授权登录后会返回一个code,用这个code进行登录
AuthResponse
login
=
authRequest
.
login
(
"code"
);
}
@Test
public
void
googleTest
()
{
AuthRequest
authRequest
=
new
AuthGoogleRequest
(
AuthConfig
.
builder
()
.
clientId
(
"clientId"
)
.
clientSecret
(
"clientSecret"
)
.
redirectUri
(
"redirectUri"
)
.
build
());
// 返回授权页面,可自行调整
String
url
=
authRequest
.
authorize
();
// 授权登录后会返回一个code,用这个code进行登录
AuthResponse
login
=
authRequest
.
login
(
"code"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录