title: How to validate a login inside a bean using spring security?
body: <p>So I'm learning Spring and I'm using JSF with PrimeFaces.</p>
  
  <p>My question:</p>
  
  <p>I would like to know how (if possible) to authenticate and authorize user credentials using a boolean function in a bean, like this:</p>
  
  <pre><code>public boolean check() {
    boolean isLoginValid = false;
    if (//run something like j_spring_security_check) {
        //obtain user authorizations....
        isLoginValid = true;
    }
    return isLoginValid;
  }
  </code></pre>
  
  <p>The reason:</p>
  
  <pre><code>public void doLogin() {
      RequestContext context = RequestContext.getCurrentInstance();
  
      FacesMessage msg;
      boolean loggedIn;
  
      if (check() //would use the functin here) {
          loggedIn = true;
          msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", usuario);
      } else {
          loggedIn = false;
          msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
      }
  
      FacesContext.getCurrentInstance().addMessage(null, msg);
      context.addCallbackParam("loggedIn", loggedIn);
      context.addCallbackParam("authorization", this.auth #obtained in check());
  }
  </code></pre>
  
  <p>This way I could have the boolean sent to my button via ajax, like this:</p>
  
  <pre><code>&lt;p:commandButton id="loginButton" value="Login" update=":growl"   
                            actionListener="#{loginBean.doLogin}"   
                            oncomplete="handleLoginRequest(xhr, status, args)"/&gt;
  
  function handleLoginRequest(xhr, status, args) {
              if(args.validationFailed || !args.loggedIn) {  
                  jQuery('#dialog').effect("shake", { times:3 }, 100);
              } else {
                  jQuery('#dialog').effect("shake", { times:1 }, 200);
                  //And redirect to the right page.
              }  
          }
  </code></pre>
  
  <p>Thanks!</p>