My approach - maybe you will find some errors.
Simple JSF Bean
public class LoginBean extends BaseBean {
private String login;
private String password;
private String lang;
public String getLogin() {
return login;
}
public void setLogin(final String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(final String password) {
this.password = password;
}
public String getLang() {
return lang;
}
public void setLang(final String lang) {
this.lang = lang;
}
/**
*
* @return
*/
public String loginProcess() {
log.debug("loginProcess()");
String result = "error";
try {
ServiceLocator loc = ServiceLocator.getInstance();
IUserManager man = (IUserManager) loc
.getObject(ServiceLocator.USER_MANAGER);
SystemUser user = man
.loginUser(this.getLogin(), this.getPassword());
CurrentUserInfoBean cur = getCurrentUser();
cur.setUserLogin(user.getLogin());
cur.setUserId(user.getId());
cur.setCurrentHotel(man.getLastHotel(user.getId()).getId());
cur.setWhenLoggedIn(new Date());
return "success";
} catch (Exception ex) {
log.error(ex);
addLoginNotFoundMessage();
}
return result;
}
Snippet from BaseBean
public CurrentUserInfoBean getCurrentUser() {
log.debug("getCurrentUser()");
Object o = getApplication().createValueBinding("#{currentUserInfo}")
.getValue(getFacesContext());
if (o == null) {
CurrentUserInfoBean bean = new CurrentUserInfoBean();
updateCurrentUserInfoBean(bean);
}
return (CurrentUserInfoBean) getApplication().createValueBinding(
"#{currentUserInfo}").getValue(getFacesContext());
}
/**
*
* @param bean
*/
public void updateCurrentUserInfoBean(final CurrentUserInfoBean bean) {
log.debug("updateCurrentUserInfoBean() param :" + bean);
getApplication().createValueBinding("#{currentUserInfo}").setValue(
getFacesContext(), bean);
}
And faces-config.xml
<managed-bean>
<managed-bean-name>currentUserInfo</managed-bean-name>
<managed-bean-class>com.hms.view.bean.session.CurrentUserInfoBean
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
How I use PhaseListener
public void afterPhase(PhaseEvent phaseEvent) {
log.debug("AfterPhase working");
FacesContext fc = phaseEvent.getFacesContext();
boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login")
> -1 ? true
: false;
if (!loginPage && !SessionUtils.isUserLoggedIn()) {
log.debug("User is not logged - redirecting to login page");
NavigationHandler nh = fc.getApplication
().getNavigationHandler();
nh.handleNavigation(fc, null, "notLogged");
} else {
log.debug("User is logged in - go to page "
+ fc.getViewRoot().getViewId());
}
log.debug(fc);
}
2007/8/8, Michał 'Gandalf' Stawicki <[EMAIL PROTECTED]>:
>
> Write a bean to store login info and use PhaseListener to check if
> user is logged in if he wants to access pages that need hi to be
> logged in, and redirect to login page if he is not.
>
> there were some good articles describing this approach. unfortunately
> I can't share my code.
>
>
> On 08/08/07, ignicolist <[EMAIL PROTECTED]> wrote:
> >
> > is if you could send to me i would apreciate very much.
> >
> > TKS
> >
> >
> > Anton Gavazuk wrote:
> > >
> > > What you mean - "filter for sessions"?
> > > I can send you some pieces of my own app with login\logut funcs
> > >
> > > 2007/8/8, ignicolist <[EMAIL PROTECTED]>:
> > >>
> > >>
> > >> Hi, i need some help, i have a aplication in jsf, with a sql
> database
> > >> using
> > >> hibernate, and i want implement a login and logout, but i dont know
> how,
> > >> i
> > >> google much, but all the solutions are so complicated. so i have a
> > >> example
> > >> with a filter, and a put that filter block the pages for non log
> users,
> > >> but
> > >> i dont know the sufs about sessions and so on... anyone have some
> example
> > >> working for i use in my aplication?
> > >>
> > >> I just want a simple login logout example, using a filter for
> sessions.
> > >>
> > >>
> > >> Tks for all the help...
> > >> --
> > >> View this message in context:
> > >> http://www.nabble.com/JSF-Authentication-tf4235729.html#a12051322
> > >> Sent from the MyFaces - Users mailing list archive at Nabble.com.
> > >>
> > >>
> > >
> > >
> >
> > --
> > View this message in context:
> http://www.nabble.com/JSF-Authentication-tf4235729.html#a12053317
> > Sent from the MyFaces - Users mailing list archive at Nabble.com.
> >
> >
>
>
> --
> Michał Stawicki
>
> [EMAIL PROTECTED]
> http://stawicki.jasliska.pl
>