成功登录用户后,如何根据用户的上一页将其重定向到其他页面?

安德鲁·布克萨(Andrew Buxa):

我将用户重定向到首页,并将“默认目标网址”设置为“ /”。但是,如果用户登录到产品页面(/ p /)或搜索页面(/ search),则需要重定向用户。我该怎么做呢?我对Spring Security和重定向还不是很了解。

我尝试在AuthenticationSuccessHandler的onAuthenticationSuccess()方法内拦截该请求,并检查该URL是否包含产品页面或搜索页面URL。

在AuthenticationSuccessHandler中:

if (!response.isCommitted()) {
   super.onAuthenticationSuccess(request,response,authentication);
}

在spring-security-config.xml中:

<bean id="authenticationSuccessHandler" class ="com.storefront.AuthenticationSuccessHandler" scope="tenant">
<property name="rememberMeCookieStrategy" ref="rememberMeCookieStrategy" />
<property name="customerFacade" ref="customerFacade" />
<property name="sCustomerFacade" ref="sCustomerFacade" />
<property name="sProductFacade" ref="sProductFacade" />
<property name="defaultTargetUrl" value="/" />
<property name="useReferer" value="true" />
<property name="requestCache" value="httpSessionRequestCache" />

预期结果将是:

  1. 当用户登录到产品页面时,他们将返回到其所在的产品页面。
  2. 当用户登录搜索页面时,他们将返回到其所在的搜索页面。
  3. 如果用户未登录产品或搜索页面而登录,则他们将被重定向到主页。
Hybris帮助:

Hybris OOTB(我指的是V6.7)具有一项功能,您可以在其中列出要重定向到“默认目标网址”的URL。这里的想法是用相反的逻辑有另一个列表(或替换现有的列表),该逻辑仅允许给定的URL并将所有其他URL重定向到默认目标URL。

在开箱即用的,你可以看到listRedirectUrlsForceDefaultTargetspring-security-config.xml,在那里可以定义要重定向到默认的目标URL列表。像下面。

<alias name="defaultLoginAuthenticationSuccessHandler" alias="loginAuthenticationSuccessHandler"/>
<bean id="defaultLoginAuthenticationSuccessHandler" class="de.hybris.platform.acceleratorstorefrontcommons.security.StorefrontAuthenticationSuccessHandler" >
    <property name="customerFacade" ref="customerFacade" />
    <property name="defaultTargetUrl" value="#{'responsive' == '${commerceservices.default.desktop.ui.experience}' ? '/' : '/my-account'}"/>
    <property name="useReferer" value="true"/>
    <property name="requestCache" ref="httpSessionRequestCache" />
    <property name="uiExperienceService" ref="uiExperienceService"/>
    <property name="cartFacade" ref="cartFacade"/>
    <property name="customerConsentDataStrategy" ref="customerConsentDataStrategy"/>
    <property name="cartRestorationStrategy" ref="cartRestorationStrategy"/>
    <property name="forceDefaultTargetForUiExperienceLevel">
        <map key-type="de.hybris.platform.commerceservices.enums.UiExperienceLevel" value-type="java.lang.Boolean">
            <entry key="DESKTOP" value="false"/>
            <entry key="MOBILE" value="false"/>
        </map>
    </property>
    <property name="bruteForceAttackCounter" ref="bruteForceAttackCounter" />
    <property name="restrictedPages">
        <list>
            <value>/login</value>
        </list>
    </property>
    <property name="listRedirectUrlsForceDefaultTarget">
        <list>/example/redirect/todefault</list>
    </property>
</bean>

StorefrontAuthenticationSuccessHandler

@Override
    public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response,
                                        final Authentication authentication) throws IOException, ServletException
    {
        //...

        //if redirected from some specific url, need to remove the cachedRequest to force use defaultTargetUrl
        final RequestCache requestCache = new HttpSessionRequestCache();
        final SavedRequest savedRequest = requestCache.getRequest(request, response);

        if (savedRequest != null)
        {
            for (final String redirectUrlForceDefaultTarget : getListRedirectUrlsForceDefaultTarget())
            {
                if (savedRequest.getRedirectUrl().contains(redirectUrlForceDefaultTarget))
                {
                    requestCache.removeRequest(request, response);
                    break;
                }
            }
        }

      //...
    }

现在,通过声明新列表(例如listAllowedRedirectUrls)或用中的listAllowedRedirectUrls替换listRedirectUrlsForceDefaultTarget来反转该逻辑,并在SuccessHandler中spring-security-config.xml进行相应的更改。喜欢

<property name="listAllowedRedirectUrls">
    <list>/p/</list>
    <list>/search</list>
</property>

StorefrontAuthenticationSuccessHandler

    if (savedRequest != null)
    {
        for (final String listAllowedRedirectUrl : getListAllowedRedirectUrls())
        {
            if ( ! savedRequest.getRedirectUrl().contains(listAllowedRedirectUrl))
            {
                requestCache.removeRequest(request, response);
                break;
            }
        }
    }

您还必须对/ login / checkout处理程序声明(defaultLoginCheckoutAuthenticationSuccessHandler进行相同的更改

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Symfony-如何将用户重定向到登录后访问的上一页?

登录后将用户重定向到同一页面

根据Laravel中的上一页重定向到其他页面

登录后将用户重定向到其他页面

Laravel 5.4,如何在成功登录后将登录用户重定向到引荐URL?

用户确认后如何重定向到其他页面

基于用户角色的登录始终重定向到同一页面

登录后,用户根据asp.net中的角色重定向其他页面

如何从php中的登录页面重定向到“上一页和仪表板”

在angularjs中成功登录后如何重定向到其他页面?

WCF上的类成功登录后如何重定向其他页面

如何将用户重定向到单击编辑按钮的上一页

如何使用Spring Security将已登录的用户重定向到其首页,并将未登录的用户重定向到另一页

如何将登录 url 重定向到登录用户到新闻提要页面

登录后如何重定向到其他页面

如何根据目标用户将其重定向到其他路由?

成功提交表单并在确认消息上单击“确定”后,重定向到同一页面

React Native:如何根据用户的帐户类型在登录后重定向到不同的页面?

登录后如何根据用户角色重定向到所需页面?

成功登录后使用React Router重定向到下一页

如何将用户重定向到其他页面?

如果未激活用户帐户,则将其重定向到一页

Asp.net mvc登录后重定向到同一页面

在 Django 中,登录后将用户重定向到上一页

SSO-SAML,成功登录后将用户重定向到指定的登录页面

如果用户在django中注册后首次登录,如何将用户重定向到其他页面?

仅当用户来自表单页面时,如何在提交表单后将CodeIgniter中的URL重定向回上一页?

身份验证后如何将用户重定向回同一页面?

点击重定向到其他商店的同一页面