3

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 app to redirect unauthorized users to login page. The funny thing i had this working but i made some stupid changes and now on accessing localhost:8080 i always see secure.xhtml even when not logged in. localhost:8080/secured/secure.xhtml redirects fine.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ivan Ivanovich
  • 227
  • 1
  • 3
  • 7

1 Answers1

7

You are not using <welcome-file> entirely correctly. It should represent the sole filename of the file which needs to be served whenever a folder is been requested, irrespective of the requested folder (the root /, or /public/ or /secured/, etc).

The welcome file is served by an internal forward as performed by RequestDispatcher#forward(). Internal forwards do not trigger the security constraints. You need to send a redirect instead.

Change the <welcome-file> to a more sane default, e.g. index.xhtml.

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

And create one in the webapp's root like /index.xhtml. If you need to redirect every request on /index.xhtml to /secured/secure.xhtml, then there are basically 2 ways:

  1. Map a Filter on the URL pattern of /index.xhtml and call response.sendRedirect("secured/secure.xhtml") inside the doFilter() method. 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. Put a <f:event type="preRenderView"> in the /index.xhtml which calls a backing bean method which in turn does a externalContext.redirect("secured/secure.xhtml"). E.g.

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

    with

    @ManagedBean
    @ApplicationScoped
    public class IndexBean {
    
        public void redirect() throws IOException {
            FacesContext.getCurrentInstance().getExternalContext().redirect("secured/secure.xhtml");
        }
    
    }
    
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Wow thanks! But what about checking if user is logged in? Should i do this in same filter? Is it's ok to take this away from container? – Ivan Ivanovich Apr 17 '12 at 14:50
  • 2
    The `` already does that. You don't need to do it yourself. The sole point is that you need to perform a redirect instead of a forward. A redirect creates a brand new request which will then trigger the security constraints again with the new URL. For a forward it's too late because the security constraints have *already* been tested based on the initial URL of `/` which is as per your configuration publicly accessible without constraints. – BalusC Apr 17 '12 at 14:51
  • i have a trouble with filter. It seams if i have `security-constraint` filter is not called, if i comment out `constraint` it starts working. Maybe its because they are overlapping on /secured/* pages? Server - glassfish 3.1. Please, if you have time, help me out. – Ivan Ivanovich Apr 17 '12 at 17:15
  • Apparently you've changed the URL pattern of the filter to the one which is matched by the security constraint, or you've changed the security constraint to match `/index.xhtml` as well. This is not the intention. – BalusC Apr 17 '12 at 17:17
  • Big thanks, works perfectly. But i have 1 more question, if you don't mind: is it possible to map some filter to the same url-pattern as constraints and get it called before constraint, for example if i want to check cookie or something like that? – Ivan Ivanovich Apr 18 '12 at 10:05