使用安全约束时无法注销

时间:2013-11-09 13:05:50

标签: security jsf primefaces

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

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>

faces-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>

1 个答案:

答案 0 :(得分:0)

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

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