安全约束不适用于FORM身份验证方法

时间:2013-06-26 10:33:16

标签: java security java-ee glassfish-3

我想使用Glassfish 3.1.2实现基于表单的身份验证。我的目标是仅在登录后使/ faces / user文件夹中的内容可用,因此从此文件夹中检索任何页面都应重定向到登录页面。不幸的是,重定向不起作用。

我在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">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <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>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/faces/signin.xhtml</form-login-page>
            <form-error-page>/faces/error.xhtml</form-error-page>
        </form-login-config>
    </login-config>


    <security-role>
        <description/>
        <role-name>users</role-name>
    </security-role>


    <security-constraint>
        <display-name>Restricted to users</display-name>
        <web-resource-collection>
            <web-resource-name>Restricted Access</web-resource-name>
            <url-pattern>/faces/user/*</url-pattern>
            <http-method>DELETE</http-method>
            <http-method>PUT</http-method>
            <http-method>HEAD</http-method>
            <http-method>OPTIONS</http-method>
            <http-method>TRACE</http-method>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>users</role-name>
        </auth-constraint>
        <user-data-constraint>
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

</web-app>

问题是,我可以直接打开http://domain.com/faces/user/dashboard.xhtml 并看到内容。没有显示登录表单!

如果我改变

<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/faces/signin.xhtml</form-login-page>
        <form-error-page>/faces/error.xhtml</form-error-page>
    </form-login-config>
</login-config>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>jdbc-realm</realm-name>
</login-config>

显示HTML身份验证,如果没有提供正确的凭据,我将无法访问http://domain.com/faces/user/dashboard.xhtml

我的安全限制有什么问题?

感谢任何帮助。提前致谢

2 个答案:

答案 0 :(得分:1)

您仍需要提供基于表单的身份验证的领域:

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>jdbc-realm</realm-name>
    <form-login-config>
        <form-login-page>/faces/signin.xhtml</form-login-page>
        <form-error-page>/faces/error.xhtml</form-error-page>
    </form-login-config>
</login-config>

答案 1 :(得分:1)

我解决了。问题不在于web.xml配置本身。

我在项目中使用pretty-faces。似乎映射制动了表单重定向。将漂亮的面孔放在项目之外可以解决所有问题。