So I'm learning Spring and I'm using JSF with PrimeFaces.
My question:
I would like to know how (if possible) to authenticate and authorize user credentials using a boolean function in a bean, like this:
public boolean check() {
boolean isLoginValid = false;
if (//run something like j_spring_security_check) {
//obtain user authorizations....
isLoginValid = true;
}
return isLoginValid;
}
The reason:
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());
}
This way I could have the boolean sent to my button via ajax, like this:
<p:commandButton id="loginButton" value="Login" update=":growl"
actionListener="#{loginBean.doLogin}"
oncomplete="handleLoginRequest(xhr, status, args)"/>
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.
}
}
Thanks!
See Question&Answers more detail:os