Welcome file ignores security restriction

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

I want my web application to redirect unauthorized users to the login page. The funny thing is that I had this work, but I made some stupid changes, and now when I access it, localhost:8080I always see secure.xhtml, even when it is not logged in. localhost:8080/secured/secure.xhtmlredirects a penalty.

+3
source share
1 answer

You are not using <welcome-file>completely correctly. It should represent the only file name that should be submitted whenever a folder is requested, regardless of the requested folder (root /or /public/or /secured/, etc.).

, 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, 2 :

  • Filter URL /index.xhtml response.sendRedirect("secured/secure.xhtml") doFilter(). .

    @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"));
        }
    
        // ...
    }
    
  • a <f:event type="preRenderView"> /index.xhtml, bean, , , externalContext.redirect("secured/secure.xhtml"). .

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

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

All Articles