SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题

如果用户登录名或密码包含符号 +,它将被替换为空格,因为 Content-Type 等于 application/x-www-form-urlencoded。

下面是一个例子:

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第1张图片

https://:9002/occ/v2/electronics-spa/forgottenpasswordtokens?lang=en&curr=USD

我输入了包含 + 号的邮件地址后,点击 Submit,发送一个 HTTP POST 请求到后台,响应为 202:
SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第2张图片

从 Form Data 区域,我们可以发现,这个 userId 显示为 a @sap.com, 邮箱地址里包含了空格符号:

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第3张图片

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第4张图片

找到 Reset Password Component 的 selector:cx-forgot-password

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第5张图片

Component 名称为:ForgotPasswordComponent

通过 Service 实现:
SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第6张图片

依赖于 UserPasswordFacade:
SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第7张图片

调用 userProfileConnector 完成:
SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第8张图片

connector 调用 userProfileAdapter:

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第9张图片

这是一个 abstract class,我们调用 OCCUserProfileAdapter 实现:

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第10张图片

OccUserProfileAdapter 最终调用 HTTP Client 的 post 操作:

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第11张图片

我们在后台打印 Angular HTTP client 发送过来的请求,发现确实 + 号被转换成了空格:

SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题_第12张图片

https://github.com/angular/an...

这个文件首先使用浏览器的 encodeURIComponent() 方法,但随后恢复了几个字符的编码,包括 + 号。

一种比较合理的设计是:

感谢@meeque 参与讨论这个话题。 Angular 确实不支持干净的编码/解码,这就是为什么我们没有为各种字符(@:$,;+=?/)获得正确的编码。 我浏览了与此主题相关的各种资源,但我不确定为什么会跳过这些字符。

  • 实现一个仅使用来自浏览器的 encodeURIComponent 和 decodeURIComponent 的 HttpParameterCodec
  • 在我们使用 HttpParameters 的任何地方都使用编码器,但至少在创建用户和密码方面
  • 允许通过 DI 或通过在配置模块中配置类来覆盖此行为

可以先在前台进行 encode:

const pass = encodeURIComponent(password.value);
this.auth.authorize(userId.value.toLowerCase(), pass);

然后在 Hybris 后台 decode:

if (credential instanceof String) {
    // if credential is a string use URLDecode class. 
    credential = URLDecoder.decode(String.valueOf(credential), StandardCharsets.UTF_8);
    if (!user.checkPassword((String) credential)) {
        throw new BadCredentialsException(this.messages.getMessage("CoreAuthenticationProvider.badCredentials", "Bad credentials"));
    }
} else {
    if (!(credential instanceof LoginToken)) {
        throw new BadCredentialsException(this.messages.getMessage("CoreAuthenticationProvider.badCredentials", "Bad credentials"));
    }
   if (!user.checkPassword((LoginToken) credential)) {
       throw new BadCredentialsException(this.messages.getMessage("CoreAuthenticationProvider.badCredentials", "Bad credentials"));
     }
}

后续修复进度,请跟踪这个 Github issue.

你可能感兴趣的:(SAP 电商云 Spartacus UI userID 即邮件地址中的加号问题)