2

我们正在使用:

BMSClient.getInstance().registerAuthenticationListener("realm", new CustomAuthentication(this));

和:

AuthorizationManager.createInstance(this.getApplicationContext());
AuthorizationManager.getInstance().setAuthorizationPersistencePolicy(AuthorizationManager.PersistencePolicy.ALWAYS);

将授权数据存储在手机上。“授权数据将保存在本地存储”设置为始终。

上面的代码总是在启动时在我们的启动画面上运行,所以它总是在应用程序重新启动时运行。

我们遇到的问题是,在我们怀疑令牌过期的一段时间(数小时或数天)之后,我们会收到HTTP 307形式的响应。即使在重新启动应用程序后,我们仍然会收到对我们请求的响应。解决它的唯一方法是从设置进入应用程序并清除所有数据。

以下问题将帮助我们继续进行测试和可能的解决方案:

  1. 令牌在 BMSClient 中缓存了多长时间?(测试目的)
  2. AuthorizationManager 可以以任何方式帮助我们强制获取新的令牌吗?
  3. 他们是否正在开发注销功能?

我们的自定义监听器:

public class CustomAuth implements AuthenticationListener {

    private Context activityContext;

    public CustomAuth(Context activityContext) {
        this.activityContext = activityContext;
    }

    @Override
    public void onAuthenticationChallengeReceived(AuthenticationContext authContext, JSONObject challenge, Context context) {
        //1. read the challenge JSONObject
        //2. handle the challenge (use the context for handling UI based operations)
        //3. return response using the AuthenticationContext authContext
        SharedPreferences preferences = activityContext.getSharedPreferences("UserPreference", Context.MODE_PRIVATE);
        String email = preferences.getString("email", "");
        if(email.equals("")) {
            email = "unidentified-user@error.com";
        }
        JSONObject jsonEmail = new JSONObject();
        try {
            jsonEmail.put("email", email);
        } catch (JSONException e) {
            authContext.submitAuthenticationChallengeAnswer(null);
        }
        authContext.submitAuthenticationChallengeAnswer(jsonEmail);

    }

    @Override
    public void onAuthenticationSuccess(Context context, JSONObject info) {
        //additional operations in case of authentication success
        Log.d("Authentication", "Auth success: " + String.valueOf(info));
    }

    @Override
    public void onAuthenticationFailure(Context context, JSONObject info) {
        //additional operations in case of authentication failure
        Log.d("Authentication", "Auth failure ." + String.valueOf(info));
    }
}
4

2 回答 2

1

当收到 307 并重新发送请求时,您是否尝试过使用 AuthorizationManager.clearAuthorizationData() API?

于 2016-03-15T14:10:26.693 回答
0

要回答您的问题:

1) 授权令牌将被无限期缓存。令牌会在 60 分钟后过期,但会一直缓存,直到获得新令牌。

最佳做法是在原始令牌过期后获取新令牌。这可以通过在前一个令牌过期后运行新的授权质询来完成。

2) 一旦前一个令牌过期,您始终可以使用 AuthorizationManager 通过访问受保护的资源、使用 getAuthorizationHeader 等来获取新令牌。

3) 目前无法使用 AuthorizationManager 退出 MCA。我将与开发团队讨论未来的计划。

关于我在您的问题中看到的主要问题。我希望您遇到此问题,因为您正试图对授权服务使用过期的令牌。即使令牌仍缓存在设备上,它也会在创建后一小时过期。一旦令牌过期,我将尝试对 MCA 服务运行新的授权挑战。

如果您想提供也可以帮助我进一步调查的代码。

于 2016-03-10T15:16:49.613 回答