2.使用

   @Override
    public boolean register(String url, String deviceId, String username, String password) {

        String method = "POST";
        String uri = "/VIID/System/Register";
        String requestUrl = url + uri;
        // 發(fā)送第一次請(qǐng)求
//        注冊(cè)對(duì)象
        AuthInfo authInfo = new AuthInfo();
        authInfo.setDeviceID(deviceId);
        String beanToJson = Gat1400Utils.beanToJson(authInfo);
        // 構(gòu)造頭信息
        HttpHeaders headers = Gat1400Utils.getHeaders(deviceId);
        log.info("自定義請(qǐng)求頭:" + headers.keySet());
        // 構(gòu)造請(qǐng)求體
        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("狀態(tài)碼:", statusCode);
        if (statusCode != 401) {
            log.error("第一次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非401 返回結(jié)果:{}", responseEntity.getBody());
            throw new BusinessException("第一次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非401,返回狀態(tài)碼:" + statusCode);
        }
        // 發(fā)送第二次請(qǐng)求
        // 組織參數(shù),發(fā)起第二次請(qǐng)求
        HttpHeaders httpHeaders = responseEntity.getHeaders();
        log.info("headers:", responseEntity.getHeaders());
        String httpHeadersFirst = httpHeaders.getFirst(Gat1400Const.WWW_AUTHENTICATE_NAME);
        if (StringUtils.isEmpty(httpHeadersFirst)) {
            throw new BusinessException("摘要認(rèn)證出錯(cuò) 返回結(jié)果:" + responseEntity.getBody());
        }

        // 獲取認(rèn)證信息
        HeaderInfo headerInfo = AuthUtils.parseAuth(httpHeadersFirst);

        // 設(shè)置認(rèn)證用戶名 密碼
        headerInfo.setUsername(username);
        headerInfo.setPassword(password);
        // 設(shè)置請(qǐng)方式
        headerInfo.setMethod(method);
        // 設(shè)置uri
        headerInfo.setUri(uri);
        String authStr = AuthUtils.getDigestResponse(headerInfo);

        // 獲取頭信息
        // 組裝認(rèn)證信息
        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("狀態(tài)碼:", statusCodeValue);
        log.info("headers:" + responseEntity.getHeaders());
        log.info("結(jié)果", responseEntity.getBody());
        if (statusCodeValue != 200) {
            log.error("第二次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非200 返回結(jié)果:{}", responseEntity1.getBody());
            throw new BusinessException("第二次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非200,返回狀態(tài)碼:" + statusCode);
        }
        // 放入本地map ?;钫{(diào)用
        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";
        // 認(rèn)證對(duì)象
        AuthInfo authInfo = new AuthInfo();
        authInfo.setDeviceID(currentUploadInfo.getDeviceId());
        String beanToJson = Gat1400Utils.beanToJson(authInfo);
        // 構(gòu)造頭信息
        HttpHeaders headers = Gat1400Utils.getHeaders(currentUploadInfo.getDeviceId());
        log.info("自定義請(qǐng)求頭", headers);
        // 構(gòu)造請(qǐng)求體
        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("?;钍?);
        }
        // 擴(kuò)展本地過(guò)期時(shí)間
        authManager.refreshExpired(currentUploadInfo.getUrl());
    }


    @Override
    public void unRegister(CurrentUploadInfo currentUploadInfo) {

        String method = "POST";
        String uri = "/VIID/System/UnRegister";
        String requestUrl = currentUploadInfo.getUrl() + uri;
        // 發(fā)送第一次請(qǐng)求
//        注冊(cè)對(duì)象
        AuthInfo authInfo = new AuthInfo();
        authInfo.setDeviceID(currentUploadInfo.getDeviceId());
        String beanToJson = Gat1400Utils.beanToJson(authInfo);
        // 構(gòu)造頭信息
        HttpHeaders headers = Gat1400Utils.getHeaders(currentUploadInfo.getDeviceId());
        log.info("自定義請(qǐng)求頭", headers);
        // 構(gòu)造請(qǐng)求體
        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("狀態(tài)碼:", statusCode);
        if (statusCode != 401) {
            log.error("第一次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非401 返回結(jié)果:{}", responseEntity.getBody());
            throw new BusinessException("第一次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非401,返回狀態(tài)碼:" + statusCode);
        }
        // 發(fā)送第二次請(qǐng)求
        // 組織參數(shù),發(fā)起第二次請(qǐng)求
        HttpHeaders httpHeaders = responseEntity.getHeaders();
        log.info("headers:", responseEntity.getHeaders());
        String httpHeadersFirst = httpHeaders.getFirst(Gat1400Const.WWW_AUTHENTICATE_NAME);
        if (StringUtils.isEmpty(httpHeadersFirst)) {
            throw new BusinessException("摘要認(rèn)證出錯(cuò) 返回結(jié)果:" + responseEntity.getBody());
        }

        // 獲取認(rèn)證信息
        HeaderInfo headerInfo = AuthUtils.parseAuth(httpHeadersFirst);

        // 設(shè)置認(rèn)證用戶名 密碼
        headerInfo.setUsername(currentUploadInfo.getUsername());
        headerInfo.setPassword(currentUploadInfo.getPassword());
        // 設(shè)置請(qǐng)方式
        headerInfo.setMethod(method);
        // 設(shè)置uri
        headerInfo.setUri(uri);
        String authStr = AuthUtils.getDigestResponse(headerInfo);

        // 獲取頭信息
        // 組裝認(rèn)證信息
        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("狀態(tài)碼:", statusCodeValue);
        log.info("headers:" + responseEntity.getHeaders());
        log.info("結(jié)果", responseEntity.getBody());
        if (statusCodeValue != 200) {
            log.error("第二次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非200 返回結(jié)果:{}", responseEntity1.getBody());
            throw new BusinessException("第二次鑒權(quán)認(rèn)證請(qǐng)求返回狀態(tài)碼非200,返回狀態(tài)碼:" + statusCode + ",注銷(xiāo)失??!");
        }

        // 本地注銷(xiāo)
        authManager.remove(currentUploadInfo.getUrl());
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容