所以我正在学习 Spring,我正在使用带有 PrimeFaces 的 JSF。
我的问题:
我想知道如何(如果可能)使用 bean 中的布尔函数对用户凭据进行身份验证和授权,如下所示:
public boolean check() {
boolean isLoginValid = false;
if (//run something like j_spring_security_check) {
//obtain user authorizations....
isLoginValid = true;
}
return isLoginValid;
}
原因:
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());
}
这样我就可以通过 ajax 将布尔值发送到我的按钮,如下所示:
<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.
}
}
谢谢!