|
|
@@ -4,6 +4,7 @@ import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.poteviohealth.cgp.sso.framework.common.enums.CommonStatusEnum;
|
|
|
import com.poteviohealth.cgp.sso.framework.common.enums.UserTypeEnum;
|
|
|
import com.poteviohealth.cgp.sso.framework.common.pojo.CommonResult;
|
|
|
import com.poteviohealth.cgp.sso.framework.common.util.http.HttpUtils;
|
|
|
@@ -15,11 +16,13 @@ import com.poteviohealth.cgp.sso.module.system.convert.oauth2.OAuth2OpenConvert;
|
|
|
import com.poteviohealth.cgp.sso.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
|
|
import com.poteviohealth.cgp.sso.module.system.dal.dataobject.oauth2.OAuth2ApproveDO;
|
|
|
import com.poteviohealth.cgp.sso.module.system.dal.dataobject.oauth2.OAuth2ClientDO;
|
|
|
+import com.poteviohealth.cgp.sso.module.system.dal.dataobject.userclient.UserClientDO;
|
|
|
import com.poteviohealth.cgp.sso.module.system.enums.oauth2.OAuth2GrantTypeEnum;
|
|
|
import com.poteviohealth.cgp.sso.module.system.service.oauth2.OAuth2ApproveService;
|
|
|
import com.poteviohealth.cgp.sso.module.system.service.oauth2.OAuth2ClientService;
|
|
|
import com.poteviohealth.cgp.sso.module.system.service.oauth2.OAuth2GrantService;
|
|
|
import com.poteviohealth.cgp.sso.module.system.service.oauth2.OAuth2TokenService;
|
|
|
+import com.poteviohealth.cgp.sso.module.system.service.userclient.UserClientService;
|
|
|
import com.poteviohealth.cgp.sso.module.system.util.oauth2.OAuth2Utils;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
@@ -38,10 +41,12 @@ import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import static com.poteviohealth.cgp.sso.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
|
|
|
+import static com.poteviohealth.cgp.sso.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static com.poteviohealth.cgp.sso.framework.common.exception.util.ServiceExceptionUtil.exception0;
|
|
|
import static com.poteviohealth.cgp.sso.framework.common.pojo.CommonResult.success;
|
|
|
import static com.poteviohealth.cgp.sso.framework.common.util.collection.CollectionUtils.convertList;
|
|
|
import static com.poteviohealth.cgp.sso.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
|
|
+import static com.poteviohealth.cgp.sso.module.system.enums.ErrorCodeConstants.OAUTH2_CLIENT_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
|
* 提供给外部应用调用为主
|
|
|
@@ -71,6 +76,7 @@ public class OAuth2OpenController {
|
|
|
@Resource
|
|
|
private OAuth2TokenService oauth2TokenService;
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 对应 Spring Security OAuth 的 TokenEndpoint 类的 postAccessToken 方法
|
|
|
* <p>
|
|
|
@@ -122,43 +128,41 @@ public class OAuth2OpenController {
|
|
|
//消息体解析
|
|
|
if (Objects.nonNull(bodyData) && !bodyData.isEmpty()) {
|
|
|
if (bodyData.containsKey("code")) {
|
|
|
- code =(String) bodyData.get("code");
|
|
|
+ code = (String) bodyData.get("code");
|
|
|
}
|
|
|
if (bodyData.containsKey("redirect_uri")) {
|
|
|
- redirectUri =(String) bodyData.get("redirect_uri");
|
|
|
+ redirectUri = (String) bodyData.get("redirect_uri");
|
|
|
}
|
|
|
if (bodyData.containsKey("state")) {
|
|
|
- state =(String) bodyData.get("state");
|
|
|
+ state = (String) bodyData.get("state");
|
|
|
}
|
|
|
if (bodyData.containsKey("username")) {
|
|
|
- username =(String) bodyData.get("username");
|
|
|
+ username = (String) bodyData.get("username");
|
|
|
}
|
|
|
if (bodyData.containsKey("password")) {
|
|
|
- password =(String) bodyData.get("password");
|
|
|
+ password = (String) bodyData.get("password");
|
|
|
}
|
|
|
if (bodyData.containsKey("scope")) {
|
|
|
- scope =(String) bodyData.get("scope");
|
|
|
+ scope = (String) bodyData.get("scope");
|
|
|
}
|
|
|
if (bodyData.containsKey("refresh_token")) {
|
|
|
- refreshToken =(String) bodyData.get("refresh_token");
|
|
|
+ refreshToken = (String) bodyData.get("refresh_token");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
// 2. 根据授权模式,获取访问令牌
|
|
|
OAuth2AccessTokenDO accessTokenDO;
|
|
|
switch (grantTypeEnum) {
|
|
|
case AUTHORIZATION_CODE:
|
|
|
- accessTokenDO = oauth2GrantService.grantAuthorizationCodeForAccessToken(client.getClientId(), code, redirectUri, state);
|
|
|
+ accessTokenDO = oauth2GrantService.grantAuthorizationCodeForAccessToken(client.getClientId(), code, redirectUri, state, client.getId());
|
|
|
break;
|
|
|
case PASSWORD:
|
|
|
- accessTokenDO = oauth2GrantService.grantPassword(username, password, client.getClientId(), scopes);
|
|
|
+ accessTokenDO = oauth2GrantService.grantPassword(username, password, client.getClientId(), scopes, client.getId());
|
|
|
break;
|
|
|
case CLIENT_CREDENTIALS:
|
|
|
- accessTokenDO = oauth2GrantService.grantClientCredentials(client.getClientId(), scopes);
|
|
|
+ accessTokenDO = oauth2GrantService.grantClientCredentials(client.getClientId(), scopes, client.getId());
|
|
|
break;
|
|
|
case REFRESH_TOKEN:
|
|
|
- accessTokenDO = oauth2GrantService.grantRefreshToken(refreshToken, client.getClientId());
|
|
|
+ accessTokenDO = oauth2GrantService.grantRefreshToken(refreshToken, client.getClientId(), client.getId());
|
|
|
break;
|
|
|
default:
|
|
|
throw new IllegalArgumentException("未知授权类型:" + grantType);
|
|
|
@@ -208,20 +212,24 @@ public class OAuth2OpenController {
|
|
|
OAuth2AccessTokenDO accessTokenDO;
|
|
|
switch (grantTypeEnum) {
|
|
|
case AUTHORIZATION_CODE:
|
|
|
- accessTokenDO = oauth2GrantService.grantAuthorizationCodeForAccessToken(client.getClientId(), code, redirectUri, state);
|
|
|
+ accessTokenDO = oauth2GrantService.grantAuthorizationCodeForAccessToken(client.getClientId(), code, redirectUri, state,client.getId());
|
|
|
break;
|
|
|
case PASSWORD:
|
|
|
- accessTokenDO = oauth2GrantService.grantPassword(username, password, client.getClientId(), scopes);
|
|
|
+ accessTokenDO = oauth2GrantService.grantPassword(username, password, client.getClientId(), scopes,client.getId());
|
|
|
break;
|
|
|
case CLIENT_CREDENTIALS:
|
|
|
- accessTokenDO = oauth2GrantService.grantClientCredentials(client.getClientId(), scopes);
|
|
|
+ accessTokenDO = oauth2GrantService.grantClientCredentials(client.getClientId(), scopes,client.getId());
|
|
|
break;
|
|
|
case REFRESH_TOKEN:
|
|
|
- accessTokenDO = oauth2GrantService.grantRefreshToken(refreshToken, client.getClientId());
|
|
|
+ accessTokenDO = oauth2GrantService.grantRefreshToken(refreshToken, client.getClientId(),client.getId());
|
|
|
break;
|
|
|
default:
|
|
|
throw new IllegalArgumentException("未知授权类型:" + grantType);
|
|
|
}
|
|
|
+ //
|
|
|
+ if (Objects.isNull(accessTokenDO)) {
|
|
|
+ throw new IllegalArgumentException("授权失败");
|
|
|
+ }
|
|
|
Assert.notNull(accessTokenDO, "访问令牌不能为空"); // 防御性检查
|
|
|
return OAuth2OpenConvert.INSTANCE.convert(accessTokenDO);
|
|
|
}
|