【问题标题】:How and when is the token refreshed when using BMS Client, and is there a way to force the refresh?使用 BMS Client 时令牌如何以及何时刷新,有没有办法强制刷新?
【发布时间】:2016-03-09 09:19:57
【问题描述】:

我们正在使用:

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));
    }
}

【问题讨论】:

    标签: android ibm-cloud ibm-mobile-services


    【解决方案1】:

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

    【讨论】:

    • 是的,我们已经尝试将策略设置为 AuthorizationManager.getInstance().setAuthorizationPersistencePolicy(AuthorizationManager.PersistencePolicy.NEVER);这做同样的事情,因为 clearAuthorizationData() 只清除本地数据。
    • 你能检查一下你使用的是什么版本的SDK吗?
    • //依赖 IBM compile 'com.ibm.mobilefirstplatform.clientsdk.android:push:1.0.2' compile group: 'com.ibm.mobilefirstplatform.clientsdk.android', name: 'core' ,版本:'1.1.0',分机:'aar',传递:真
    【解决方案2】:

    回答您的问题:

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

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

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

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

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

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

    【讨论】:

    • 添加了我们的自定义 authlistener。我们很确定这可能是您的问题。我们遇到的是最近几天突然应用程序不会转到受保护的资源。查看日志时,我们从 onAuthFail 获取日志,其 json {"reson":"Login Failed"}。在调试时,我们可以确保它永远不会进入 onAuthChallengeReceived。然后突然之间它会收到一个挑战,一切都很好。你知道你们最近是否进行了任何可能导致这种行为的更新?
    • 能否请您提供您的 Bluemix 实例的 appID,以便我查看日志
    • 我们的生产应用程序 ID:9c877adb-c380-43ce-bb2d-ac9c3d32a04c 我们的开发人员实例应用程序 ID:905ce1db-d7b5-41ca-9b4e-a9e1a0695fed 我们在两个实例中都遇到了同样的问题。
    • @enno4859 请同时提供您的 appRoute。你能确认这是今天一直在发生的事情吗?你评论说你从来没有收到过挑战,然后你接受了,它就奏效了。那是没有清除应用程序中的所有数据吗?当没有收到挑战时,您是否正在监控服务状态页面?我想知道它是否只发生在间歇性中断期间
    • 我们的开发环境的appRoute:wenngarn.eu-gb.mybluemix.net 开发环境的app-id:9c877adb-c380-43ce-bb2d-ac9c3d32a04c 在我写这篇评论时,我遇到了同样的行为我没有受到挑战的地方。 03-14 08:57:15.184 18403-24175/? D/WenngarnAuthentication:身份验证失败。{“原因”:“登录失败”} 03-14 08:57:15.200 18403-18435/? E/mfpsdk.AuthorizationProcessManager: AuthorizationProcessManager.handleAuthorizationFailure in AuthorizationProcessManager.java:426 :: 授权过程失败
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2016-01-11
    • 2012-11-04
    • 2021-09-13
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多