应用中心

This commit is contained in:
黑小马 2025-07-15 18:31:51 +08:00
parent 57af6b5757
commit 6c1bc61ece
3 changed files with 129 additions and 0 deletions

View File

@ -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<AuthToken> 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();
}
}

View File

@ -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<? extends AuthDefaultRequest> getTargetClass() {
return SsoAuthRequest.class;
}
},
}

View File

@ -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());
}
}