2.使用
2020-12-21 本文已影响0人
盗生一
@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());
}