@Override
public boolean register(String url, String deviceId, String username, String password) {
String method = "POST";
String uri = "/VIID/System/Register";
String requestUrl = url + uri;
// 发送第一次请求
// 注册对象
AuthInfo authInfo = new AuthInfo();
authInfo.setDeviceID(deviceId);
String beanToJson = Gat1400Utils.beanToJson(authInfo);
// 构造头信息
HttpHeaders headers = Gat1400Utils.getHeaders(deviceId);
log.info("自定义请求头:" + headers.keySet());
// 构造请求体
HttpEntity<String> reqEntity = new HttpEntity<>(beanToJson, headers);
ResponseEntity<Void> responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, reqEntity, Void.class);
log.info("headers:" + responseEntity.getHeaders());
int statusCode = responseEntity.getStatusCodeValue();
log.info("状态码:", statusCode);
if (statusCode != 401) {
log.error("第一次鉴权认证请求返回状态码非401 返回结果:{}", responseEntity.getBody());
throw new BusinessException("第一次鉴权认证请求返回状态码非401,返回状态码:" + statusCode);
}
// 发送第二次请求
// 组织参数,发起第二次请求
HttpHeaders httpHeaders = responseEntity.getHeaders();
log.info("headers:", responseEntity.getHeaders());
String httpHeadersFirst = httpHeaders.getFirst(Gat1400Const.WWW_AUTHENTICATE_NAME);
if (StringUtils.isEmpty(httpHeadersFirst)) {
throw new BusinessException("摘要认证出错 返回结果:" + responseEntity.getBody());
}
// 获取认证信息
HeaderInfo headerInfo = AuthUtils.parseAuth(httpHeadersFirst);
// 设置认证用户名 密码
headerInfo.setUsername(username);
headerInfo.setPassword(password);
// 设置请方式
headerInfo.setMethod(method);
// 设置uri
headerInfo.setUri(uri);
String authStr = AuthUtils.getDigestResponse(headerInfo);
// 获取头信息
// 组装认证信息
headers.add(Gat1400Const.REQUEST_AUTHORIZATION, authStr);
HttpEntity<String> req = new HttpEntity<>(beanToJson, headers);
ResponseEntity<ResponseStatusObject> responseEntity1 = restTemplate.exchange(requestUrl, HttpMethod.POST, req, ResponseStatusObject.class);
int statusCodeValue = responseEntity1.getStatusCodeValue();
log.info("状态码:", statusCodeValue);
log.info("headers:" + responseEntity.getHeaders());
log.info("结果", responseEntity.getBody());
if (statusCodeValue != 200) {
log.error("第二次鉴权认证请求返回状态码非200 返回结果:{}", responseEntity1.getBody());
throw new BusinessException("第二次鉴权认证请求返回状态码非200,返回状态码:" + statusCode);
}
// 放入本地map 保活调用
CurrentUploadInfo currentUploadInfo = new CurrentUploadInfo();
currentUploadInfo.setDeviceId(deviceId);
currentUploadInfo.setUrl(url);
currentUploadInfo.setPassword(password);
currentUploadInfo.setUsername(username);
authManager.addAuth(url, currentUploadInfo);
return true;
}
@Override
public void keepLive(CurrentUploadInfo currentUploadInfo) {
String requestUrl = currentUploadInfo.getUrl() + "/VIID/System/Keepalive";
// 认证对象
AuthInfo authInfo = new AuthInfo();
authInfo.setDeviceID(currentUploadInfo.getDeviceId());
String beanToJson = Gat1400Utils.beanToJson(authInfo);
// 构造头信息
HttpHeaders headers = Gat1400Utils.getHeaders(currentUploadInfo.getDeviceId());
log.info("自定义请求头", headers);
// 构造请求体
HttpEntity<String> reqEntity = new HttpEntity<>(beanToJson, headers);
ResponseEntity<ResponseStatusObject> responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, reqEntity, ResponseStatusObject.class);
int statusCodeValue = responseEntity.getStatusCodeValue();
if (statusCodeValue != 0) {
throw new BusinessException("保活失败");
}
// 扩展本地过期时间
authManager.refreshExpired(currentUploadInfo.getUrl());
}
@Override
public void unRegister(CurrentUploadInfo currentUploadInfo) {
String method = "POST";
String uri = "/VIID/System/UnRegister";
String requestUrl = currentUploadInfo.getUrl() + uri;
// 发送第一次请求
// 注册对象
AuthInfo authInfo = new AuthInfo();
authInfo.setDeviceID(currentUploadInfo.getDeviceId());
String beanToJson = Gat1400Utils.beanToJson(authInfo);
// 构造头信息
HttpHeaders headers = Gat1400Utils.getHeaders(currentUploadInfo.getDeviceId());
log.info("自定义请求头", headers);
// 构造请求体
HttpEntity<String> reqEntity = new HttpEntity<>(beanToJson, headers);
ResponseEntity<Void> responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, reqEntity, Void.class);
log.info("headers:" + responseEntity.getHeaders());
int statusCode = responseEntity.getStatusCodeValue();
log.info("状态码:", statusCode);
if (statusCode != 401) {
log.error("第一次鉴权认证请求返回状态码非401 返回结果:{}", responseEntity.getBody());
throw new BusinessException("第一次鉴权认证请求返回状态码非401,返回状态码:" + statusCode);
}
// 发送第二次请求
// 组织参数,发起第二次请求
HttpHeaders httpHeaders = responseEntity.getHeaders();
log.info("headers:", responseEntity.getHeaders());
String httpHeadersFirst = httpHeaders.getFirst(Gat1400Const.WWW_AUTHENTICATE_NAME);
if (StringUtils.isEmpty(httpHeadersFirst)) {
throw new BusinessException("摘要认证出错 返回结果:" + responseEntity.getBody());
}
// 获取认证信息
HeaderInfo headerInfo = AuthUtils.parseAuth(httpHeadersFirst);
// 设置认证用户名 密码
headerInfo.setUsername(currentUploadInfo.getUsername());
headerInfo.setPassword(currentUploadInfo.getPassword());
// 设置请方式
headerInfo.setMethod(method);
// 设置uri
headerInfo.setUri(uri);
String authStr = AuthUtils.getDigestResponse(headerInfo);
// 获取头信息
// 组装认证信息
headers.add(Gat1400Const.REQUEST_AUTHORIZATION, authStr);
HttpEntity<String> req = new HttpEntity<>(beanToJson, headers);
ResponseEntity<ResponseStatusObject> responseEntity1 = restTemplate.exchange(requestUrl, HttpMethod.POST, req, ResponseStatusObject.class);
int statusCodeValue = responseEntity1.getStatusCodeValue();
log.info("状态码:", statusCodeValue);
log.info("headers:" + responseEntity.getHeaders());
log.info("结果", responseEntity.getBody());
if (statusCodeValue != 200) {
log.error("第二次鉴权认证请求返回状态码非200 返回结果:{}", responseEntity1.getBody());
throw new BusinessException("第二次鉴权认证请求返回状态码非200,返回状态码:" + statusCode + ",注销失败!");
}
// 本地注销
authManager.remove(currentUploadInfo.getUrl());
}
2.使用
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 链接 flask web开发中,对链接的介绍比较少(可能后面还有,暂时没看到那)。使用链接的地方有很多,例如:导航...
- 本系列在简书首发,转载请注明出处,作者うちはイタチ上一篇文章你对Cocos2d-x有了一些了解,本篇主要来阐述你在...
- 代码见https://gitee.com/XiaoKunErGe/JianShu.git历史第四次提交 1.样式 ...