第三方系统访问乐创者服务的页面或接口前,需要先通过获取凭证的接口从乐创者服务获取用户访问凭证,以确保该用户有访问乐创者服务的权限。
1 接口规范
Request Method:POST
Content-Type:application/json;charset=UTF-8
Request URL:{http}://{ip}:{port}/{lczServer}/oapi/getUserAuthId组成说明
{http}      网络协议名称,支持http或https两种协议
{ip}        乐创者服务部署所在的 Internet 主机名,支持ip地址或域名
{port}      乐创者服务部署所在Web应用服务器的访问端口。如果是80端口,可缺省
{lczServer} 乐创者服务应用名,乐创者服务部署时的应用上下文根目录(Web Context-root),可根据业务需要调整为其他名称。当乐创者服务代码发布在“ROOT”目录下时,内容为空即可。2 请求体参数
请求体参数格式
{
    "userName": "用户登录名(帐号)",
    "virtualUser": true,
    "user": {
        "userId": "用户ID",
        "userAlias": "用户显示名称",
        "jobNumber": "用户工号",
        "orgName": "用户所属组织名称",
        "orgAlias": "用户所属组织显示名称",
        "orgNumber": "组织工号",
        "roleNames": ["用户角色"],
        "roleAlias": ["用户角色显示名称"],
        "dingId": "用户钉钉id",
        "weCorpId": "用户企业微信Id"
    },
    "tenantIdent": "租户标识",
    "clientName": "客户端名称"
}必选参数
| 参数名 | 类型 | 默认值 | 说明 | 
|---|---|---|---|
| userName | string | 用户登录名(帐号)。 | |
| virtualUser | boolean | false | 用于识别待访问乐创者服务的用户是否在乐创者平台上是否真实注册的用户。默认:false(真实注册的用户),true代表为虚拟用户。 | 
| user | json | 用户其他信息,可按照业务需求传参。 | |
| tenantIdent | string | 通过通用地址访问乐创者云服务时,可通过此参数指定企业标识。如果是特色地址方式访问,此参数可以不传。 | |
| clientName | string | thirdPartyPortal | 客户端类型名称,用于识别同一个用户通过不同设备访问乐创者服务时,不相互干扰,默认:thirdPartyPortal。 | 
用户登录名通过RSA加密(请使用 乐创者门户>>系统选项>>开发信息 中的公钥钥作为加密密钥)后,作为userName的值。
user对象参数说明
| 参数名 | 类型 | 说明 | 
|---|---|---|
| userId | string | 用户编号 | 
| userAlias | string | 用户显示名称,为空使用userName | 
| jobNumber | string | 用户所属组织名称 | 
| orgName | string | 用户所属组织显示名称 | 
| orgAlias | string | 用户编号 | 
| orgNumber | string | 用户所属组织编号 | 
| roleNames | string[] | 用户角色列表 | 
| roleAlias | string[] | 用户角色显示名称列表 | 
| dingId | string | 用户钉钉号 | 
| weCorpId | string | 用户企业微信号 | 
正确时返回格式
{
    "success": true,
    "content": 
        {
            "authId": "用户凭证",
            "expiresTime": "失效时间"
        }
}出错时返回格式
{
    "success":false,
    "code":"错误码",
    "msg":"错误信息"
}4 整合示例
@Service
public class CreateTokenService {
    private GhonHttp ghonHttp = new GhonHttp();
    public BaseResult<String> getToken(CreateTokenDTO createTokenDTO) {
        if(EmptyVeify.string(createTokenDTO.getPath())) {
            throw new RuntimeException("path is required.");
        }else if(createTokenDTO.getPath().endsWith("/")) {
            String psth = createTokenDTO.getPath();
            createTokenDTO.setPath(psth.substring(0, psth.length() - 1));
        }
        if(EmptyVeify.string(createTokenDTO.getSecretKey())) {
            throw new RuntimeException("SecretKey is required.");
        }
        if(EmptyVeify.string(createTokenDTO.getUserName())) {
            throw new RuntimeException("UserName is required.");
        }
        if(EmptyVeify.string(createTokenDTO.getUserMode())) {
            throw new RuntimeException("UserMode is required.");
        }
        String userName = createTokenDTO.getUserName();
        Charset charset = Charset.forName("UTF-8");
        try {
                userName = QEncodeUtil.base64Encode(RSAUtil.encrypt(userName.getBytes(charset), createTokenDTO.getSecretKey()));
            } catch (Exception e) {
                throw new RuntimeException("加密失败");
            }
            GetTokenDTO getTokenDTO = new GetTokenDTO();
            getTokenDTO.setClientName("testDemo");
            getTokenDTO.setTenantIdent(createTokenDTO.getTenantIdent());
            getTokenDTO.setUserName(userName);
            if("normal".equalsIgnoreCase(createTokenDTO.getUserMode())) {
                getTokenDTO.setVirtualUser(false);
            } else {
                getTokenDTO.setVirtualUser(true);
            }
            String urlPath = String.format("%s/oapi/getUserAuthId", createTokenDTO.getPath());
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json;charset=UTF-8");
            BaseResult<TokenDTO> result = null;
            Ghon ghon = ghonHttp.getGhon();
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                byte[] bytes = ghonHttp.call(urlPath, "POST", objectMapper.writeValueAsString(getTokenDTO).getBytes(charset),
                    headers, ghonHttp.getConnectTimeout(), ghonHttp.getReadTimeout(),false);
                GhonEle ghonEle = ghon.bytesOrJsonToGhonEle(bytes);
                result = ghon.ghonEleToBaseResult(ghonEle, TokenDTO.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e.getMessage(), e);
            } catch (GhonHttpException e) {
                throw new RuntimeException(String.format("http request error\nurl:%s\nhttp code:%s\nmessage:%s",
                    urlPath, e.getCode(), e.getMessage()), e);
            }
            if(!result.success) {
                return new BaseResult<String>(result.getCode(), result.getMsg());
            }
            return new BaseResult<String>(result.getContent().getAuthId());
        }
    }
}作者:柳杨  创建时间:2023-08-08 13:30
最后编辑:柳杨 更新时间:2025-10-11 17:30
最后编辑:柳杨 更新时间:2025-10-11 17:30
 扫码关注网盛数新公众号,获取更多帮助资料
扫码关注网盛数新公众号,获取更多帮助资料
 上一篇:无
上一篇:无 
 