No title Revision 326133346238 (Tue Dec 08 2009 at 12:50) - Diff Link to this snippet: https://friendpaste.com/6f4VNxH71K61pKFhpci14q Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193|||faces-config.xml:|||<managed-bean> <description>login utente</description> <managed-bean-name>logIn</managed-bean-name> <managed-bean-class>autoshop.web.LogIn</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>username</property-name> <value>#{username}</value> </managed-property> <managed-property> <property-name>password</property-name> <value>#{password}</value> </managed-property> </managed-bean> <managed-bean> <description>bean per la sessione</description> <managed-bean-name>sessionBean</managed-bean-name> <managed-bean-class>autoshop.web.SessionBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>username</property-name> <value>#{username}</value> </managed-property> <managed-property> <property-name>isLoggato</property-name> <value>#{isLoggato}</value> </managed-property> </managed-bean> ..... ||| logIn.java: |||package autoshop.web;import autoshop.ejb.facade.EjbUtenteFacade;import autoshop.jpa.dominio.Utente;import javax.faces.context.FacesContext;import javax.naming.InitialContext;/** * * @author mill */public class LogIn{ private String username; private String password; public String getUsername(){ return this.username; } public void setUsername(String username){ this.username=username; } public String getPassword(){ return this.password; } public void setPassword(String password){ this.password=password; } public String loginAction(){ String result=""; EjbUtenteFacade euf=getEjbUtenteFacade(); Utente utente=euf.effettuaLogin(this.getUsername(),this.getPassword()); if(utente!=null){ this.sessionUtil(); result="success"; } else result="error"; return result; } private EjbUtenteFacade getEjbUtenteFacade(){ try{ InitialContext ctx = new InitialContext(); return (EjbUtenteFacade)ctx.lookup("AutoShop/EjbUtenteFacadeImpl/remote"); }catch(Exception e){ return null; } } private void sessionUtil(){ FacesContext context = FacesContext.getCurrentInstance(); SessionBean sessionBean = (SessionBean)context.getApplication().evaluateExpressionGet(context,"#{sessionBean}",SessionBean.class); sessionBean.setUsername(this.getUsername()); sessionBean.setIsLoggato(true); }}||| SessionBean.java |||package autoshop.web;/** * * @author mill */public class SessionBean { private String username; private boolean isLoggato; public String getUsername(){ return username; } public void setUsername(String username){ this.username=username; } public boolean getIsLoggato(){ return this.isLoggato; } public void setIsLoggato(boolean isLoggato){ this.isLoggato=isLoggato; }}||| SessionUtility.java ||| package autoshop.utility;import autoshop.web.SessionBean;import javax.faces.context.FacesContext;/** * * @author mill */public class SessionUtility { public static SessionBean getSessionBean(){ return (SessionBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionBean"); }}||| ControlloPermessiListener.java ||| /* * To change this template, choose Tools | Templates * and open the template in the editor. */package autoshop.utility;import javax.faces.application.NavigationHandler;import javax.faces.context.FacesContext;import javax.faces.event.PhaseEvent;import javax.faces.event.PhaseId;import javax.faces.event.PhaseListener;public class ControlloPermessiListener implements PhaseListener { public void afterPhase(PhaseEvent event) { FacesContext fc = event.getFacesContext(); //controllo se sono nella pagina di login boolean loginpage = fc.getViewRoot().getViewId().lastIndexOf("logIn") > -1 ? true : false; //se non sono nella pagina di login e se non sono loggato if (!loginpage && !SessionUtility.getSessionBean().getIsLoggato()) { NavigationHandler nh = fc.getApplication().getNavigationHandler(); nh.handleNavigation(fc, null, "logout"); } } public void beforePhase(PhaseEvent event) { } public PhaseId getPhaseId() { return PhaseId.RESTORE_VIEW; }}