0

我使用 Glassfish 3.1.2.2 和 Primefaces 4.0 编写了一个应用程序。这真的是开始,所以没有太多的事情发生。我添加了基于表单的安全性。当我第一次这样做时,我的 WAR 文件中的同一个文件夹中包含所有 xhtml 页面。我可以通过在我的 UserBean 中使用以下方法来注销

public String logout() {
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    session.invalidate();
    return "welcomeRedir";
    }

我的 faces-config.xml 有以下导航规则

    <navigation-rule>
    <navigation-case>
        <from-outcome>welcomeRedir</from-outcome>
        <to-view-id>/welcome.xhtml</to-view-id>
        <redirect/>
    </navigation-case>
</navigation-rule>

welcome.xhtml 是原始登录页面。而且,当我以这种方式定义它时,它起作用了。我可以使用 JDBC 领域登录,当我单击注销按钮时,页面导航回欢迎页面。

接下来,我想添加安全约束。因此,我将相关页面(但不是欢迎页面)移动到子目录,并将适当的安全约束元素添加到 web.xml。我仍然可以登录这些页面。

但是,当我单击注销按钮时,什么也没有发生。如果我查看从浏览器发送的网络流量,它会收到 403 错误作为响应。

我看了又看,但找不到任何相关的东西。有没有人在定义安全约束时让它工作?

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <!-- More efficient, in an AJAX environment, to have server side state saving -->
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <!-- HTML comments become components unless they're stripped -->
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <!-- PrimeFaces Theme -->
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>cupertino</param-value>
    </context-param>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>welcome.xhtml</welcome-file>
    </welcome-file-list>
    <resource-ref>
        <res-ref-name>jdbc/resmandb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    <security-constraint>
        <display-name>Protected Pages</display-name>
        <web-resource-collection>
            <web-resource-name>ApplicationPages</web-resource-name>
            <url-pattern>/protected/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
            <role-name>registeredUser</role-name>
        </auth-constraint>
    </security-constraint>
    <security-constraint>
        <display-name>System Admin Pages</display-name>
        <web-resource-collection>
            <web-resource-name>SysAdminPages</web-resource-name>
            <description/>
            <url-pattern>/sysadmin/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>authentication-realm</realm-name>
        <form-login-config>
            <form-login-page>/welcome.xhtml</form-login-page>
            <form-error-page>/welcome.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <description>A System Admin of ResMan</description>
        <role-name>admin</role-name>
    </security-role>
    <security-role>
        <description>A Registered User of ResMan</description>
        <role-name>registeredUser</role-name>
    </security-role>
</web-app>

面孔-config.xml

<faces-config version="2.1"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd">
    <application>
        <resource-bundle>
            <base-name>messages</base-name>
            <var>msg</var>
        </resource-bundle>
    </application>
    <navigation-rule>
        <navigation-case>
            <from-outcome>welcome</from-outcome>
            <to-view-id>/welcome.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <navigation-case>
            <from-outcome>welcomeRedir</from-outcome>
            <to-view-id>/welcome.xhtml</to-view-id>
            <redirect/>
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <navigation-case>
            <from-outcome>reservations</from-outcome>
            <to-view-id>/protected/reservations.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>
4

1 回答 1

0

事实证明,这与 faces 配置无关,更多的是关于我的 glassfish-web.xml 如何定义其 security-role-mapping 元素。元素中的值与我的 JDBC 领域中包含的组不匹配(即,我的数据库表中的组名称)。一旦我正确定义了这些,注销就可以了。

事实上,在我这样做之前,Primefaces 的 AJAX 请求都没有被接受。这不是注销问题,而是一般交互问题。

于 2013-11-09T21:00:23.733 回答