我正在构建一个对api使用Hawk身份验证的应用。我发现的少数几本解释它的文章之一是https://futurestud.io/tutorials/retrofit-2-hawk-authentication-on-android。本文提到了一个类HawkAuthenticationInterceptor,该类在任何地方都找不到。还有其他添加鹰授权标头的方法吗?
[Tauntz on Reddit引用了以下解决方案
package br.com.xplore.xploreretrofit1.interceptors;
import com.wealdtech.hawk.HawkClient;
import com.wealdtech.hawk.HawkCredentials;
import java.io.IOException;
import java.net.URI;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by r028367 on 29/11/2017.
*/
public class HawkAuthenticationInterceptor implements Interceptor{
private HawkClient hawkClient;
public HawkAuthenticationInterceptor(HawkCredentials hawkCredentials) {
this.hawkClient = new HawkClient.Builder()
.credentials(hawkCredentials).build();
}
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
URI uri = original.url().uri();
String method = original.method();
String header = hawkClient
.generateAuthorizationHeader(uri, method, null
, null, null, null);
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", header)
.method(method, original.body());
return chain.proceed(requestBuilder.build());
}
}