1
0
mirror of synced 2026-02-19 18:47:46 +08:00

🎉 修改认证方式为显式的传参

This commit is contained in:
yadong.zhang
2019-02-18 09:39:21 +08:00
parent c327cbb1c3
commit 002430e4ac
11 changed files with 184 additions and 57 deletions

View File

@@ -1,5 +1,8 @@
package me.zhyd.oauth.config;
import lombok.Builder;
import lombok.Getter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
@@ -7,41 +10,13 @@ package me.zhyd.oauth.config;
* @date 2019/1/31 14:03
* @since 1.8
*/
@Getter
@Builder
public class AuthConfig {
private String clientId;
private String clientSecret;
/**
* 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;
private String redirectUri;
}

View File

@@ -0,0 +1,16 @@
package me.zhyd.oauth.config;
import lombok.Builder;
import lombok.Getter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/2/14 13:34
* @since 1.8
*/
@Getter
@Builder
public class GiteeConfig extends AuthConfig {
}

View File

@@ -0,0 +1,16 @@
package me.zhyd.oauth.config;
import lombok.Builder;
import lombok.Getter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/2/14 13:34
* @since 1.8
*/
@Getter
@Builder
public class GithubConfig extends AuthConfig {
}

View File

@@ -0,0 +1,17 @@
package me.zhyd.oauth.config;
import lombok.Builder;
import lombok.Getter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/2/14 13:34
* @since 1.8
*/
@Getter
@Builder
@Deprecated
public class QqConfig extends AuthConfig {
}

View File

@@ -0,0 +1,17 @@
package me.zhyd.oauth.config;
import lombok.Builder;
import lombok.Getter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/2/14 13:34
* @since 1.8
*/
@Getter
@Builder
@Deprecated
public class WechatConfig extends AuthConfig {
}

View File

@@ -0,0 +1,16 @@
package me.zhyd.oauth.config;
import lombok.Builder;
import lombok.Getter;
/**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0
* @website https://www.zhyd.me
* @date 2019/2/14 13:34
* @since 1.8
*/
@Getter
@Builder
public class WeiboConfig extends AuthConfig {
}

View File

@@ -24,11 +24,11 @@ import java.io.IOException;
public class AuthGiteeRequest implements AuthRequest {
@Override
public void authorize(HttpServletResponse response) {
public void authorize(AuthConfig config, HttpServletResponse response) {
if (!AuthConfigChecker.isSupportedGitee()) {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
String authorizeUrl = UrlBuilder.getGiteeAuthorizeUrl(AuthConfig.giteeClientId, AuthConfig.giteeRedirectUri);
String authorizeUrl = UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
try {
response.sendRedirect(authorizeUrl);
} catch (IOException e) {
@@ -37,22 +37,22 @@ public class AuthGiteeRequest implements AuthRequest {
}
@Override
public String authorize() {
public String authorize(AuthConfig config) {
if (!AuthConfigChecker.isSupportedGitee()) {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
return UrlBuilder.getGiteeAuthorizeUrl(AuthConfig.giteeClientId, AuthConfig.giteeRedirectUri);
return UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
@Override
public AuthResponse login(String code) {
public AuthResponse login(AuthConfig config, 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);
String accessTokenUrl = UrlBuilder.getGiteeAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
String accessTokenStr = response.body();
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);

View File

@@ -24,11 +24,11 @@ import java.io.IOException;
public class AuthGithubRequest implements AuthRequest {
@Override
public void authorize(HttpServletResponse response) {
public void authorize(AuthConfig config, HttpServletResponse response) {
if (!AuthConfigChecker.isSupportedGithub()) {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
String authorizeUrl = UrlBuilder.getGithubAuthorizeUrl(AuthConfig.githubClientId, AuthConfig.githubRedirectUri);
String authorizeUrl = UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
try {
response.sendRedirect(authorizeUrl);
} catch (IOException e) {
@@ -37,22 +37,22 @@ public class AuthGithubRequest implements AuthRequest {
}
@Override
public String authorize() {
public String authorize(AuthConfig config) {
if (!AuthConfigChecker.isSupportedGithub()) {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
return UrlBuilder.getGithubAuthorizeUrl(AuthConfig.githubClientId, AuthConfig.githubRedirectUri);
return UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
@Override
public AuthResponse login(String code) {
public AuthResponse login(AuthConfig config, 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);
String accessTokenUrl = UrlBuilder.getGithubAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
String accessTokenStr = response.body();
String accessToken = accessTokenStr.split("&")[0];

View File

@@ -1,5 +1,6 @@
package me.zhyd.oauth.request;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponse;
@@ -17,26 +18,30 @@ public interface AuthRequest {
/**
* 自动跳转到认证页面
*
* @param config 授权的配置,对应不同平台
* @param response response
*/
default void authorize(HttpServletResponse response) {
default void authorize(AuthConfig config, HttpServletResponse response) {
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
}
/**
* 返回认证url可自行跳转页面
*
* @param config 授权的配置,对应不同平台
*/
default String authorize() {
default String authorize(AuthConfig config) {
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
}
/**
* 第三方登录
*
* @param code 通过authorize换回的code
* @param config 授权的配置,对应不同平台
* @param code 通过authorize换回的code
* @return 返回登陆成功后的用户信息
*/
default AuthResponse login(String code) {
default AuthResponse login(AuthConfig config, String code) {
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
}
}

View File

@@ -16,7 +16,6 @@ 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)
@@ -28,11 +27,11 @@ import java.net.InetAddress;
public class AuthWeiboRequest implements AuthRequest {
@Override
public void authorize(HttpServletResponse response) {
public void authorize(AuthConfig config, HttpServletResponse response) {
if (!AuthConfigChecker.isSupportedWeibo()) {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
String authorizeUrl = UrlBuilder.getWeiboAuthorizeUrl(AuthConfig.weiboClientId, AuthConfig.weiboRedirectUri);
String authorizeUrl = UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
try {
response.sendRedirect(authorizeUrl);
} catch (IOException e) {
@@ -41,22 +40,22 @@ public class AuthWeiboRequest implements AuthRequest {
}
@Override
public String authorize() {
public String authorize(AuthConfig config) {
if (!AuthConfigChecker.isSupportedWeibo()) {
throw new AuthException(ResponseStatus.UNSUPPORTED);
}
return UrlBuilder.getWeiboAuthorizeUrl(AuthConfig.weiboClientId, AuthConfig.weiboRedirectUri);
return UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
}
@Override
public AuthResponse login(String code) {
public AuthResponse login(AuthConfig config, 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);
String accessTokenUrl = UrlBuilder.getWeiboAccessTokenUrl(config.getClientId(), config.getClientSecret(), code, config.getRedirectUri());
HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
String accessTokenStr = response.body();
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);