|
|
|
|
@ -21,17 +21,17 @@ import cn.iocoder.yudao.module.system.service.oauth2.OAuth2ClientService;
|
|
|
|
|
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2GrantService;
|
|
|
|
|
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
|
|
|
|
|
import cn.iocoder.yudao.module.system.util.oauth2.OAuth2Utils;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameters;
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import jakarta.annotation.security.PermitAll;
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import jakarta.annotation.security.PermitAll;
|
|
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
@ -94,6 +94,7 @@ public class OAuth2OpenController {
|
|
|
|
|
@Parameter(name = "scope", example = "user_info"),
|
|
|
|
|
@Parameter(name = "refresh_token", example = "123424233"),
|
|
|
|
|
})
|
|
|
|
|
@SuppressWarnings("EnhancedSwitchMigration")
|
|
|
|
|
public CommonResult<OAuth2OpenAccessTokenRespVO> postAccessToken(HttpServletRequest request,
|
|
|
|
|
@RequestParam("grant_type") String grantType,
|
|
|
|
|
@RequestParam(value = "code", required = false) String code, // 授权码模式
|
|
|
|
|
@ -119,15 +120,23 @@ public class OAuth2OpenController {
|
|
|
|
|
grantType, scopes, redirectUri);
|
|
|
|
|
|
|
|
|
|
// 2. 根据授权模式,获取访问令牌
|
|
|
|
|
OAuth2AccessTokenDO accessTokenDO = switch (grantTypeEnum) {
|
|
|
|
|
// TODO @xingyu:这里改了,可能会影响 jdk8 版本哈;
|
|
|
|
|
case AUTHORIZATION_CODE ->
|
|
|
|
|
oauth2GrantService.grantAuthorizationCodeForAccessToken(client.getClientId(), code, redirectUri, state);
|
|
|
|
|
case PASSWORD -> oauth2GrantService.grantPassword(username, password, client.getClientId(), scopes);
|
|
|
|
|
case CLIENT_CREDENTIALS -> oauth2GrantService.grantClientCredentials(client.getClientId(), scopes);
|
|
|
|
|
case REFRESH_TOKEN -> oauth2GrantService.grantRefreshToken(refreshToken, client.getClientId());
|
|
|
|
|
default -> throw new IllegalArgumentException("未知授权类型:" + grantType);
|
|
|
|
|
};
|
|
|
|
|
OAuth2AccessTokenDO accessTokenDO;
|
|
|
|
|
switch (grantTypeEnum) {
|
|
|
|
|
case AUTHORIZATION_CODE:
|
|
|
|
|
accessTokenDO = oauth2GrantService.grantAuthorizationCodeForAccessToken(client.getClientId(), code, redirectUri, state);
|
|
|
|
|
break;
|
|
|
|
|
case PASSWORD:
|
|
|
|
|
accessTokenDO = oauth2GrantService.grantPassword(username, password, client.getClientId(), scopes);
|
|
|
|
|
break;
|
|
|
|
|
case CLIENT_CREDENTIALS:
|
|
|
|
|
accessTokenDO = oauth2GrantService.grantClientCredentials(client.getClientId(), scopes);
|
|
|
|
|
break;
|
|
|
|
|
case REFRESH_TOKEN:
|
|
|
|
|
accessTokenDO = oauth2GrantService.grantRefreshToken(refreshToken, client.getClientId());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("未知授权类型:" + grantType);
|
|
|
|
|
}
|
|
|
|
|
Assert.notNull(accessTokenDO, "访问令牌不能为空"); // 防御性检查
|
|
|
|
|
return success(OAuth2OpenConvert.INSTANCE.convert(accessTokenDO));
|
|
|
|
|
}
|
|
|
|
|
|