登录后不需要的重定向到 http://localhost:8080/login/j_security_check

Unwanted redirect to http://localhost:8080/login/j_security_check after logging in

当我登录时,我被重定向到 http://localhost:8080/login/j_security_check 而不是 web.xml 中指定的所需欢迎页面。只有在我之前注销时才会发生这种情况,如果我从头开始登录,它就像一个魅力。

登录页面

<html xmlns="http://www.w3.org/1999/xhtml"      
      xmlns:h="http://xmlns.jcp.org/jsf/html"

      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <h:outputStylesheet library="css" name="main.css"  />
        <title>Login</title>
    </h:head>
    <body>
        <div class="login_form">
            <h:form id="login" prependId="false" class="login_form"
                    onsubmit="document.getElementById('login').action = 'j_security_check';">
                <br/><br/>
                <p:graphicImage value="/resources/img/ggs_logo.png" styleClass="login_logo"/>
                <h1>Icosphere</h1> 
                <h1>Data Platform</h1>
                <p:inputText  id="j_username" size="20" />
                <br/>
                <p:password  id="j_password" size="20"/>
                <br/><br/>            
                <p:commandButton id="submit" value="Log in" ajax="false"/>

            </h:form>
        </div>
    </body>
</html>

注销页面

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:p="http://primefaces.org/ui"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <body>
        <div class="leftright">
            <span class="aligned">
                <h:form>
                    <p:commandButton ajax="false" action="#{logoutBean.logout()}"
                                     value="Logout!"/>  
                </h:form>          
            </span>
        </div>
    </body>
</html>

注销 Bean

@Named(value = "logoutBean")
@ApplicationScoped
public class LogoutBean {
    public String logout() throws ServletException {
        Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "/login/login.xhtml?faces-redirect=true'";
    }
}

您的 LogoutBean 需要重定向到您的 "welcome" 页面,而不是登录表单。

只要客户端请求受保护的资源,标准网络安全就会显示指定的登录表单。当用户验证容器 returns 最初请求的资源时。

所以,您正在故意显示登录表单;但它受到保护,因此容器会将您重定向到相同的登录表单;用户进行身份验证,然后 returns 最初请求的登录表单。

因此您永远不会 link 直接进入登录页面。一旦请求受保护的资源,它将始终显示。