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

red*_*ead 7 jsf login java-ee prettyfaces

使用JSF在Java EE中开发Web应用程序.通过身份验证表单查看所有页面,并使用操作'j_security_check'并输入'j_username'和'j_password'.

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

/faces/javax.faces.resource/jsf.js?ln=javax.faces&stage=Development
Run Code Online (Sandbox Code Playgroud)

所以我正在查看脚本文件jsf.js,其中包含所有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>
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 9

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

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

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

@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);
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)