当前位置: 首页 > 知识库问答 >
问题:

FullAjaxExceptionHandler在ajax按钮上不显示会话过期错误页面

端木宏才
2023-03-14

我已经实现了Omnifaces FullAjaxExceptionHandler,但问题是它不能处理ajax请求。在会话到期后,当我单击非ajax按钮时,它工作得很好。它将用户重定向到自定义错误页面。但是,如果按钮使用ajax,那么它什么都不做。佩奇只是个傻瓜。

编辑:我已将ActionListener更改为Action,但仍然相同。

编辑2:没有错误。ApacheTomcat输出和ApacheTomcat日志都没有。

这是我的Spring Security;

<http auto-config='true' use-expressions="true">
    <intercept-url pattern="/login" access="permitAll"/>
    <intercept-url pattern="/ajaxErrorPage" access="permitAll"/>
    <intercept-url pattern="/pages/*" access="hasRole('admin')" />
    <intercept-url pattern="/j_spring_security_check" access="permitAll"/>        
    <logout logout-success-url="/login.xhtml" />
    <form-login login-page="/login.xhtml"
                login-processing-url="/j_spring_security_check"                                                       
                default-target-url="/pages/index.xhtml"
                always-use-default-target="true"                                                        
                authentication-failure-url="/login.xhtml"/>
</http>

共有2个答案

谷森
2023-03-14

在文件Spring安全(在档案中的Spring安全)

<beans:bean id="httpSessionSecurityContextRepository" class="org.springframework.security.web.context.HttpSessionSecurityContextRepository"/>

<!-- redirection strategy -->
<beans:bean id="jsfRedirectStrategy" class="com.mycompany.JsfRedirectStrategy">
    <beans:property name="invalidSessionUrl" value="/login.xhtml" />
</beans:bean>

<beans:bean id="sessionManagementFilter" class="org.springframework.security.web.session.SessionManagementFilter">
    <beans:constructor-arg name="securityContextRepository" ref="httpSessionSecurityContextRepository" />
    <beans:property name="invalidSessionStrategy" ref="jsfRedirectStrategy" />
</beans:bean>

<http auto-config='true' use-expressions="true">
    <intercept-url pattern="/login" access="permitAll"/>
    <intercept-url pattern="/ajaxErrorPage" access="permitAll"/>
    <intercept-url pattern="/pages/*" access="hasRole('admin')" />
    <intercept-url pattern="/j_spring_security_check" access="permitAll"/>        
    <logout logout-success-url="/login.xhtml" />
    <form-login login-page="/login.xhtml"
            login-processing-url="/j_spring_security_check"                                                       
            default-target-url="/pages/index.xhtml"
            always-use-default-target="true"                                                        
            authentication-failure-url="/login.xhtml"/>

     <!-- custom filter -->
    <custom-filter ref="sessionManagementFilter"  before="SESSION_MANAGEMENT_FILTER" />

</http>

自定义重定向策略(个性化重定向策略)

public class JsfRedirectStrategy implements InvalidSessionStrategy
{

    private static final String FACES_REQUEST = "Faces-Request";

    private String invalidSessionUrl;

    public void setInvalidSessionUrl(String invalidSessionUrl) {
        this.invalidSessionUrl = invalidSessionUrl;
    }

    public String getInvalidSessionUrl() {
       return invalidSessionUrl;
    }



    @Override
    public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {



        String contextPath = request.getContextPath();
        String urlFinal = contextPath+invalidSessionUrl;




        if ("partial/ajax".equals(request.getHeader(FACES_REQUEST))) {
            // with ajax
            response.setContentType("text/xml");
            response.getWriter()
                .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", urlFinal);
        } else {

            // not ajax
            request.getSession(true);
            response.sendRedirect(urlFinal);

        }

   }

为我工作。

弓俊晖
2023-03-14

您正在发送一个同步重定向作为对ajax请求的响应(HTTP 302响应,使用例如response.sendRedirect())。这是不对的。JavaScript ajax引擎将302响应视为重新发送ajax请求的新目标。然而,这反过来会返回一个普通的HTML页面,而不是一个XML文档,其中包含要更新页面的哪些部分的说明。这是令人困惑的,因此重定向的响应被完全忽略。这正好解释了你所面临的症状。

同样的问题也在以下密切相关的问题中提出和回答:

  • FullAjaxExceptionHandler在无效会话后不会重定向到错误页面
  • 当会话过期时,如果用户单击浏览器后退按钮,如何将用户移动到超时页面
  • 获取由浏览器启动的重定向请求,但未成功
  • 会话到期时的授权重定向在提交JSF表单时不起作用,页面保持不变
  • JSF筛选器在初始重定向后未重定向

基本上,您需要以某种方式指示Spring Security执行以下条件检查:

if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
    // JSF ajax request. Return special XML response which instructs JavaScript that it should in turn perform a redirect.
    response.setContentType("text/xml");
    response.getWriter()
        .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
        .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", loginURL);
} else {
    // Normal request. Perform redirect as usual.
    response.sendRedirect(loginURL);
}

然而,我不是Spring用户,我对使用它不感兴趣,因此无法给出如何在Spring Security性中执行此检查的更详细答案。然而,我可以看出ApacheShiro有着与本文中解释和解决的问题完全相同的问题:让Shiro JSF支持Ajax。

 类似资料:
  • @将有效的打印信息发送到控制台,但不发送到页面 当我的字段有错误@Valid print info到控制台时,为什么不在第页? 字段hotelName上的对象formData中的字段错误:拒绝的值[];代码[Size.formData.hotelName, Size.hotelName, Size.java.lang.String, size];参数[org.springframework.con

  • 我在微型MCE配置中添加了缩进和向外缩进按钮,如下所示: tinyMCE.init({ // 常规选项模式:“textareas”,主题:“高级”,editor_selector:cl2,皮肤:“o2k7”,插件:“advimage,advlink,spellchecker,inlinepopups,contextmenu,paste,noneditable,mergefields” , spel

  • 将“然后向我显示页面”这一行添加到cucumber场景中,在我同事的计算机上使用相同的设置,但在我的计算机上则不然。实际上,添加暂停步骤定义并调用它似乎被完全忽略了。 步骤定义: 功能: 命令行: 使用Firefox 16.02(避免17中的错误) 两台机器都有git-fetch'd,拉取,捆绑安装,所以所有的宝石都是相同的,都使用Rbenv版本* 1.9.3-p327-perf。我可能错过了一些

  • 问题内容: 当我尝试使用复选按钮时,它可以正常工作,但文本不会出现。我不明白为什么。下面是我的代码 出现复选框,但旁边的文本没有 问题答案: 尝试使用主题的tk(ttk)检查按钮小部件:

  • 我在Tomcat 8.x web服务器上使用JAAS和基于表单的web身份验证。用户在尝试登录时被拒绝访问的原因有很多: 无效的用户名 密码无效 在数据库中没有帐户 数据库中的帐户未激活 数据库中的帐户没有必需的角色 等…… 在我的LoginModule中,我检查所有这些条件以及更多条件,如果不允许用户访问web应用程序,就会抛出LoginException。JAAS似乎捕捉到了抛出的LoginE