Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
justauth
JustAuth
提交
ebc19eea
J
JustAuth
项目概览
justauth
/
JustAuth
接近 1 年 前同步成功
通知
361
Star
15209
Fork
2709
代码
文件
提交
分支
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ebc19eea
编写于
1月 31, 2019
作者:
智布道
👁
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
🎉
初始化提交
上级
95bf3633
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
829 addition
and
0 deletion
+829
-0
src/main/java/me/zhyd/oauth/config/AuthConfig.java
src/main/java/me/zhyd/oauth/config/AuthConfig.java
+47
-0
src/main/java/me/zhyd/oauth/consts/ApiUrlConst.java
src/main/java/me/zhyd/oauth/consts/ApiUrlConst.java
+59
-0
src/main/java/me/zhyd/oauth/exception/AuthException.java
src/main/java/me/zhyd/oauth/exception/AuthException.java
+36
-0
src/main/java/me/zhyd/oauth/model/AuthResponse.java
src/main/java/me/zhyd/oauth/model/AuthResponse.java
+19
-0
src/main/java/me/zhyd/oauth/model/AuthSource.java
src/main/java/me/zhyd/oauth/model/AuthSource.java
+16
-0
src/main/java/me/zhyd/oauth/model/AuthToken.java
src/main/java/me/zhyd/oauth/model/AuthToken.java
+20
-0
src/main/java/me/zhyd/oauth/model/AuthUser.java
src/main/java/me/zhyd/oauth/model/AuthUser.java
+26
-0
src/main/java/me/zhyd/oauth/model/AuthUserGender.java
src/main/java/me/zhyd/oauth/model/AuthUserGender.java
+40
-0
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
+83
-0
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
+76
-0
src/main/java/me/zhyd/oauth/request/AuthRequest.java
src/main/java/me/zhyd/oauth/request/AuthRequest.java
+42
-0
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
+90
-0
src/main/java/me/zhyd/oauth/request/ResponseStatus.java
src/main/java/me/zhyd/oauth/request/ResponseStatus.java
+32
-0
src/main/java/me/zhyd/oauth/utils/AuthConfigChecker.java
src/main/java/me/zhyd/oauth/utils/AuthConfigChecker.java
+60
-0
src/main/java/me/zhyd/oauth/utils/IpUtils.java
src/main/java/me/zhyd/oauth/utils/IpUtils.java
+32
-0
src/main/java/me/zhyd/oauth/utils/StringUtils.java
src/main/java/me/zhyd/oauth/utils/StringUtils.java
+19
-0
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
+132
-0
未找到文件。
src/main/java/me/zhyd/oauth/config/AuthConfig.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.config
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 14:03
* @since 1.8
*/
public
class
AuthConfig
{
/**
* github应用的Client ID
*/
public
static
String
githubClientId
=
null
;
/**
* github应用的Client Secret
*/
public
static
String
githubClientSecret
=
null
;
/**
* github应用的redirect_uri 登陆成功后的回调地址
*/
public
static
String
githubRedirectUri
=
null
;
/**
* weibo应用的App Key
*/
public
static
String
weiboClientId
=
null
;
/**
* weibo应用的App Secret
*/
public
static
String
weiboClientSecret
=
null
;
/**
* weibo应用的redirect_uri 登陆成功后的回调地址
*/
public
static
String
weiboRedirectUri
=
null
;
/**
* gitee应用的Client ID
*/
public
static
String
giteeClientId
=
null
;
/**
* gitee应用的Client Secret
*/
public
static
String
giteeClientSecret
=
null
;
/**
* gitee应用的redirect_uri 登陆成功后的回调地址
*/
public
static
String
giteeRedirectUri
=
null
;
}
src/main/java/me/zhyd/oauth/consts/ApiUrlConst.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.consts
;
/**
* 各api需要的url常量类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 12:26
* @since 1.0
*/
public
class
ApiUrlConst
{
/**
* 获取github access_token的地址
*/
public
static
final
String
GITHUB_ACCESS_TOKEN_URL
=
"https://github.com/login/oauth/access_token"
;
/**
* 获取github用户信息的地址
*/
public
static
final
String
GITHUB_USER_INFO_URL
=
"https://api.github.com/user"
;
/**
* 获取github授权地址
*/
public
static
final
String
GITHUB_AUTHORIZE_URL
=
"https://github.com/login/oauth/authorize"
;
/**
* 获取weibo access_token的地址
*/
public
static
final
String
WEIBO_ACCESS_TOKEN_URL
=
"https://api.weibo.com/oauth2/access_token"
;
/**
* 获取weibo用户信息的地址
*/
public
static
final
String
WEIBO_USER_INFO_URL
=
"https://api.weibo.com/2/users/show.json"
;
/**
* 获取weibo授权地址
*/
public
static
final
String
WEIBO_AUTHORIZE_URL
=
"https://api.weibo.com/oauth2/authorize"
;
/**
* 获取gitee access_token的地址
*/
public
static
final
String
GITEE_ACCESS_TOKEN_URL
=
"https://gitee.com/oauth/token"
;
/**
* 获取gitee用户信息的地址
*/
public
static
final
String
GITEE_USER_INFO_URL
=
"https://gitee.com/api/v5/user"
;
/**
* 获取gitee授权地址
*/
public
static
final
String
GITEE_AUTHORIZE_URL
=
"https://gitee.com/oauth/authorize"
;
}
src/main/java/me/zhyd/oauth/exception/AuthException.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.exception
;
import
me.zhyd.oauth.request.ResponseStatus
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 16:05
* @since 1.8
*/
public
class
AuthException
extends
RuntimeException
{
public
AuthException
()
{
super
();
}
public
AuthException
(
String
message
)
{
super
(
message
);
}
public
AuthException
(
ResponseStatus
status
)
{
super
(
status
.
getMsg
());
}
public
AuthException
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
public
AuthException
(
Throwable
cause
)
{
super
(
cause
);
}
protected
AuthException
(
String
message
,
Throwable
cause
,
boolean
enableSuppression
,
boolean
writableStackTrace
)
{
super
(
message
,
cause
,
enableSuppression
,
writableStackTrace
);
}
}
src/main/java/me/zhyd/oauth/model/AuthResponse.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.model
;
import
lombok.Builder
;
import
lombok.Data
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 15:43
* @since 1.8
*/
@Builder
@Data
public
class
AuthResponse
<
T
>
{
private
int
code
=
200
;
private
String
msg
;
private
T
data
;
}
src/main/java/me/zhyd/oauth/model/AuthSource.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.model
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 13:52
* @since 1.8
*/
public
enum
AuthSource
{
GITHUB
,
GITEE
,
WEIBO
,
QQ
,
WEIXIN
}
src/main/java/me/zhyd/oauth/model/AuthToken.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.model
;
import
lombok.Builder
;
import
lombok.Data
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 15:43
* @since 1.8
*/
@Data
@Builder
public
class
AuthToken
{
private
String
accessToken
;
private
String
expireIn
;
private
String
refreshToken
;
private
String
uid
;
}
src/main/java/me/zhyd/oauth/model/AuthUser.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.model
;
import
lombok.Builder
;
import
lombok.Data
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 13:47
* @since 1.8
*/
@Builder
@Data
public
class
AuthUser
{
private
String
username
;
private
String
avatar
;
private
String
blog
;
private
String
nickname
;
private
String
company
;
private
String
location
;
private
String
email
;
private
String
remark
;
private
AuthUserGender
gender
;
private
AuthSource
source
;
}
src/main/java/me/zhyd/oauth/model/AuthUserGender.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.model
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 17:24
* @since 1.8
*/
public
enum
AuthUserGender
{
MALE
(
1
,
"男"
),
FEMALE
(
0
,
"女"
),
UNKNOW
(-
1
,
""
);
private
int
code
;
private
String
desc
;
AuthUserGender
(
int
code
,
String
desc
)
{
this
.
code
=
code
;
this
.
desc
=
desc
;
}
public
static
AuthUserGender
getRealGender
(
String
code
)
{
if
(
code
==
null
)
{
return
UNKNOW
;
}
if
(
"m"
.
equals
(
code
)
||
"男"
.
equals
(
code
)
||
"1"
.
equals
(
code
)
||
"male"
.
equalsIgnoreCase
(
code
))
{
return
MALE
;
}
if
(
"f"
.
equals
(
code
)
||
"女"
.
equals
(
code
)
||
"0"
.
equals
(
code
)
||
"female"
.
equalsIgnoreCase
(
code
))
{
return
FEMALE
;
}
return
UNKNOW
;
}
public
int
getCode
()
{
return
code
;
}
public
String
getDesc
()
{
return
desc
;
}
}
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
0 → 100644
浏览文件 @
ebc19eea
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.AuthResponse
;
import
me.zhyd.oauth.model.AuthSource
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.utils.AuthConfigChecker
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 16:31
* @since 1.8
*/
public
class
AuthGiteeRequest
implements
AuthRequest
{
@Override
public
void
authorize
(
HttpServletResponse
response
)
{
if
(!
AuthConfigChecker
.
isSupportedGitee
())
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
String
authorizeUrl
=
UrlBuilder
.
getGiteeAuthorizeUrl
(
AuthConfig
.
giteeClientId
,
AuthConfig
.
giteeRedirectUri
);
try
{
response
.
sendRedirect
(
authorizeUrl
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
String
authorize
()
{
if
(!
AuthConfigChecker
.
isSupportedGitee
())
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
return
UrlBuilder
.
getGiteeAuthorizeUrl
(
AuthConfig
.
giteeClientId
,
AuthConfig
.
giteeRedirectUri
);
}
@Override
public
AuthResponse
login
(
String
code
)
{
if
(!
AuthConfigChecker
.
isSupportedGitee
())
{
return
AuthResponse
.
builder
()
.
code
(
ResponseStatus
.
UNSUPPORTED
.
getCode
())
.
msg
(
ResponseStatus
.
UNSUPPORTED
.
getMsg
())
.
build
();
}
String
accessTokenUrl
=
UrlBuilder
.
getGiteeAccessTokenUrl
(
AuthConfig
.
giteeClientId
,
AuthConfig
.
giteeClientSecret
,
code
,
AuthConfig
.
giteeRedirectUri
);
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
String
accessTokenStr
=
response
.
body
();
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
accessTokenStr
);
if
(
accessTokenObject
.
containsKey
(
"error"
))
{
return
AuthResponse
.
builder
()
.
code
(
500
)
.
msg
(
"Unable to get token from gitee using code ["
+
code
+
"]"
)
.
build
();
}
String
accessToken
=
accessTokenObject
.
getString
(
"access_token"
);
response
=
HttpRequest
.
get
(
UrlBuilder
.
getGiteeUserInfoUrl
(
accessToken
)).
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
return
AuthResponse
.
builder
()
.
data
(
AuthUser
.
builder
()
.
username
(
object
.
getString
(
"login"
))
.
avatar
(
object
.
getString
(
"avatar_url"
))
.
blog
(
object
.
getString
(
"blog"
))
.
nickname
(
object
.
getString
(
"name"
))
.
company
(
object
.
getString
(
"company"
))
.
location
(
object
.
getString
(
"address"
))
.
email
(
object
.
getString
(
"email"
))
.
remark
(
object
.
getString
(
"bio"
))
.
source
(
AuthSource
.
GITEE
)
.
build
())
.
build
();
}
}
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
0 → 100644
浏览文件 @
ebc19eea
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.AuthResponse
;
import
me.zhyd.oauth.model.AuthSource
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.utils.AuthConfigChecker
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 16:31
* @since 1.8
*/
public
class
AuthGithubRequest
implements
AuthRequest
{
@Override
public
void
authorize
(
HttpServletResponse
response
)
{
if
(!
AuthConfigChecker
.
isSupportedGithub
())
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
String
authorizeUrl
=
UrlBuilder
.
getGithubAuthorizeUrl
(
AuthConfig
.
githubClientId
,
AuthConfig
.
githubRedirectUri
);
try
{
response
.
sendRedirect
(
authorizeUrl
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
String
authorize
()
{
if
(!
AuthConfigChecker
.
isSupportedGithub
())
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
return
UrlBuilder
.
getGithubAuthorizeUrl
(
AuthConfig
.
githubClientId
,
AuthConfig
.
githubRedirectUri
);
}
@Override
public
AuthResponse
login
(
String
code
)
{
if
(!
AuthConfigChecker
.
isSupportedGithub
())
{
return
AuthResponse
.
builder
()
.
code
(
ResponseStatus
.
UNSUPPORTED
.
getCode
())
.
msg
(
ResponseStatus
.
UNSUPPORTED
.
getMsg
())
.
build
();
}
String
accessTokenUrl
=
UrlBuilder
.
getGithubAccessTokenUrl
(
AuthConfig
.
githubClientId
,
AuthConfig
.
githubClientSecret
,
code
,
AuthConfig
.
githubRedirectUri
);
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
String
accessTokenStr
=
response
.
body
();
String
accessToken
=
accessTokenStr
.
split
(
"&"
)[
0
];
response
=
HttpRequest
.
get
(
UrlBuilder
.
getGithubUserInfoUrl
(
accessToken
)).
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
return
AuthResponse
.
builder
()
.
data
(
AuthUser
.
builder
()
.
username
(
object
.
getString
(
"login"
))
.
avatar
(
object
.
getString
(
"avatar_url"
))
.
blog
(
object
.
getString
(
"blog"
))
.
nickname
(
object
.
getString
(
"name"
))
.
company
(
object
.
getString
(
"company"
))
.
location
(
object
.
getString
(
"location"
))
.
email
(
object
.
getString
(
"email"
))
.
remark
(
object
.
getString
(
"bio"
))
.
source
(
AuthSource
.
GITHUB
)
.
build
())
.
build
();
}
}
src/main/java/me/zhyd/oauth/request/AuthRequest.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.request
;
import
me.zhyd.oauth.exception.AuthException
;
import
me.zhyd.oauth.model.AuthResponse
;
import
javax.servlet.http.HttpServletResponse
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 15:45
* @since 1.8
*/
public
interface
AuthRequest
{
/**
* 自动跳转到认证页面
*
* @param response response
*/
default
void
authorize
(
HttpServletResponse
response
)
{
throw
new
AuthException
(
ResponseStatus
.
NOT_IMPLEMENTED
);
}
/**
* 返回认证url,可自行跳转页面
*/
default
String
authorize
()
{
throw
new
AuthException
(
ResponseStatus
.
NOT_IMPLEMENTED
);
}
/**
* 第三方登录
*
* @param code 通过authorize换回的code
* @return 返回登陆成功后的用户信息
*/
default
AuthResponse
login
(
String
code
)
{
throw
new
AuthException
(
ResponseStatus
.
NOT_IMPLEMENTED
);
}
}
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
0 → 100644
浏览文件 @
ebc19eea
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.AuthResponse
;
import
me.zhyd.oauth.model.AuthSource
;
import
me.zhyd.oauth.model.AuthUser
;
import
me.zhyd.oauth.model.AuthUserGender
;
import
me.zhyd.oauth.utils.AuthConfigChecker
;
import
me.zhyd.oauth.utils.IpUtils
;
import
me.zhyd.oauth.utils.StringUtils
;
import
me.zhyd.oauth.utils.UrlBuilder
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.net.InetAddress
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 16:31
* @since 1.8
*/
public
class
AuthWeiboRequest
implements
AuthRequest
{
@Override
public
void
authorize
(
HttpServletResponse
response
)
{
if
(!
AuthConfigChecker
.
isSupportedWeibo
())
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
String
authorizeUrl
=
UrlBuilder
.
getWeiboAuthorizeUrl
(
AuthConfig
.
weiboClientId
,
AuthConfig
.
weiboRedirectUri
);
try
{
response
.
sendRedirect
(
authorizeUrl
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
@Override
public
String
authorize
()
{
if
(!
AuthConfigChecker
.
isSupportedWeibo
())
{
throw
new
AuthException
(
ResponseStatus
.
UNSUPPORTED
);
}
return
UrlBuilder
.
getWeiboAuthorizeUrl
(
AuthConfig
.
weiboClientId
,
AuthConfig
.
weiboRedirectUri
);
}
@Override
public
AuthResponse
login
(
String
code
)
{
if
(!
AuthConfigChecker
.
isSupportedWeibo
())
{
return
AuthResponse
.
builder
()
.
code
(
ResponseStatus
.
UNSUPPORTED
.
getCode
())
.
msg
(
ResponseStatus
.
UNSUPPORTED
.
getMsg
())
.
build
();
}
String
accessTokenUrl
=
UrlBuilder
.
getWeiboAccessTokenUrl
(
AuthConfig
.
weiboClientId
,
AuthConfig
.
weiboClientSecret
,
code
,
AuthConfig
.
weiboRedirectUri
);
HttpResponse
response
=
HttpRequest
.
post
(
accessTokenUrl
).
execute
();
String
accessTokenStr
=
response
.
body
();
JSONObject
accessTokenObject
=
JSONObject
.
parseObject
(
accessTokenStr
);
if
(
accessTokenObject
.
containsKey
(
"error"
))
{
return
AuthResponse
.
builder
()
.
code
(
500
)
.
msg
(
"Unable to get token from gitee using code ["
+
code
+
"]"
)
.
build
();
}
String
accessToken
=
accessTokenObject
.
getString
(
"access_token"
);
String
uid
=
accessTokenObject
.
getString
(
"uid"
);
response
=
HttpRequest
.
get
(
UrlBuilder
.
getWeiboUserInfoUrl
(
uid
,
accessToken
))
.
header
(
"Authorization"
,
"OAuth2 "
+
accessToken
)
.
header
(
"API-RemoteIP"
,
IpUtils
.
getIp
())
.
execute
();
String
userInfo
=
response
.
body
();
JSONObject
object
=
JSONObject
.
parseObject
(
userInfo
);
return
AuthResponse
.
builder
()
.
data
(
AuthUser
.
builder
()
.
username
(
object
.
getString
(
"name"
))
.
avatar
(
object
.
getString
(
"profile_image_url"
))
.
blog
(
StringUtils
.
isEmpty
(
object
.
getString
(
"url"
))
?
"https://weibo.com/"
+
object
.
getString
(
"profile_url"
)
:
object
.
getString
(
"url"
))
.
nickname
(
object
.
getString
(
"screen_name"
))
.
location
(
object
.
getString
(
"location"
))
.
remark
(
object
.
getString
(
"description"
))
.
gender
(
AuthUserGender
.
getRealGender
(
object
.
getString
(
"gender"
)))
.
source
(
AuthSource
.
GITHUB
)
.
build
())
.
build
();
}
}
src/main/java/me/zhyd/oauth/request/ResponseStatus.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.request
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 16:37
* @since 1.8
*/
public
enum
ResponseStatus
{
FAILURE
(
5000
,
"Authentication failure"
),
NOT_IMPLEMENTED
(
5001
,
"Not Implemented"
),
UNSUPPORTED
(
5002
,
"Unsupported authentication, please check the configuration."
),
;
private
int
code
;
private
String
msg
;
ResponseStatus
(
int
code
,
String
msg
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
}
public
int
getCode
()
{
return
code
;
}
public
String
getMsg
()
{
return
msg
;
}
}
src/main/java/me/zhyd/oauth/utils/AuthConfigChecker.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.utils
;
import
me.zhyd.oauth.config.AuthConfig
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 15:46
* @since 1.8
*/
public
class
AuthConfigChecker
{
/**
* 是否支持微博
*
* @return true or false
*/
public
static
boolean
isSupportedWeibo
()
{
return
StringUtils
.
isNotEmpty
(
AuthConfig
.
weiboClientId
)
&&
StringUtils
.
isNotEmpty
(
AuthConfig
.
weiboClientSecret
)
&&
StringUtils
.
isNotEmpty
(
AuthConfig
.
weiboRedirectUri
);
}
/**
* 是否支持Github
*
* @return true or false
*/
public
static
boolean
isSupportedGithub
()
{
return
StringUtils
.
isNotEmpty
(
AuthConfig
.
githubClientId
)
&&
StringUtils
.
isNotEmpty
(
AuthConfig
.
githubClientSecret
)
&&
StringUtils
.
isNotEmpty
(
AuthConfig
.
githubRedirectUri
);
}
/**
* 是否支持Gitee
*
* @return true or false
*/
public
static
boolean
isSupportedGitee
()
{
return
StringUtils
.
isNotEmpty
(
AuthConfig
.
giteeClientId
)
&&
StringUtils
.
isNotEmpty
(
AuthConfig
.
giteeClientSecret
)
&&
StringUtils
.
isNotEmpty
(
AuthConfig
.
giteeRedirectUri
);
}
/**
* 是否支持QQ
*
* @return true or false
*/
@Deprecated
public
static
boolean
isSupportedQq
()
{
return
false
;
}
/**
* 是否支持微信
*
* @return true or false
*/
@Deprecated
public
static
boolean
isSupportedWeixin
()
{
return
false
;
}
}
src/main/java/me/zhyd/oauth/utils/IpUtils.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.utils
;
import
lombok.extern.slf4j.Slf4j
;
import
javax.servlet.http.HttpServletRequest
;
import
java.net.InetAddress
;
import
java.net.UnknownHostException
;
/**
* 获取IP的工具类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 16:31
* @since 1.0
*/
@Slf4j
public
class
IpUtils
{
/**
* 获取IP
*/
public
static
String
getIp
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
\ No newline at end of file
src/main/java/me/zhyd/oauth/utils/StringUtils.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.utils
;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 15:48
* @since 1.8
*/
public
class
StringUtils
{
public
static
boolean
isEmpty
(
String
str
)
{
return
null
==
str
||
str
.
isEmpty
();
}
public
static
boolean
isNotEmpty
(
String
str
)
{
return
!
isEmpty
(
str
);
}
}
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
0 → 100644
浏览文件 @
ebc19eea
package
me.zhyd.oauth.utils
;
import
me.zhyd.oauth.consts.ApiUrlConst
;
import
java.text.MessageFormat
;
/**
* Url构建工具类
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/1/31 12:26
* @since 1.0
*/
public
class
UrlBuilder
{
private
static
final
String
GITHUB_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&code={3}&redirect_uri={4}"
;
private
static
final
String
GITHUB_USER_INFO_PATTERN
=
"{0}?{1}"
;
private
static
final
String
GITHUB_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&state=1&redirect_uri={2}"
;
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}?uid={1}&access_token={2}"
;
private
static
final
String
WEIBO_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}"
;
private
static
final
String
GITEE_ACCESS_TOKEN_PATTERN
=
"{0}?client_id={1}&client_secret={2}&grant_type=authorization_code&code={3}&redirect_uri={4}"
;
private
static
final
String
GITEE_USER_INFO_PATTERN
=
"{0}?access_token={1}"
;
private
static
final
String
GITEE_AUTHORIZE_PATTERN
=
"{0}?client_id={1}&response_type=code&redirect_uri={2}"
;
/**
* 获取githubtoken的接口地址
*
* @param clientId github应用的Client ID
* @param clientSecret github应用的Client Secret
* @param code github授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return 换取github授权token的真实地址
*/
public
static
String
getGithubAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
GITHUB_ACCESS_TOKEN_PATTERN
,
ApiUrlConst
.
GITHUB_ACCESS_TOKEN_URL
,
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取github用户详情的接口地址
*
* @param token github 应用的token
* @return json
*/
public
static
String
getGithubUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
GITHUB_USER_INFO_PATTERN
,
ApiUrlConst
.
GITHUB_USER_INFO_URL
,
token
);
}
/**
* 获取github授权地址
*
* @param clientId github 应用的Client ID
* @param redirectUrl github 应用授权成功后的回调地址
* @return json
*/
public
static
String
getGithubAuthorizeUrl
(
String
clientId
,
String
redirectUrl
)
{
return
MessageFormat
.
format
(
GITHUB_AUTHORIZE_PATTERN
,
ApiUrlConst
.
GITHUB_AUTHORIZE_URL
,
clientId
,
redirectUrl
);
}
/**
* 获取weibo token的接口地址
*
* @param clientId weibo应用的App Key
* @param clientSecret weibo应用的App Secret
* @param code weibo授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return 换取weibo授权token的真实地址
*/
public
static
String
getWeiboAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
WEIBO_ACCESS_TOKEN_PATTERN
,
ApiUrlConst
.
WEIBO_ACCESS_TOKEN_URL
,
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取weibo用户详情的接口地址
*
* @param uid 用户id
* @param token weibo 应用的token
* @return json
*/
public
static
String
getWeiboUserInfoUrl
(
String
uid
,
String
token
)
{
return
MessageFormat
.
format
(
WEIBO_USER_INFO_PATTERN
,
ApiUrlConst
.
WEIBO_USER_INFO_URL
,
uid
,
token
);
}
/**
* 获取weibo授权地址
*
* @param clientId weibo 应用的Client ID
* @param redirectUrl weibo 应用授权成功后的回调地址
* @return json
*/
public
static
String
getWeiboAuthorizeUrl
(
String
clientId
,
String
redirectUrl
)
{
return
MessageFormat
.
format
(
WEIBO_AUTHORIZE_PATTERN
,
ApiUrlConst
.
WEIBO_AUTHORIZE_URL
,
clientId
,
redirectUrl
);
}
/**
* 获取gitee token的接口地址
*
* @param clientId gitee应用的Client ID
* @param clientSecret gitee应用的Client Secret
* @param code gitee授权前的code,用来换token
* @param redirectUri 待跳转的页面
* @return 换取gitee授权token的真实地址
*/
public
static
String
getGiteeAccessTokenUrl
(
String
clientId
,
String
clientSecret
,
String
code
,
String
redirectUri
)
{
return
MessageFormat
.
format
(
GITEE_ACCESS_TOKEN_PATTERN
,
ApiUrlConst
.
GITEE_ACCESS_TOKEN_URL
,
clientId
,
clientSecret
,
code
,
redirectUri
);
}
/**
* 获取gitee用户详情的接口地址
*
* @param token gitee 应用的token
* @return json
*/
public
static
String
getGiteeUserInfoUrl
(
String
token
)
{
return
MessageFormat
.
format
(
GITEE_USER_INFO_PATTERN
,
ApiUrlConst
.
GITEE_USER_INFO_URL
,
token
);
}
/**
* 获取gitee授权地址
*
* @param clientId gitee 应用的Client ID
* @param redirectUrl gitee 应用授权成功后的回调地址
* @return json
*/
public
static
String
getGiteeAuthorizeUrl
(
String
clientId
,
String
redirectUrl
)
{
return
MessageFormat
.
format
(
GITEE_AUTHORIZE_PATTERN
,
ApiUrlConst
.
GITEE_AUTHORIZE_URL
,
clientId
,
redirectUrl
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录