feat(auth): 增加内部签发小程序 token,供 Go 过渡网关换票
空密钥时拒绝签发,避免未配置环境对外开接口。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.oneone.auth.controller.app;
|
||||
|
||||
import com.oneone.auth.service.LoginService;
|
||||
import com.oneone.common.base.LocalToken;
|
||||
import com.oneone.common.result.Result;
|
||||
import io.swagger.v3.oas.annotations.Hidden;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Hidden
|
||||
@RestController
|
||||
@RequestMapping("/oauth/internal")
|
||||
@RequiredArgsConstructor
|
||||
public class InternalSsoController {
|
||||
private final LoginService loginService;
|
||||
|
||||
@Value("${auth.internal-issue-key:}")
|
||||
private String internalIssueKey;
|
||||
|
||||
@PostMapping("/issue-mini-app")
|
||||
public Result<LocalToken> issueMiniApp(
|
||||
@RequestHeader(value = "X-Internal-Key", required = false) String key,
|
||||
@RequestBody IssueMiniAppParam body
|
||||
) {
|
||||
if (!StringUtils.hasText(internalIssueKey) || !internalIssueKey.equals(key)) {
|
||||
return Result.failed("unauthorized");
|
||||
}
|
||||
if (body == null || !StringUtils.hasText(body.getOpenid())) {
|
||||
return Result.failed("openid required");
|
||||
}
|
||||
return Result.success(loginService.issueMiniAppByOpenid(body.getOpenid(), body.getUnionId(), body.getPhone()));
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class IssueMiniAppParam {
|
||||
private String openid;
|
||||
private String unionId;
|
||||
private String phone;
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,8 @@ public interface LoginService {
|
||||
|
||||
LocalToken miniApp(String code,String miniAppParamStr);
|
||||
|
||||
LocalToken issueMiniAppByOpenid(String openid, String unionId, String phone);
|
||||
|
||||
LocalToken officialAccount(String officialAccountParamStr);
|
||||
|
||||
LocalToken appleJwt(String token);
|
||||
|
||||
@@ -272,6 +272,27 @@ public class LoginServiceImpl implements LoginService {
|
||||
return token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalToken issueMiniAppByOpenid(String openid, String unionId, String phone) {
|
||||
if (!StringUtils.hasText(openid)) {
|
||||
throw new BusinessException("登录失败,请稍后再试");
|
||||
}
|
||||
AccountInfo accountInfo = accountInfoService.getAccountInfo(AccountTypeEnum.MINI_APP.getCode(), openid);
|
||||
if (accountInfo == null) {
|
||||
RegisterParam registerParam = new RegisterParam(AccountTypeEnum.MINI_APP.getCode(), openid);
|
||||
registerParam.setPhone(phone);
|
||||
registerParam.setUnionId(unionId);
|
||||
accountInfo = accountInfoService.register(registerParam);
|
||||
}
|
||||
if (accountInfo.getStatus() == 0) {
|
||||
log.error("微信登录,账号被封禁,账号:{}", openid);
|
||||
throw new BusinessException(ErrorCodeConstants.USER_ACCOUNT_LOCKED);
|
||||
}
|
||||
LocalToken token = TokenUtil.generateToken(tokenValid, AccountTypeEnum.MINI_APP.getCode(), openid, accountInfo.getPlatformUserId(), GrantTypeEnum.MINI_APP.getCode());
|
||||
appRedisTokenStore.storeAccessToken(token);
|
||||
return token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalToken officialAccount(String officialAccountParamStr) {
|
||||
OfficialAccountParam officialAccountParam = JsonUtils.strToClass(officialAccountParamStr, OfficialAccountParam.class);
|
||||
|
||||
@@ -19,4 +19,8 @@ springdoc:
|
||||
knife4j:
|
||||
enable: true # 2.2 是否开启 Swagger 文档的 Knife4j UI 界面
|
||||
setting:
|
||||
language: zh_cn
|
||||
language: zh_cn
|
||||
|
||||
# ECR-049:Go 内网换小程序 token。须与 Go JAVA_INTERNAL_KEY 一致;空则拒绝签发。
|
||||
auth:
|
||||
internal-issue-key: ${AUTH_INTERNAL_ISSUE_KEY:}
|
||||
Reference in New Issue
Block a user