mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2025-12-06 16:58:24 +08:00
🎉 初始化提交
This commit is contained in:
parent
6adf47f06f
commit
37179e7b5a
47
src/main/java/me/zhyd/oauth/config/AuthConfig.java
Normal file
47
src/main/java/me/zhyd/oauth/config/AuthConfig.java
Normal file
@ -0,0 +1,47 @@
|
||||
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;
|
||||
}
|
||||
59
src/main/java/me/zhyd/oauth/consts/ApiUrlConst.java
Normal file
59
src/main/java/me/zhyd/oauth/consts/ApiUrlConst.java
Normal file
@ -0,0 +1,59 @@
|
||||
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";
|
||||
|
||||
}
|
||||
36
src/main/java/me/zhyd/oauth/exception/AuthException.java
Normal file
36
src/main/java/me/zhyd/oauth/exception/AuthException.java
Normal file
@ -0,0 +1,36 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
19
src/main/java/me/zhyd/oauth/model/AuthResponse.java
Normal file
19
src/main/java/me/zhyd/oauth/model/AuthResponse.java
Normal file
@ -0,0 +1,19 @@
|
||||
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;
|
||||
}
|
||||
16
src/main/java/me/zhyd/oauth/model/AuthSource.java
Normal file
16
src/main/java/me/zhyd/oauth/model/AuthSource.java
Normal file
@ -0,0 +1,16 @@
|
||||
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
|
||||
}
|
||||
20
src/main/java/me/zhyd/oauth/model/AuthToken.java
Normal file
20
src/main/java/me/zhyd/oauth/model/AuthToken.java
Normal file
@ -0,0 +1,20 @@
|
||||
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;
|
||||
}
|
||||
26
src/main/java/me/zhyd/oauth/model/AuthUser.java
Normal file
26
src/main/java/me/zhyd/oauth/model/AuthUser.java
Normal file
@ -0,0 +1,26 @@
|
||||
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;
|
||||
}
|
||||
40
src/main/java/me/zhyd/oauth/model/AuthUserGender.java
Normal file
40
src/main/java/me/zhyd/oauth/model/AuthUserGender.java
Normal file
@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
83
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
Normal file
83
src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
Normal file
@ -0,0 +1,83 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
76
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
Normal file
76
src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
Normal file
@ -0,0 +1,76 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
42
src/main/java/me/zhyd/oauth/request/AuthRequest.java
Normal file
42
src/main/java/me/zhyd/oauth/request/AuthRequest.java
Normal file
@ -0,0 +1,42 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
90
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
Normal file
90
src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
Normal file
@ -0,0 +1,90 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
32
src/main/java/me/zhyd/oauth/request/ResponseStatus.java
Normal file
32
src/main/java/me/zhyd/oauth/request/ResponseStatus.java
Normal file
@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
60
src/main/java/me/zhyd/oauth/utils/AuthConfigChecker.java
Normal file
60
src/main/java/me/zhyd/oauth/utils/AuthConfigChecker.java
Normal file
@ -0,0 +1,60 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
32
src/main/java/me/zhyd/oauth/utils/IpUtils.java
Normal file
32
src/main/java/me/zhyd/oauth/utils/IpUtils.java
Normal file
@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/main/java/me/zhyd/oauth/utils/StringUtils.java
Normal file
19
src/main/java/me/zhyd/oauth/utils/StringUtils.java
Normal file
@ -0,0 +1,19 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
132
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
Normal file
132
src/main/java/me/zhyd/oauth/utils/UrlBuilder.java
Normal file
@ -0,0 +1,132 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user