欢迎文件忽略安全性约束

时间:2012-04-17 14:23:58

标签: jsf servlets security-constraint welcome-file

我的 web.xml

    <context-param>
            <param-name>javax.faces.PROJECT_STAGE</param-name>
            <param-value>Development</param-value>
        </context-param>

        <welcome-file-list>
            <welcome-file>/secured/secure.xhtml</welcome-file>
        </welcome-file-list>

        <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>
        </servlet-mapping>

        <context-param>
            <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
            <param-value>true</param-value>
        </context-param> 
    <security-constraint>
        <web-resource-collection>
          <web-resource-name>Restricted</web-resource-name>
          <url-pattern>/secured/*</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>jdbc-realm</realm-name>
     <form-login-config>
       <form-login-page>/public/login.xhtml</form-login-page>
       <form-error-page>/public/error.xhtml</form-error-page>
     </form-login-config>
   </login-config>

我希望我的网络应用程序将未经授权的用户重定向到登录页面。有趣的事情我有这个工作,但我做了一些愚蠢的更改,现在访问localhost:8080我总是看到secure.xhtml即使没有登录。localhost:8080/secured/secure.xhtml重定向很好。

1 个答案:

答案 0 :(得分:7)

您没有完全正确使用<welcome-file>。它应该代表在请求文件夹时需要提供的文件的唯一文件名,而不管请求的文件夹(根//public//secured/等) 。

欢迎文件由RequestDispatcher#forward()执行的内部转发提供。内部转发会触发安全限制。您需要发送重定向。

<welcome-file>更改为更合理的默认设置,例如index.xhtml

<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

在webapp的根目录中创建一个,如/index.xhtml。如果您需要将/index.xhtml上的每个请求重定向到/secured/secure.xhtml,那么基本上有两种方式:

  1. /index.xhtml的网址格式上映射Filter,并在doFilter()方法中调用response.sendRedirect("secured/secure.xhtml")。 E.g。

    @WebFilter("/index.xhtml")
    public class IndexFilter implements Filter {
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.sendRedirect("secured/secure.xhtml"));
        }
    
        // ...
    }
    
  2. <f:event type="preRenderView">中放置一个/index.xhtml调用一个支持bean方法,后者又调用externalContext.redirect("secured/secure.xhtml")。 E.g。

    <f:event type="preRenderView" listener="#{indexBean.redirect}" />
    

    @ManagedBean
    @ApplicationScoped
    public class IndexBean {
    
        public void redirect() throws IOException {
            FacesContext.getCurrentInstance().getExternalContext().redirect("secured/secure.xhtml");
        }
    
    }