🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
一、服务端配置文件:application.yml ``` config: #应用id appId: #应用密钥 appSecret: #服务地址 server-url: https://open.digitalcitizen.com.cn:7280/idauth ``` 二、接口代码 接口controller类:com.nlp.ctids.controller.ValidateIdentityController 接口方法:com.nlp.ctids.controller.ValidateIdentityController#apply ``` @RequestMapping("/{method}/{authMode}") @ResponseBody public String apply(@PathVariable String method, @PathVariable String authMode, @RequestBody String jsonData, HttpServletRequest request) { String accessToken = tokenService.getAccessToken(); Map<String, String> headers = new HashMap<>(); headers.put("Authorization", accessToken); headers.put("appId", config.appId); String post = HttpUtil.post(config.serverUrl + "/validateIdentity/" + method + "/" + authMode, jsonData, headers); return post; } ``` 三、获取token方法代码 方法类:com.nlp.ctids.service.TokenService ``` public CommonResponse getAccessToken(String appId, String appSecret) { CommonResponse commonResponse = tokenMap.get(appId + appSecret); try { if (commonResponse != null) { Map<String, Object> data = (Map<String, Object>) commonResponse.getData(); Long expireTime = (Long) data.get("expireTime"); if (expireTime == null || expireTime < System.currentTimeMillis()) { commonResponse = null; } } } catch (Exception e) { commonResponse = null; } if (commonResponse != null) { return commonResponse; } Map<String, String> data = new HashMap<>(); data.put("appId", appId); data.put("appSecret", appSecret); String response = HttpUtil.post(config.serverUrl + "/auth/getCode", JSON.toJSONString(data), null); commonResponse = JSON.parseObject(response, CommonResponse.class); if ("0".equals(commonResponse.getCode())) { tokenMap.put(appId + appSecret, commonResponse); } return commonResponse; } ```