【问题标题】:How to restrict Spartacus Authentication to certain customers?如何将 Spartacus 身份验证限制为某些客户?
【发布时间】:2021-02-22 08:18:01
【问题描述】:

我是 Spartacus 店面开发的新手,但在 Hybris/SAP Commerce 工作了几年,

设置 spartacus 我注意到在 AuthenticationService 中,客户可以从任何基础商店通过 REST API 登录。调用只需使用用户电子邮件/ID 和密码转到 hybris,然后通过商店的身份验证。

是否有可能仅将用户从一个 basestore 限制到特定的 spartacus 店面?或所述用户的组。只是不要将斯巴达克斯店面暴露给所有客户......

谢谢

【问题讨论】:

  • 嘿@arthurjs!我们有与您上述相同的要求。您是否以某种方式解决了问题?
  • @ArtemZur 我发布了我们所做工作的答案

标签: spartacus-storefront


【解决方案1】:

我们做了一个RequiredRoleMatchingFilter extends AbstractUrlMatchingFilter 检查BaseSite 级别上的自定义角色属性(UserGroupModel

像这样的

private static final String ROLE_PREFIX = "ROLE_";
private BaseSiteService baseSiteService;

    @Override
    protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
                                    final FilterChain filterChain) throws ServletException, IOException {
        final Authentication auth = getAuth();
        boolean isCustomer = false;
        if (hasRole(ROLE_CUSTOMERGROUP, auth) || hasRole(ROLE_CUSTOMERMANAGERGROUP, auth)) {
            isCustomer = true;
        }

        final Optional<BaseSiteModel> currentBaseSite = Optional.ofNullable(getBaseSiteService().getCurrentBaseSite());
        if (isCustomer && currentBaseSite.isPresent() && currentBaseSite.get().getRequiredRole() != null) {
            final UserGroupModel requiredRole = currentBaseSite.get().getRequiredRole();
            final String REQUIRED_ROLE = ROLE_PREFIX + requiredRole.getUid().toUpperCase();
            if (hasRole(REQUIRED_ROLE, auth)) {
                LOG.debug("Authorized as " + requiredRole.getUid());
            } else {
                // could not match the authorized role
                throw new AccessDeniedException("Access is denied");
            }
        }

        filterChain.doFilter(request, response);
    }

【讨论】:

  • 嘿@arthurjs!您是如何管理“/authorizationserver/oauth/token”不包含“basesiteid”的,您如何知道用户想要从哪个“basesite”授权?是否也自定义了授权服务?
  • 呃,当用户登录我们的 spartacus 商店/商店时,这可以得到解决: getBaseSiteService().getCurrentBaseSite() 所以这个角色过滤器在我们的例子中似乎工作正常。如果你想在授权服务器端阻止它(所以即使在调用登录 API 之前)我不知道你应该怎么做。
  • 我刚刚与我们的后端团队仔细检查了解决方案,它也适用于我们。并且不需要使用此类过滤器自定义“/authorizationserver/oauth/token”服务。谢谢!
  • 好吧,太棒了;)
猜你喜欢
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
相关资源
最近更新 更多