From 6c1bc61ece489e30c6b72eeef57251adda415c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91=E5=B0=8F=E9=A9=AC?= Date: Tue, 15 Jul 2025 18:31:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8=E4=B8=AD=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lktx/center/config/SsoAuthRequest.java | 65 +++++++++++++++++++ .../com/lktx/center/config/SsoSource.java | 27 ++++++++ .../center/controller/RestAuthController.java | 37 +++++++++++ 3 files changed, 129 insertions(+) create mode 100644 src/main/java/com/lktx/center/config/SsoAuthRequest.java create mode 100644 src/main/java/com/lktx/center/config/SsoSource.java create mode 100644 src/main/java/com/lktx/center/controller/RestAuthController.java diff --git a/src/main/java/com/lktx/center/config/SsoAuthRequest.java b/src/main/java/com/lktx/center/config/SsoAuthRequest.java new file mode 100644 index 0000000..0d001fd --- /dev/null +++ b/src/main/java/com/lktx/center/config/SsoAuthRequest.java @@ -0,0 +1,65 @@ +package com.lktx.center.config; + +import com.alibaba.fastjson.JSONObject; +import me.zhyd.oauth.cache.AuthStateCache; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.enums.AuthResponseStatus; +import me.zhyd.oauth.enums.AuthUserGender; +import me.zhyd.oauth.exception.AuthException; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.model.AuthResponse; +import me.zhyd.oauth.model.AuthToken; +import me.zhyd.oauth.model.AuthUser; +import me.zhyd.oauth.request.AuthDefaultRequest; +import me.zhyd.oauth.utils.*; + +import java.util.Map; + +public class SsoAuthRequest extends AuthDefaultRequest { + + public SsoAuthRequest(AuthConfig config) { + super(config, SsoSource.SSO_SOURCE); + } + + public SsoAuthRequest(AuthConfig config, AuthStateCache authStateCache) { + super(config,SsoSource.SSO_SOURCE, authStateCache); + } + + public AuthToken getAccessToken(AuthCallback authCallback) { + return AuthToken.builder().accessCode(authCallback.getCode()).build(); + } + + private AuthToken getAuthToken(JSONObject object) { + this.checkResponse(object); + return AuthToken.builder().accessToken(object.getString("access_token")).expireIn(object.getIntValue("expires_in")).tokenType(object.getString("token_type")).idToken(object.getString("id_token")).refreshToken(object.getString("refresh_token")).uid(object.getString("userId")).openId(object.getString("userId")).build(); + } + + private void checkResponse(JSONObject object) { + if (object.containsKey("error")) { + throw new AuthException(object.getString("error_description")); + } + } + + public AuthUser getUserInfo(AuthToken authToken) { + String response = this.doPostAuthorizationCode(authToken.getAccessCode()); + JSONObject accessTokenObject = JSONObject.parseObject(response); + if (accessTokenObject.containsKey("error")) { + throw new AuthException(accessTokenObject.getString("error_description")); + } else { + authToken = this.getAuthToken(accessTokenObject); + String nick = GlobalAuthUtils.urlDecode(accessTokenObject.getString("taobao_user_nick")); + return AuthUser.builder().rawUserInfo(accessTokenObject).uuid(StringUtils.isEmpty(authToken.getUid()) ? authToken.getOpenId() : authToken.getUid()).username(nick).nickname(nick).gender(AuthUserGender.UNKNOWN).token(authToken).source(this.source.toString()).build(); + } + } + + public AuthResponse refresh(AuthToken oldToken) { + String tokenUrl = this.refreshTokenUrl(oldToken.getRefreshToken()); + String response = (new HttpUtils(this.config.getHttpConfig())).post(tokenUrl).getBody(); + JSONObject accessTokenObject = JSONObject.parseObject(response); + return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(this.getAuthToken(accessTokenObject)).build(); + } + + public String authorize(String state) { + return UrlBuilder.fromBaseUrl(this.source.authorize()).queryParam("response_type", "code").queryParam("client_id", this.config.getClientId()).queryParam("redirect_uri", this.config.getRedirectUri()).queryParam("view", "web").queryParam("state", this.getRealState(state)).build(); + } +} diff --git a/src/main/java/com/lktx/center/config/SsoSource.java b/src/main/java/com/lktx/center/config/SsoSource.java new file mode 100644 index 0000000..0d2c809 --- /dev/null +++ b/src/main/java/com/lktx/center/config/SsoSource.java @@ -0,0 +1,27 @@ +package com.lktx.center.config; + +import me.zhyd.oauth.config.AuthSource; +import me.zhyd.oauth.request.AuthDefaultRequest; + +public enum SsoSource implements AuthSource { + SSO_SOURCE { + public String authorize() { + return "https://github.com/login/oauth/authorize"; + } + + public String accessToken() { + return "https://github.com/login/oauth/access_token"; + } + + public String userInfo() { + return "https://api.github.com/user"; + } + + @Override + public Class getTargetClass() { + return SsoAuthRequest.class; + } + + }, + +} \ No newline at end of file diff --git a/src/main/java/com/lktx/center/controller/RestAuthController.java b/src/main/java/com/lktx/center/controller/RestAuthController.java new file mode 100644 index 0000000..772214b --- /dev/null +++ b/src/main/java/com/lktx/center/controller/RestAuthController.java @@ -0,0 +1,37 @@ +package com.lktx.center.controller; + +import cn.hserver.plugin.web.annotation.Controller; +import cn.hserver.plugin.web.annotation.RequestMapping; +import cn.hserver.plugin.web.interfaces.HttpResponse; +import com.lktx.center.config.SsoAuthRequest; +import me.zhyd.oauth.config.AuthConfig; +import me.zhyd.oauth.model.AuthCallback; +import me.zhyd.oauth.request.AuthQqRequest; +import me.zhyd.oauth.request.AuthRequest; +import me.zhyd.oauth.utils.AuthStateUtils; + +import java.io.IOException; + +@Controller("/oauth") +public class RestAuthController { + + @RequestMapping("/render") + public void renderAuth(HttpResponse response) throws IOException { + AuthRequest authRequest = getAuthRequest(); + response.redirect(authRequest.authorize(AuthStateUtils.createState())); + } + + @RequestMapping("/callback") + public Object login(AuthCallback callback) { + AuthRequest authRequest = getAuthRequest(); + return authRequest.login(callback); + } + + private AuthRequest getAuthRequest() { + return new SsoAuthRequest(AuthConfig.builder() + .clientId("App ID") + .clientSecret("App Key") + .redirectUri("网站回调域") + .build()); + } +} \ No newline at end of file