AWS Cognito调用RespondToAuthChallenge报缺Session但文档标注参数可选
问题原因
- AWS官方文档标注
RespondToAuthChallenge接口的Session参数为可选,仅适用于自定义挑战首次触发等极少数特殊场景。你当前需要应答的SMS_MFA挑战属于常规认证流中的环节,Session为必填参数。 Session参数是Cognito服务在你上一步调用InitiateAuth完成用户名密码校验后,随SMS_MFA挑战标识一同返回的会话凭证,用来关联本次MFA验证对应的用户认证上下文,缺失后服务端无法识别你当前提交的验证码对应哪次认证请求,因此抛出必填参数缺失异常。
修复方案
- 保存上一步调用
InitiateAuth(或前一次RespondToAuthChallenge)接口返回结果中的Session字段值 - 构造
RespondToAuthChallengeRequest时将该值通过withSession方法传入即可,修改后的参考代码如下:
// 此处的previousAuthSession为上一步InitiateAuth返回的Session值 String previousAuthSession = initiateAuthResult.getSession(); Map<String, String> challengeResponses = new HashMap<String, String>(); challengeResponses.put("USERNAME", "username"); challengeResponses.put("SMS_MFA_CODE","code"); challengeResponses.put("SECRET_HASH", secretHash); RespondToAuthChallengeRequest respondToAuthChallengeRequest = new RespondToAuthChallengeRequest() .withChallengeName(ChallengeNameType.SMS_MFA) .withClientId("client_id") .withChallengeResponses(challengeResponses) // 新增传入session参数 .withSession(previousAuthSession); RespondToAuthChallengeResult respondToAuthChallengeResult = cognitoClient.respondToAuthChallenge(respondToAuthChallengeRequest);
内容的提问来源于stack exchange,提问作者user2160919
相关产品推荐
相关产品推荐

