0

when i set my login.xhtml page to be the web.xml to be the welcome file, it does not work, i mean it displays the page just fine but when i press log in nothing happens, but when i manually go to the log in page http://localhost:8080/fileuploadWithPreview/login.xhtml it works perfectly, any ideas why this is ?

web.xml :

       <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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_2_5.xsd">
    <filter>
        <filter-name>Upload Filter</filter-name>
        <filter-class>richard.fileupload.UploadFilter</filter-class>
        <init-param>
            <param-name>sizeThreshold</param-name>
            <param-value>1024</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Upload Filter</filter-name>
        <url-pattern>/faces/upload/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.xhtml</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.LIBRARIES</param-name>
        <param-value>/WEB-INF/corejsf.taglib.xml</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>LDAP</realm-name>
        <form-login-config>
            <form-login-page>/login.xhtml</form-login-page>
            <form-error-page>/login-failed.xhtml</form-error-page>
        </form-login-config>
    </login-config>
    <security-role>
        <role-name>user</role-name>
    </security-role>
    <security-constraint>     
        <!-- web resources that are protected -->
        <web-resource-collection>
            <web-resource-name>All Resources</web-resource-name>
            <url-pattern>/*</url-pattern>
            <!-- this is currently causing a 404 -->
            <http-method>GETLIB</http-method>
            <http-method>COPY</http-method>
            <http-method>MOVE</http-method>
            <http-method>DELETE</http-method>
            <http-method>PROPFIND</http-method>
            <http-method>GET</http-method>
            <http-method>HEAD</http-method>
            <http-method>PUT</http-method>
            <http-method>MKCOL</http-method>
            <http-method>PROPPATCH</http-method>
            <http-method>LOCK</http-method>
            <http-method>UNLOCK</http-method>
            <http-method>VERSION-CONTROL</http-method>
            <http-method>CHECKIN</http-method>
            <http-method>CHECKOUT</http-method>
            <http-method>UNCHECKOUT</http-method>
            <http-method>REPORT</http-method>
            <http-method>UPDATE</http-method>
            <http-method>CANCELUPLOAD</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>
</web-app>

Ok so it is working, just when i press the login button it takes me back to the login page but it has already authenticated the user, how do i get it on the login button to go to the index.html page once authenticated ?

login page :

<div id="site_content">
    <div id="content">
        <h:body>
            <form method="post" action="j_security_check">
                <p>You need to log in to access protected information.</p>
                <table>
                    <tr>
                        <td>User name:</td>
                        <td><input type="text" name="j_username" /></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="j_password" /></td>
                    </tr>
                </table>
                <p><input type="submit" value="Login" />

                    <!--<h:commandButton value="This will reset all the data " type="button" > </h:commandButton> -->
                    <input type="reset" name="Login-reset" value="Reset" onclick="alert('This will reset all the data');" />
                </p>
            </form>
        </h:body>
    </div>
</div>
user1924104
  • 891
  • 2
  • 16
  • 38

1 Answers1

1

Replace <url-pattern>/faces/*</url-pattern> by <url-pattern>*.xhtml</url-pattern> and get rid of all /faces/ paths in all your URLs. This way everything should work as expected.

Explanation: the <welcome-file> expects a physically existing file in the currently requested folder(!) which should have been index.xhtml or login.xhtml and the <form-login-page> expects a fullworthy URI which should have been /faces/login.xhtml. That don't mix quite well when you use a folder mapping for JSF. So just map it on *.xhtml and you never need to worry about virtual URLs.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks ! i have managed to get it to work, although now i have a new issue, where when i press the login button it takes me back to the login page but it has already authenticated the user, how do i get it on the login button to go to the index.html page once authenticated ? – user1924104 Jan 23 '13 at 14:39
  • It will just go to the initially requested page, which is apparently `login.xhtml` itself. You need to change the welcome file back to `index.xhtml`. Which would also make much more sense in hindsight, you surely don't have a `login.xhtml` file in every subfolder of your webapp, right? Perhaps you misunderstood what `` means; it should specify the name of the file in the **current** folder which should be served to the user when **a folder** is been requested in the URL. – BalusC Jan 23 '13 at 14:40
  • i only have one folder, currently as it is a very small project, so login.xhtml is in it, is there a way to change the login button to go to index as this is the main page if you like or should i start renaming login as index, as i have a lot of things associated with login.xhtml – user1924104 Jan 23 '13 at 14:46
  • As said, change the welcome file back to `index.xhtml`. The `j_security_check` will upon successful login redirect to the initially requested page. That won't help if the initially requested page is the login page itself. – BalusC Jan 23 '13 at 14:46
  • Ok thanks, whats the best way for on load of the project to start up with the login page, but the homepage and everything to be index ? – user1924104 Jan 23 '13 at 14:53
  • Just set the security constraint URL pattern on `/*`. This way the login page will automatically be displayed when visiting the site while not logged-in. – BalusC Jan 23 '13 at 15:14
  • Stupid question - how ? as i think i have done it, updated the question , but it is still not working, also would this mean the users would have to be authenticated if they wanted to view any of the pages ? – user1924104 Jan 23 '13 at 15:24
  • The auth constraint is missing. Add `*` to security constraint if you want to cover all roles. – BalusC Jan 23 '13 at 15:30
  • Thanks, just added that but now get a `404 The requested resource () is not available.` error on start up of the project now – user1924104 Jan 23 '13 at 15:39
  • managed to fix the 404, but now i et a 403 error, it looks like it is blocking the .css file, how do i stop this ? `http://localhost:8080/fileuploadWithPreview/javax.faces.resource/theme.css.xhtml?ln=primefaces-aristo` `HTTP Status 403 - Access to the requested resource has been denied ` – user1924104 Jan 23 '13 at 16:12
  • Add another security constraint which allows `/javax.faces.resource/*`. See also e.g. http://stackoverflow.com/questions/13822978/css-skin-not-showing-in-primefaces-3-4/13823189#13823189 – BalusC Jan 23 '13 at 16:29
  • quick final question, i have my css file in a sub folder called css, how would i link this in the `` – user1924104 Jan 23 '13 at 16:54
  • Just add the appropriate `` entry to the security constraint. However, it's strongly recommended to use `` as it manages among others caching, versioning and easy templating for you. – BalusC Jan 23 '13 at 16:56