当 adpater 完成其在 Alamofire 中的工作时,请求不会继续进行

问题描述 投票:0回答:1

我在使用 Alamofire 的 http 请求时遇到问题 我的服务器使用 JWT 来验证用户身份。 因此,当 JWT 不存在时,请求必须创建一个。 所以我在 Alamofire 的 Adapter 中完成这项工作

步骤1。检查令牌是否在内存中 步骤 2。如果不存在,则向服务器请求新令牌(新请求) 步骤 3。获得令牌并调用completion(.success(带有令牌的请求))

问题是,虽然step3完成得很好,但是请求不起作用(我检查了Proxyman中没有发出请求)

我不明白出了什么问题..请帮助我

下面的代码是我的适配器


lazy private(set) var tokenAdapter = Adapter { [weak self] request, session, completion in
    
    var modifiedRequest = request
    
    ✋✋✋ Step1
    if let accessToken = self?.credential.accessToken {
        
        let bearerToken = "Bearer \(accessToken)"
        
        modifiedRequest
            .addValue("Authorization", forHTTPHeaderField: bearerToken)
        
        completion(.success(modifiedRequest))
        
    } else {
        
        // 토큰을 발행한 적이 없는 경우
        guard let deviceId = self?.credential.devideIdentifier,
              let requestBox = self?.issueToken(deviceIdentity: DeviceIdentityDTO(imei: deviceId))
        else {
            return completion(.failure(RequestGenerationError.crendentialIsNotAvailable))
        }
    
        do {
            let issueTokenRequest = try requestBox.endPoint.toRequest()
                
            ✋✋✋ Step2
            session
                .request(issueTokenRequest)
                .responseDecodable(of: TokenResponse.self) { response in
                    
                    if let error = response.error {
                        
                        return completion(.failure(error))
                    }
                    
                    if let responseData = try? response.result.get().data {
                        
                        let accessToken = responseData.accessToken
                        let refreshToken = responseData.refreshToken
                        
                        self?.credential.renewalTokens(
                            accessToken: accessToken,
                            refreshToken: refreshToken
                        )
                        
                        let bearerToken = "Bearer \(accessToken)"
                        
                        modifiedRequest
                            .addValue("Authorization", forHTTPHeaderField: bearerToken)
                        
                        
                        ✋✋✋ Step3
                        return completion(.success(modifiedRequest))
                    }
                    
                    return completion(.failure(ResponseError.dataIsNotFound))
                }
            
        } catch {
            completion(.failure(error))
        }
    }
}

调用Adapter的completion闭包后确认请求没有启动。

ios swift alamofire
1个回答
0
投票

问题似乎在于如何将授权标头添加到请求中。而不是modifiedRequest.addValue("Authorization", forHTTPHeaderField: bearerToken),应该是modifiedRequest.addValue(bearerToken, forHTTPHeaderField: "Authorization")。这是更正后的适配器代码:

这是您应该尝试的代码,如果这对您有用的话

lazy private(set) var tokenAdapter = Adapter { [weak self] request, session, completion in
    
    var modifiedRequest = request
    
    // Step 1
    if let accessToken = self?.credential.accessToken {
        
        let bearerToken = "Bearer \(accessToken)"
        
        modifiedRequest.addValue(bearerToken, forHTTPHeaderField: "Authorization")
        
        completion(.success(modifiedRequest))
        
    } else {
        
        // No token issued previously
        guard let deviceId = self?.credential.deviceIdentifier,
              let requestBox = self?.issueToken(deviceIdentity: DeviceIdentityDTO(imei: deviceId))
        else {
            return completion(.failure(RequestGenerationError.credentialIsNotAvailable))
        }
    
        do {
            let issueTokenRequest = try requestBox.endPoint.toRequest()
                
            // Step 2
            session
                .request(issueTokenRequest)
                .responseDecodable(of: TokenResponse.self) { response in
                    
                    if let error = response.error {
                        return completion(.failure(error))
                    }
                    
                    if let responseData = try? response.result.get().data {
                        
                        let accessToken = responseData.accessToken
                        let refreshToken = responseData.refreshToken
                        
                        self?.credential.renewalTokens(
                            accessToken: accessToken,
                            refreshToken: refreshToken
                        )
                        
                        let bearerToken = "Bearer \(accessToken)"
                        
                        modifiedRequest.addValue(bearerToken, forHTTPHeaderField: "Authorization")
                        
                        // Step 3
                        return completion(.success(modifiedRequest))
                    }
                    
                    return completion(.failure(ResponseError.dataIsNotFound))
                }
            
        } catch {
            completion(.failure(error))
        }
    }
}

主要更正位于modifiedRequest.addValue(bearerToken, forHTTPHeaderField: "Authorization")行中。这应该可以解决调用适配器完成关闭后请求无法继续的问题。

© www.soinside.com 2019 - 2024. All rights reserved.