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

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

上级 8d77311a
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.zhyd.oauth</groupId> <groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId> <artifactId>JustAuth</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<name>JustAuth</name> <name>JustAuth</name>
<url>https://gitee.com/yadong.zhang/JustAuth</url> <url>https://gitee.com/yadong.zhang/JustAuth</url>
<description>史上最全的整合第三方登录的工具, Just Auth</description> <description>史上最全的整合第三方登录的工具, Just Auth</description>
<licenses> <licenses>
<license> <license>
<name>The Apache Software License, Version 2.0</name> <name>The Apache Software License, Version 2.0</name>
<url>https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE</url> <url>https://gitee.com/yadong.zhang/JustAuth/blob/master/LICENSE</url>
</license> </license>
</licenses> </licenses>
<developers> <scm>
<developer> <connection>scm:git:https://gitee.com/yadong.zhang/JustAuth.git</connection>
<name>yadong.zhang</name> <developerConnection>scm:git:https://gitee.com/yadong.zhang/JustAuth.git</developerConnection>
<email>yadong.zhang0415@gmail.com</email> <url>https://gitee.com/yadong.zhang/JustAuth</url>
</developer> </scm>
</developers>
<developers>
<properties> <developer>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <name>yadong.zhang</name>
<java.version>1.8</java.version> <email>yadong.zhang0415@gmail.com</email>
<maven.compiler.source>1.8</maven.compiler.source> </developer>
<maven.compiler.target>1.8</maven.compiler.target> </developers>
<maven-source.version>2.2.1</maven-source.version>
<maven-compiler.version>3.7.0</maven-compiler.version> <properties>
<maven.test.skip>true</maven.test.skip> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hutool-version>4.1.21</hutool-version> <java.version>1.8</java.version>
<lombok-version>1.18.4</lombok-version> <maven.compiler.source>1.8</maven.compiler.source>
<junit-version>4.11</junit-version> <maven.compiler.target>1.8</maven.compiler.target>
<servlet-api-version>2.5</servlet-api-version> <maven-source.version>2.2.1</maven-source.version>
<fastjson-version>1.2.44</fastjson-version> <maven-compiler.version>3.7.0</maven-compiler.version>
</properties> <maven.test.skip>true</maven.test.skip>
<hutool-version>4.1.21</hutool-version>
<dependencies> <lombok-version>1.18.4</lombok-version>
<dependency> <junit-version>4.11</junit-version>
<groupId>org.projectlombok</groupId> <servlet-api-version>2.5</servlet-api-version>
<artifactId>lombok</artifactId> <fastjson-version>1.2.44</fastjson-version>
<version>${lombok-version}</version> </properties>
</dependency>
<dependency> <dependencies>
<groupId>cn.hutool</groupId> <dependency>
<artifactId>hutool-http</artifactId> <groupId>org.projectlombok</groupId>
<version>${hutool-version}</version> <artifactId>lombok</artifactId>
</dependency> <version>${lombok-version}</version>
<dependency> </dependency>
<groupId>junit</groupId> <dependency>
<artifactId>junit</artifactId> <groupId>cn.hutool</groupId>
<version>${junit-version}</version> <artifactId>hutool-http</artifactId>
<scope>test</scope> <version>${hutool-version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>junit</groupId>
<artifactId>servlet-api</artifactId> <artifactId>junit</artifactId>
<version>${servlet-api-version}</version> <version>${junit-version}</version>
</dependency> <scope>test</scope>
<dependency> </dependency>
<groupId>com.alibaba</groupId> <dependency>
<artifactId>fastjson</artifactId> <groupId>javax.servlet</groupId>
<version>${fastjson-version}</version> <artifactId>servlet-api</artifactId>
</dependency> <version>${servlet-api-version}</version>
</dependencies> </dependency>
<dependency>
<build> <groupId>com.alibaba</groupId>
<finalName>${project.artifactId}-${project.version}</finalName> <artifactId>fastjson</artifactId>
<plugins> <version>${fastjson-version}</version>
<plugin> </dependency>
<groupId>org.apache.maven.plugins</groupId> </dependencies>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler.version}</version> <build>
<configuration> <finalName>${project.artifactId}-${project.version}</finalName>
<encoding>${project.build.sourceEncoding}</encoding> <plugins>
<source>${java.version}</source> <plugin>
<target>${java.version}</target> <groupId>org.apache.maven.plugins</groupId>
</configuration> <artifactId>maven-compiler-plugin</artifactId>
</plugin> <version>${maven-compiler.version}</version>
<plugin> <configuration>
<artifactId>maven-source-plugin</artifactId> <encoding>${project.build.sourceEncoding}</encoding>
<version>${maven-source.version}</version> <source>${java.version}</source>
<inherited>true</inherited> <target>${java.version}</target>
<executions> </configuration>
<execution> </plugin>
<phase>package</phase> <plugin>
<goals> <artifactId>maven-source-plugin</artifactId>
<goal>jar-no-fork</goal> <version>${maven-source.version}</version>
</goals> <inherited>true</inherited>
</execution> <executions>
</executions> <execution>
</plugin> <phase>package</phase>
<!-- Javadoc --> <goals>
<plugin> <goal>jar-no-fork</goal>
<groupId>org.apache.maven.plugins</groupId> </goals>
<artifactId>maven-javadoc-plugin</artifactId> </execution>
</plugin> </executions>
<!-- GPG --> </plugin>
<plugin> <!-- Javadoc -->
<groupId>org.apache.maven.plugins</groupId> <plugin>
<artifactId>maven-gpg-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId>
</plugin> <artifactId>maven-javadoc-plugin</artifactId>
</plugins> </plugin>
</build> <!-- GPG -->
</project> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
</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; package me.zhyd.oauth.config;
import lombok.Builder;
import lombok.Getter;
/** /**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com) * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0 * @version 1.0
...@@ -7,41 +10,13 @@ package me.zhyd.oauth.config; ...@@ -7,41 +10,13 @@ package me.zhyd.oauth.config;
* @date 2019/1/31 14:03 * @date 2019/1/31 14:03
* @since 1.8 * @since 1.8
*/ */
@Getter
@Builder
public class AuthConfig { 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; ...@@ -24,11 +24,11 @@ import java.io.IOException;
public class AuthGiteeRequest implements AuthRequest { public class AuthGiteeRequest implements AuthRequest {
@Override @Override
public void authorize(HttpServletResponse response) { public void authorize(AuthConfig config, HttpServletResponse response) {
if (!AuthConfigChecker.isSupportedGitee()) { if (!AuthConfigChecker.isSupportedGitee()) {
throw new AuthException(ResponseStatus.UNSUPPORTED); throw new AuthException(ResponseStatus.UNSUPPORTED);
} }
String authorizeUrl = UrlBuilder.getGiteeAuthorizeUrl(AuthConfig.giteeClientId, AuthConfig.giteeRedirectUri); String authorizeUrl = UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
try { try {
response.sendRedirect(authorizeUrl); response.sendRedirect(authorizeUrl);
} catch (IOException e) { } catch (IOException e) {
...@@ -37,22 +37,22 @@ public class AuthGiteeRequest implements AuthRequest { ...@@ -37,22 +37,22 @@ public class AuthGiteeRequest implements AuthRequest {
} }
@Override @Override
public String authorize() { public String authorize(AuthConfig config) {
if (!AuthConfigChecker.isSupportedGitee()) { if (!AuthConfigChecker.isSupportedGitee()) {
throw new AuthException(ResponseStatus.UNSUPPORTED); throw new AuthException(ResponseStatus.UNSUPPORTED);
} }
return UrlBuilder.getGiteeAuthorizeUrl(AuthConfig.giteeClientId, AuthConfig.giteeRedirectUri); return UrlBuilder.getGiteeAuthorizeUrl(config.getClientId(), config.getRedirectUri());
} }
@Override @Override
public AuthResponse login(String code) { public AuthResponse login(AuthConfig config, String code) {
if (!AuthConfigChecker.isSupportedGitee()) { if (!AuthConfigChecker.isSupportedGitee()) {
return AuthResponse.builder() return AuthResponse.builder()
.code(ResponseStatus.UNSUPPORTED.getCode()) .code(ResponseStatus.UNSUPPORTED.getCode())
.msg(ResponseStatus.UNSUPPORTED.getMsg()) .msg(ResponseStatus.UNSUPPORTED.getMsg())
.build(); .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(); HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
String accessTokenStr = response.body(); String accessTokenStr = response.body();
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr); JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
......
...@@ -24,11 +24,11 @@ import java.io.IOException; ...@@ -24,11 +24,11 @@ import java.io.IOException;
public class AuthGithubRequest implements AuthRequest { public class AuthGithubRequest implements AuthRequest {
@Override @Override
public void authorize(HttpServletResponse response) { public void authorize(AuthConfig config, HttpServletResponse response) {
if (!AuthConfigChecker.isSupportedGithub()) { if (!AuthConfigChecker.isSupportedGithub()) {
throw new AuthException(ResponseStatus.UNSUPPORTED); throw new AuthException(ResponseStatus.UNSUPPORTED);
} }
String authorizeUrl = UrlBuilder.getGithubAuthorizeUrl(AuthConfig.githubClientId, AuthConfig.githubRedirectUri); String authorizeUrl = UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
try { try {
response.sendRedirect(authorizeUrl); response.sendRedirect(authorizeUrl);
} catch (IOException e) { } catch (IOException e) {
...@@ -37,22 +37,22 @@ public class AuthGithubRequest implements AuthRequest { ...@@ -37,22 +37,22 @@ public class AuthGithubRequest implements AuthRequest {
} }
@Override @Override
public String authorize() { public String authorize(AuthConfig config) {
if (!AuthConfigChecker.isSupportedGithub()) { if (!AuthConfigChecker.isSupportedGithub()) {
throw new AuthException(ResponseStatus.UNSUPPORTED); throw new AuthException(ResponseStatus.UNSUPPORTED);
} }
return UrlBuilder.getGithubAuthorizeUrl(AuthConfig.githubClientId, AuthConfig.githubRedirectUri); return UrlBuilder.getGithubAuthorizeUrl(config.getClientId(), config.getRedirectUri());
} }
@Override @Override
public AuthResponse login(String code) { public AuthResponse login(AuthConfig config, String code) {
if (!AuthConfigChecker.isSupportedGithub()) { if (!AuthConfigChecker.isSupportedGithub()) {
return AuthResponse.builder() return AuthResponse.builder()
.code(ResponseStatus.UNSUPPORTED.getCode()) .code(ResponseStatus.UNSUPPORTED.getCode())
.msg(ResponseStatus.UNSUPPORTED.getMsg()) .msg(ResponseStatus.UNSUPPORTED.getMsg())
.build(); .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(); HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
String accessTokenStr = response.body(); String accessTokenStr = response.body();
String accessToken = accessTokenStr.split("&")[0]; String accessToken = accessTokenStr.split("&")[0];
......
package me.zhyd.oauth.request; package me.zhyd.oauth.request;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException; import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.model.AuthResponse;
...@@ -17,26 +18,30 @@ public interface AuthRequest { ...@@ -17,26 +18,30 @@ public interface AuthRequest {
/** /**
* 自动跳转到认证页面 * 自动跳转到认证页面
* *
* @param config 授权的配置,对应不同平台
* @param response response * @param response response
*/ */
default void authorize(HttpServletResponse response) { default void authorize(AuthConfig config, HttpServletResponse response) {
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED); throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
} }
/** /**
* 返回认证url,可自行跳转页面 * 返回认证url,可自行跳转页面
*
* @param config 授权的配置,对应不同平台
*/ */
default String authorize() { default String authorize(AuthConfig config) {
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED); throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
} }
/** /**
* 第三方登录 * 第三方登录
* *
* @param code 通过authorize换回的code * @param config 授权的配置,对应不同平台
* @param code 通过authorize换回的code
* @return 返回登陆成功后的用户信息 * @return 返回登陆成功后的用户信息
*/ */
default AuthResponse login(String code) { default AuthResponse login(AuthConfig config, String code) {
throw new AuthException(ResponseStatus.NOT_IMPLEMENTED); throw new AuthException(ResponseStatus.NOT_IMPLEMENTED);
} }
} }
...@@ -16,7 +16,6 @@ import me.zhyd.oauth.utils.UrlBuilder; ...@@ -16,7 +16,6 @@ import me.zhyd.oauth.utils.UrlBuilder;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
/** /**
* @author yadong.zhang (yadong.zhang0415(a)gmail.com) * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
...@@ -28,11 +27,11 @@ import java.net.InetAddress; ...@@ -28,11 +27,11 @@ import java.net.InetAddress;
public class AuthWeiboRequest implements AuthRequest { public class AuthWeiboRequest implements AuthRequest {
@Override @Override
public void authorize(HttpServletResponse response) { public void authorize(AuthConfig config, HttpServletResponse response) {
if (!AuthConfigChecker.isSupportedWeibo()) { if (!AuthConfigChecker.isSupportedWeibo()) {
throw new AuthException(ResponseStatus.UNSUPPORTED); throw new AuthException(ResponseStatus.UNSUPPORTED);
} }
String authorizeUrl = UrlBuilder.getWeiboAuthorizeUrl(AuthConfig.weiboClientId, AuthConfig.weiboRedirectUri); String authorizeUrl = UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
try { try {
response.sendRedirect(authorizeUrl); response.sendRedirect(authorizeUrl);
} catch (IOException e) { } catch (IOException e) {
...@@ -41,22 +40,22 @@ public class AuthWeiboRequest implements AuthRequest { ...@@ -41,22 +40,22 @@ public class AuthWeiboRequest implements AuthRequest {
} }
@Override @Override
public String authorize() { public String authorize(AuthConfig config) {
if (!AuthConfigChecker.isSupportedWeibo()) { if (!AuthConfigChecker.isSupportedWeibo()) {
throw new AuthException(ResponseStatus.UNSUPPORTED); throw new AuthException(ResponseStatus.UNSUPPORTED);
} }
return UrlBuilder.getWeiboAuthorizeUrl(AuthConfig.weiboClientId, AuthConfig.weiboRedirectUri); return UrlBuilder.getWeiboAuthorizeUrl(config.getClientId(), config.getRedirectUri());
} }
@Override @Override
public AuthResponse login(String code) { public AuthResponse login(AuthConfig config, String code) {
if (!AuthConfigChecker.isSupportedWeibo()) { if (!AuthConfigChecker.isSupportedWeibo()) {
return AuthResponse.builder() return AuthResponse.builder()
.code(ResponseStatus.UNSUPPORTED.getCode()) .code(ResponseStatus.UNSUPPORTED.getCode())
.msg(ResponseStatus.UNSUPPORTED.getMsg()) .msg(ResponseStatus.UNSUPPORTED.getMsg())
.build(); .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(); HttpResponse response = HttpRequest.post(accessTokenUrl).execute();
String accessTokenStr = response.body(); String accessTokenStr = response.body();
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr); JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
......
...@@ -15,7 +15,6 @@ import java.net.UnknownHostException; ...@@ -15,7 +15,6 @@ import java.net.UnknownHostException;
* @date 2019/1/31 16:31 * @date 2019/1/31 16:31
* @since 1.0 * @since 1.0
*/ */
@Slf4j
public class IpUtils { public class IpUtils {
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册