NSURLCredential
NSURLCredential代表的是一個(gè)身份驗(yàn)證證書(shū)颈畸。URL Loading系統(tǒng)支持3種類型的證書(shū):password-based user credentials, certificate-based user credentials, and certificate-based server credentials垃瞧。
NSURLCredential適合大多數(shù)的認(rèn)證請(qǐng)求览闰,因?yàn)樗梢员硎居捎脩裘?密碼組合箫荡、客戶端證書(shū)及服務(wù)器信任創(chuàng)建的認(rèn)證信息卖陵。
認(rèn)證信息有三種持久化選項(xiàng):
- NSURLCredentialPersistenceNone :要求 URL 載入系統(tǒng) “在用完相應(yīng)的認(rèn)證信息后立刻丟棄”唐责。
- NSURLCredentialPersistenceForSession :要求 URL 載入系統(tǒng) “在應(yīng)用終止時(shí)瞒爬,丟棄相應(yīng)的 credential ”。
- NSURLCredentialPersistencePermanent :要求 URL 載入系統(tǒng) “將相應(yīng)的認(rèn)證信息存入鑰匙串(keychain)蜀变,以便其他應(yīng)用也能使用悄谐。
為了認(rèn)證,要?jiǎng)?chuàng)建一個(gè)NSURLCredential對(duì)象昏苏。在提供的authentication challenge的protection space上調(diào)用authenticationMethod 方法尊沸,可以獲取服務(wù)器的認(rèn)證方法。
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic) {
}else{
}
NSURLCredential支持的認(rèn)證方法有:
HTTP basic authentication (NSURLAuthenticationMethodHTTPBasic) 需要一個(gè)用戶名和密碼贤惯。使用
credentialWithUser:password:persistence:方法創(chuàng)建NSURLCredential對(duì)象洼专。HTTP digest authentication (NSURLAuthenticationMethodHTTPDigest),與basic authentication類似孵构,也需要一個(gè)用戶名和密碼屁商。使用
credentialWithUser:password:persistence:方法創(chuàng)建NSURLCredential對(duì)象。Client certificate authentication (NSURLAuthenticationMethodClientCertificate) 需要system identity和需要與server進(jìn)行身份驗(yàn)證的所有證書(shū)颈墅。使用 credentialWithIdentity:certificates:persistence:創(chuàng)建NSURLCredential對(duì)象蜡镶。
Server trust authentication (NSURLAuthenticationMethodServerTrust) 需要authentication challenge的protection space提供一個(gè)trust。使用credentialForTrust:來(lái)創(chuàng)建NSURLCredential對(duì)象恤筛。
(Basic官还、Digest與NTLM認(rèn)證都是基于用戶名/密碼的認(rèn)證。他們認(rèn)證的響應(yīng)邏輯是相同的毒坛。)
身份認(rèn)證原理
在代碼需要向認(rèn)證的服務(wù)器請(qǐng)求資源時(shí)望伦,服務(wù)器會(huì)使用http狀態(tài)碼401進(jìn)行響應(yīng),即訪問(wèn)被拒絕需要驗(yàn)證煎殷。NSURLConnection會(huì)接收到響應(yīng)并立刻使用認(rèn)證challenge的一份副本來(lái)發(fā)送一條willSendRequestForAuthenticationChallenge:委托消息屯伞。過(guò)程如下所示:
代碼
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
//以前的失敗次數(shù)
if ([challenge previousFailureCount] == 0) {
//身份認(rèn)證的類
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"賬號(hào)"
password:@"密碼"
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
}else{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
NSURLAuthenticationChallenge
NSURLAuthenticationChallenge encapsulates a challenge from a server requiring authentication from the client.
翻譯
NSURLAuthenticationChallenge封裝一個(gè)挑戰(zhàn)來(lái)自客戶機(jī)的服務(wù)器要求身份驗(yàn)證。
authentication challenge — An HTTP or HTTPS response indicating that the server requires authentication information from the client Foundation represents this with the NSURLAuthenticationChallenge class, and it also uses this infrastructure to support custom HTTPS server trust evaluation. An authentication challenge originates from a protection space.
翻譯
身份驗(yàn)證的挑戰(zhàn)——一個(gè)HTTP或HTTPS響應(yīng)表明服務(wù)器需要身份驗(yàn)證信息從客戶機(jī)與NSURLAuthenticationChallenge基金會(huì)代表這類,它也使用這種基礎(chǔ)設(shè)施,以支持自定義HTTPS服務(wù)器信任評(píng)估豪直。身份驗(yàn)證的挑戰(zhàn)源于保護(hù)空間劣摇。
NSURLSession
對(duì)于NSURLSession,代理對(duì)象要實(shí)現(xiàn)URLSession:task:didReceiveChallenge:completionHandler:方法弓乙。 (怎么請(qǐng)求去網(wǎng)上找 這里就不寫(xiě)了)
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
//以前的失敗次數(shù)
if ([challenge previousFailureCount] == 0) {
//身份認(rèn)證的類
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:@"賬號(hào)"
password:@"密碼"
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
}else{
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
AFNetworking
+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/plain", nil];
[mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[mgr setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession *session, NSURLSessionTask *task, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing *credential) {
if (challenge.previousFailureCount == 0) {
NSURLCredential *cre = [NSURLCredential credentialWithUser:@"賬號(hào)"
password:@"密碼" persistence:NSURLCredentialPersistenceForSession];
*credential = cre;
return NSURLSessionAuthChallengeUseCredential;
} else {
return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
}];
[mgr POST:url parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
本人新手呆鳥(niǎo)末融,忘各位老司機(jī)多多鞭策钧惧,使我快速成長(zhǎng)。謝謝觀看