|
|
@@ -1,15 +1,13 @@ |
|
|
|
package com.xkcoding.oauth.controller; |
|
|
|
|
|
|
|
import com.xkcoding.oauth.config.props.CommonProperties; |
|
|
|
import cn.hutool.core.lang.Dict; |
|
|
|
import com.xkcoding.oauth.config.props.OAuthProperties; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import me.zhyd.oauth.config.AuthConfig; |
|
|
|
import me.zhyd.oauth.model.AuthResponse; |
|
|
|
import me.zhyd.oauth.model.AuthSource; |
|
|
|
import me.zhyd.oauth.request.AuthGithubRequest; |
|
|
|
import me.zhyd.oauth.request.AuthRequest; |
|
|
|
import me.zhyd.oauth.request.AuthWeChatRequest; |
|
|
|
import me.zhyd.oauth.request.*; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.web.bind.annotation.GetMapping; |
|
|
|
import org.springframework.web.bind.annotation.PathVariable; |
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; |
|
|
|
import org.springframework.web.bind.annotation.RestController; |
|
|
@@ -36,6 +34,18 @@ import java.io.IOException; |
|
|
|
public class OauthController { |
|
|
|
private final OAuthProperties properties; |
|
|
|
|
|
|
|
/** |
|
|
|
* 登录类型 |
|
|
|
*/ |
|
|
|
@GetMapping |
|
|
|
public Dict loginType() { |
|
|
|
return Dict.create() |
|
|
|
.set("QQ登录", "http://oauth.xkcoding.com/demo/oauth/login/qq") |
|
|
|
.set("GitHub登录", "http://oauth.xkcoding.com/demo/oauth/login/github") |
|
|
|
.set("微信登录", "http://oauth.xkcoding.com/demo/oauth/login/wechat") |
|
|
|
.set("Google登录", "http://oauth.xkcoding.com/demo/oauth/login/google"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 登录 |
|
|
|
* |
|
|
@@ -65,28 +75,32 @@ public class OauthController { |
|
|
|
private AuthRequest getAuthRequest(String oauthType) { |
|
|
|
AuthSource authSource = AuthSource.valueOf(oauthType.toUpperCase()); |
|
|
|
switch (authSource) { |
|
|
|
case QQ: |
|
|
|
return getQqAuthRequest(); |
|
|
|
case GITHUB: |
|
|
|
return getGithubAuthRequest(); |
|
|
|
case WECHAT: |
|
|
|
return getWechatAuthRequest(); |
|
|
|
case GOOGLE: |
|
|
|
return getGoogleAuthRequest(); |
|
|
|
default: |
|
|
|
throw new RuntimeException("暂不支持的第三方登录"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private AuthRequest getQqAuthRequest() { |
|
|
|
return new AuthQqRequest(properties.getQq()); |
|
|
|
} |
|
|
|
|
|
|
|
private AuthRequest getGithubAuthRequest() { |
|
|
|
return new AuthGithubRequest(buildAuthConfig(properties.getGithub())); |
|
|
|
return new AuthGithubRequest(properties.getGithub()); |
|
|
|
} |
|
|
|
|
|
|
|
private AuthRequest getWechatAuthRequest() { |
|
|
|
return new AuthWeChatRequest(buildAuthConfig(properties.getWechat())); |
|
|
|
return new AuthWeChatRequest(properties.getWechat()); |
|
|
|
} |
|
|
|
|
|
|
|
private AuthConfig buildAuthConfig(CommonProperties properties) { |
|
|
|
return AuthConfig.builder() |
|
|
|
.clientId(properties.getClientId()) |
|
|
|
.clientSecret(properties.getClientSecret()) |
|
|
|
.redirectUri(properties.getRedirectUri()) |
|
|
|
.build(); |
|
|
|
private AuthRequest getGoogleAuthRequest() { |
|
|
|
return new AuthGoogleRequest(properties.getGoogle()); |
|
|
|
} |
|
|
|
} |