Search code examples
securityjsfjakarta-eejbossjaas

Can't use programmatic security with custom JSF login form


I'm trying to do something like that: https://stackoverflow.com/a/2207147/494826

<security-constraint>
        <display-name>Amministrazione</display-name>
        <web-resource-collection>
            <web-resource-name>wrcollAdmin</web-resource-name>
            <description/>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>admin</role-name>
            <role-name>guest</role-name>
        </auth-constraint>
    </security-constraint>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Risorse</web-resource-name>
            <url-pattern>/javax.faces.resource/*</url-pattern>
        </web-resource-collection>
        <!-- No Auth Contraint! -->
    </security-constraint>

    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.htm</form-login-page>
            <form-error-page>/loginError.htm</form-error-page>
        </form-login-config>
    </login-config>  

My login JSF contains:

<h:form id="loginForm">                 
    <div class="inputlabel">
        <h:outputLabel for="j_username" value="Utente:"/>
    </div>
    <div>
        <h:inputText value="#{loginController.username}" id="j_username" size="25" />
    </div>
    <div class="inputlabel">
        <h:outputLabel for="j_password" value="Password:"/>
    </div>
    <div>
        <h:inputText value="#{loginController.password}" id="j_password" size="25" />
    </div>
    <div>
        <h:commandButton action="#{loginController.login}" value="LOGIN" />
    </div>                  
</h:form>
<h:messages styleClass="errors" />
    ...

And here's the login method:

public void login() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    ExternalContext externalContext = context.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    try {
        log4jLogger.info("LOGIN");
        request.login(username, password);            
        externalContext.redirect("/");
    } catch (ServletException e) {
        log4jLogger.info("DENIED");
        context.addMessage(null, new FacesMessage("Accesso Negato"));        
    }
}

This doesn't work, because the form never calls the login() method. Why? I suppose it behaves like that because of the web.xml security configuration. Maybe it considers the loginController (a named @ViewScoped bean) as a protected resource?

The second question is more general. Is that the right way for achieving programmatic security?


Solution

  • As the login page itself is also covered by the security constraint restriction and the login is not handled by j_security_check, you need to add the login page itself to the collection of allowed resources as well.

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Risorse</web-resource-name>
            <url-pattern>/javax.faces.resource/*</url-pattern>
            <url-pattern>/login.htm</url-pattern>
        </web-resource-collection>
        <!-- No Auth Contraint! -->
    </security-constraint>