提交 002430e4 编写于 作者: 智布道's avatar 智布道 👁

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

上级 c327cbb1
......@@ -19,6 +19,12 @@
</license>
</licenses>
<scm>
<connection>scm:git:https://gitee.com/yadong.zhang/JustAuth.git</connection>
<developerConnection>scm:git:https://gitee.com/yadong.zhang/JustAuth.git</developerConnection>
<url>https://gitee.com/yadong.zhang/JustAuth</url>
</scm>
<developers>
<developer>
<name>yadong.zhang</name>
......@@ -108,4 +114,64 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source.version}</version>
<inherited>true</inherited>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- GPG -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>sonatype-oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>sonatype-oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>
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;
}
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 {
}
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 {
}
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 {
}
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 {
}
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 {
}
......@@ -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);
......
......@@ -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];
......
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);
}
}
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册