Retrofit — 实现Basic Authentication(2)
来源:互联网 发布:汉语方言地图集数据库 编辑:程序博客网 时间:2024/06/06 18:24
Retrofit — 用实现Basic Authentication(2)
这是在一系列 Retrofit 文章的第二篇, 它介绍了怎么在 Retrofit 里集成 base authentication。
Retrofit 系列文章概览
- 基础入门
- 用 Retrofit 实现 Basic Authentication
在基础入门里,我们创建了一个初始的 client 去执行 API/HTTP 请求。我们将上一篇文章里创建的 client 为基础,然后在它上面扩展功能来进行 basic authentication。你也可以再次阅读基础入门去获取更多关于创建 client 端的信息。
集成 Basic Authentication
我们先更新 ServiceGenerator
类然后创建一个方法在请求中添加 authentication header。下面的代码片段扩展了上一篇文章 的 ServiceGenerator
类。在 Retrofit 1.9的代码下面,我们添加了 Retrofit 2的示范代码。如果你使用了新版本的 Retrofit,可以直接跳到下面看第二块代码:)
Retrofit 1.9
public class ServiceGenerator { public static final String API_BASE_URL = "https://your.api-base.url"; private static RestAdapter.Builder builder = new RestAdapter.Builder() .setEndpoint(API_BASE_URL) .setClient(new OkClient(new OkHttpClient())); public static <S> S createService(Class<S> serviceClass) { return createService(serviceClass, null, null); } public static <S> S createService(Class<S> serviceClass, String username, String password) { if (username != null && password != null) { // concatenate username and password with colon for authentication String credentials = username + ":" + password; // create Base64 encodet string final String basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); builder.setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(RequestFacade request) { request.addHeader("Authorization", basic); request.addHeader("Acceppt", "application/json"); } }); } RestAdapter adapter = builder.build(); return adapter.create(serviceClass); }}
Retrofit 2
public class ServiceGenerator { public static final String API_BASE_URL = "https://your.api-base.url"; private static OkHttpClient httpClient = new OkHttpClient(); private static Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create()); public static <S> S createService(Class<S> serviceClass) { return createService(serviceClass, null, null); } public static <S> S createService(Class<S> serviceClass, String username, String password) { if (username != null && password != null) { String credentials = username + ":" + password; final String basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); httpClient.interceptors().clear(); httpClient.interceptors().add(new Interceptor() { @Override public Response intercept(Interceptor.Chain chain) throws IOException { Request original = chain.request(); Request.Builder requestBuilder = original.newBuilder() .header("Authorization", basic); .header("Accept", "applicaton/json"); .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }); } Retrofit retrofit = builder.client(httpClient).build(); return retrofit.create(serviceClass); }}
这个新的 createService
方法多了两个参数:username 和 password。username 也可以使用 email。创建 client 的基本过程与原来相同:使用 RestAdapter
(在Retrofit2里使用 Retrofit
)类创建一个 OkHttp client 去处理所有的 http 请求和响应。
不同的地方在于: 我们用 RequestInterceptor
(在Retrofit2里使用 Interceptor
)为每一个通过这个 OkHttp client 的 http 请求设置 Authorization header。但是这个只有在提供 username 和 password 的情况下才会做。如果你不提供 username 和 password ,它将会创建一个和上篇文章里一样的 client 。因此我们可以简化 ServiceGenerator
类的第一个方法。
对于认证部分,我们必须调整给定的 username/email 和 password 的格式。Basic authentication 要求把这两个值组成一个用冒号分隔的字符串,然后把这个新组成的字符串用 Base64 编码。
几乎所有的 webservice 和 API 验证 Authorization header,因此我们把编码后的内容放到它里面,如果你调用的webservice指定了另一个header field来获取内容,你只要调整 Authorization 为它指定的 field。
如果你想从服务器收到一个指定格式的响应,那么 Accept header 是必需的,在我们的例子里,我们想要JSON格式的响应,既然 Retrofit 已经使用 GSON 来把服务端的响应转成我们需要的类型。
使用
只需调用 ServiceGenerator
类的新的方法,就像在基础入门文章里那样。首先我们定义一个 LoginService
。
Retrofit 1.9
public interface LoginService { @POST("/login") User basicLogin();}
Retrofit 2
public interface LoginService { @POST("/login") Call<User> basicLogin();}
上面的接口只有一个方法: basicLogin
。它有一个 User
类型的返回值,并且它不需要额外的 query 或 path 参数。
现在你可以传递你的认证信息 ( username, password ) 来创建一个 HTTP client 。
Retrofit 1.9
LoginService loginService = ServiceGenerator.createService(LoginService.class, "user", "secretpassword");User user = loginService.basicLogin();
Retrofit 2
LoginService loginService = ServiceGenerator.createService(LoginService.class, "user", "secretpassword");Call<User> call = loginService.basicLogin(); User user = call.execute().body();
ServiceGenerator
里面的方法会自动创建含有认证信息 HTTP client,只要你调用 loginService
的 basicLogin
方法。你提供的认证信息会自动的发送给后端API。
下篇介绍
下一篇文章会介绍使用 Retrofit 实现一个 OAuth client。
参考资源
- 原文链接
- android source
- Retrofit — 实现Basic Authentication(2)
- Basic and Digest Access Authentication (rfc2617) 及HttpClient实现
- HTTP Basic Authentication认证的资源的C#实现
- Basic and Digest Access Authentication (rfc2617) 及HttpClient实现
- Implement basic HTTP authentication
- HTTP: Basic Authentication
- HTTP Basic Authentication认证
- Basic access authentication
- Spring Security Basic Authentication
- urllib2: Basic Authentication
- Tomcat Basic Authentication
- newlisp HTTP Basic Authentication
- HTTP: Basic Authentication
- python Basic Authentication
- HTTP Basic Authentication
- Tomcat配置Basic Authentication
- HttpClient Basic Authentication
- HTTP Basic Authentication认证
- Android Studio导入SlidingMenu类库的方法(其他类库应该也适用)
- kafka集群笔记
- OC-027.手动内存管理中的自动释放池@autorelease
- discuz接入七牛sdk
- Arduino 平台与C语言程序设计-week4-Arduino Programs-Lesson3
- Retrofit — 实现Basic Authentication(2)
- 中级篇——优先队列
- oracle数据导入导出
- POJ2456 二分
- storm集群部署
- VMware中Ubuntu为什么挂载不上U盘的解决方法
- Qt对话框中show和exec的区别
- Spring文件上传的问题
- 文章标题