jsf - 登录后重定向错误(Java EE w/JSF)

标签 jsf jakarta-ee login prettyfaces

使用 JSF 在 Java EE 中开发 Web 应用程序。所有页面都通过带有操作“j_security_check”和输入“j_username”和“j_password”的身份验证表单进行保护,以防止查看。

但是,成功登录后,我被重定向的不是我想要访问的页面,而是这个 URL

/faces/javax.faces.resource/jsf.js?ln=javax.faces&stage=Development

所以我正在查看带有所有 JS 代码的脚本文件 jsf.js,而不是我想要查看的页面。无论我访问 Web 根目录还是任何其他页面都没有关系,我每次都被重定向到此 URL。然后我将 URL 更改为任何页面,它可以正常加载并登录。

我不得不说我已经遇到了这个问题,它神奇地消失了,所以它正确地重定向了我。几周后它又坏了,但我不知道这是我的错,如果是我不知道原因。我根本没有搞乱重定向或导航规则。

值得一提的是,我也在使用 PrettyFaces。

编辑:
<security-constraint>
    <display-name>secured</display-name>
    <web-resource-collection>
        <web-resource-name>all</web-resource-name>
        <description/>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <description/>
        <role-name>admin</role-name>
        <role-name>teacher</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <display-name>secured for admins</display-name>
    <web-resource-collection>
        <web-resource-name>admin pages</web-resource-name>
        <description/>
        <url-pattern>/admin/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <description/>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <display-name>unsecured</display-name>
    <web-resource-collection>
        <web-resource-name>css</web-resource-name>
        <description/>
        <url-pattern>/css/*</url-pattern>
    </web-resource-collection>
    <web-resource-collection>
        <web-resource-name>js</web-resource-name>
        <description/>
        <url-pattern>/js/*</url-pattern>
    </web-resource-collection>
    <web-resource-collection>
        <web-resource-name>img</web-resource-name>
        <description/>
        <url-pattern>/img/*</url-pattern>
    </web-resource-collection>
</security-constraint>
<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>wetk-security</realm-name>
    <form-login-config>
        <form-login-page>/faces/login.xhtml</form-login-page>
        <form-error-page>/faces/login.xhtml</form-error-page>
    </form-login-config>
</login-config>

最佳答案

容器管理的安全性将重定向到触发身份验证检查的最后一个 HTTP 请求。在您的情况下,它显然是自动包含的 JSF ajax API JavaScript 文件。如果浏览器已经从浏览器缓存中完整加载了待验证页面,而浏览器已经从服务器端完整加载了 JS 文件,或者已经通过条件 GET 请求测试了 JavaScript 文件的缓存有效性,就会发生这种情况.

您想从身份验证检查中排除 JSF 资源( <h:outputScript><h:outputStylesheet><h:graphicImage> 。您可以通过排除常见的 URL 模式 /javax.faces.resource/* 来做到这一点。您可能只想添加 /faces 前缀模式,因为您显然正在使用它而不是 *.xhtml 后缀模式。

您还需要指示浏览器不要缓存受限页面以防止浏览器从缓存中加载它(例如,通过在注销后按后退按钮)。将以下过滤器映射到与您的 <security-constraint> 相同的 URL 模式上.

@WebFilter("/secured/*") // Use the same URL pattern as <security-constraint>
public class NoCacheFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpRes = (HttpServletResponse) response;

        if (!httpReq.getRequestURI().startsWith(httpReq.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
            httpRes.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            httpRes.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            httpRes.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(request, response);
    }

    // ...
}

关于jsf - 登录后重定向错误(Java EE w/JSF),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903023/

相关文章:

jsf - 选择日期后,Primefaces 5.1 日历在文本字段中失去焦点

gwt - 如何在GWT应用中实现登录页面?

login - 从 chrome 扩展程序登录到我的网站

jsf - JSF 中的基本安全性

javascript - 如何在 JSF 上使用 Markdown 编辑器 - Primefaces

internet-explorer - IE 11 中禁用的 primefaces 编辑器工具栏

java - 如何在jsf中显示多对多的jpa集合?

java - 在 JSP 中循环访问响应变量

java - 这个LoginModule 有什么问题吗?如何在 Jboss EAP 6.1 中使用 JAAS 对用户进行身份验证?

java - 如何在 java 1.6 项目中使用 java 1.3 编译的 Web 服务工作?