简体   繁体   English

如何在Android上使用Volley进行基本身份验证?

[英]How does one use Basic Authentication with Volley on Android?

I'm looking through examples and code but I don't see anything implemented. 我正在查看示例和代码,但我没有看到任何实现。 Is this possible at this stage? 这个阶段有可能吗?

For those who don't want to use Spring for Android just for that, here's how to do it. 对于那些不想仅仅为了Android而使用Spring for Android的人来说,这是如何做到的。

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> params = new HashMap<String, String>();
    String creds = String.format("%s:%s","USERNAME","PASSWORD");
    String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
    params.put("Authorization", auth);
    return params;
}

Note that you may have to use Base64.NO_WRAP instead of Base64.DEFAULT for this to work. 请注意, Base64.DEFAULT实现此目的,您可能必须使用Base64.NO_WRAP而不是Base64.DEFAULT As pointed in the comments. 正如评论中指出的那样。

API 8+ API 8+

Yes it's possible. 是的,这是可能的。 You need to override Request.getHeaders(). 您需要覆盖Request.getHeaders()。 I'm lazy and I used HttpHeaders and HttpAuthentication from Spring for Android but you can just build the auth header and return it from the method. 我很懒,我使用Spring for Android的HttpHeaders和HttpAuthentication,但你可以构建auth头并从方法中返回它。 From getHeaders() you can return the auth header for basic auth. 从getHeaders(),您可以返回基本身份验证的auth标头。 This is a sample request with basic auth. 这是一个带有基本身份验证的示例请求。

public class GetUser extends Request<User> {

    private static final String TAG = GetUser.class.getName();

    private Response.Listener<User> mListener;
    private ObjectMapper mMapper = new ObjectMapper();

    public GetUser(Response.ErrorListener errorListener, Response.Listener<User> listener){
        super(Method.GET, PoisUtils.BASE_URL + "/users", errorListener);

        mListener = listener;
    }

    @Override
    protected Response<User> parseNetworkResponse(NetworkResponse response) {
        String jsonString = new String(response.data);
        try {
            User result = mMapper.readValue(jsonString, User.class);
            return Response.success(result, getCacheEntry());
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }
        return null;
    }

    @Override
    protected void deliverResponse(User response) {
        mListener.onResponse(response);
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return AuthUtils.buildAuthHeaders().toSingleValueMap();
    }
}

And here is how I build the auth headers 以下是我如何构建auth标头

public static HttpHeaders buildAuthHeaders(){

    if(UserUtils.isUserLogged()){
        HttpHeaders requestHeaders = new HttpHeaders();
        User user = PoisApplication.get().getUser();

        HttpAuthentication auth = new HttpBasicAuthentication(
                user.getUsername(), user.getPassword());
        requestHeaders.setAuthorization(auth);

        return requestHeaders;
    }
    return null;
}

For a proxy authorization (like squid) use this header : 对于代理授权(如squid),请使用此标头:

String credentials = proxyUsername + ":" + proxyPassword;
String auth = "Basic "  + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);                
headers.put("Proxy-Authorization", auth);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM