| a | b | |
|---|
| 0 | + | faces-config.xml: |
|---|
| 0 | + | |
|---|
| 0 | + | <managed-bean> |
|---|
| 0 | + | <description>login utente</description> |
|---|
| 0 | + | <managed-bean-name>logIn</managed-bean-name> |
|---|
| 0 | + | <managed-bean-class>autoshop.web.LogIn</managed-bean-class> |
|---|
| 0 | + | <managed-bean-scope>request</managed-bean-scope> |
|---|
| 0 | + | <managed-property> |
|---|
| 0 | + | <property-name>username</property-name> |
|---|
| 0 | + | <value>#{username}</value> |
|---|
| 0 | + | </managed-property> |
|---|
| 0 | + | <managed-property> |
|---|
| 0 | + | <property-name>password</property-name> |
|---|
| 0 | + | <value>#{password}</value> |
|---|
| 0 | + | </managed-property> |
|---|
| 0 | + | </managed-bean> |
|---|
| 0 | + | |
|---|
| 0 | + | <managed-bean> |
|---|
| 0 | + | <description>bean per la sessione</description> |
|---|
| 0 | + | <managed-bean-name>sessionBean</managed-bean-name> |
|---|
| 0 | + | <managed-bean-class>autoshop.web.SessionBean</managed-bean-class> |
|---|
| 0 | + | <managed-bean-scope>session</managed-bean-scope> |
|---|
| 0 | + | <managed-property> |
|---|
| 0 | + | <property-name>username</property-name> |
|---|
| 0 | + | <value>#{username}</value> |
|---|
| 0 | + | </managed-property> |
|---|
| 0 | + | <managed-property> |
|---|
| 0 | + | <property-name>isLoggato</property-name> |
|---|
| 0 | + | <value>#{isLoggato}</value> |
|---|
| 0 | + | </managed-property> |
|---|
| 0 | + | </managed-bean> |
|---|
| 0 | + | |
|---|
| 0 | + | ..... |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | logIn.java: |
|---|
| 0 | + | |
|---|
| 0 | + | package autoshop.web; |
|---|
| 0 | + | |
|---|
| 0 | + | import autoshop.ejb.facade.EjbUtenteFacade; |
|---|
| 0 | + | import autoshop.jpa.dominio.Utente; |
|---|
| 0 | + | import javax.faces.context.FacesContext; |
|---|
| 0 | + | import javax.naming.InitialContext; |
|---|
| 0 | + | |
|---|
| 0 | + | /** |
|---|
| 0 | + | * |
|---|
| 0 | + | * @author mill |
|---|
| 0 | + | */ |
|---|
| 0 | + | public class LogIn{ |
|---|
| 0 | + | |
|---|
| 0 | + | private String username; |
|---|
| 0 | + | private String password; |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | public String getUsername(){ |
|---|
| 0 | + | return this.username; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public void setUsername(String username){ |
|---|
| 0 | + | this.username=username; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public String getPassword(){ |
|---|
| 0 | + | return this.password; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public void setPassword(String password){ |
|---|
| 0 | + | this.password=password; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | public String loginAction(){ |
|---|
| 0 | + | String result=""; |
|---|
| 0 | + | EjbUtenteFacade euf=getEjbUtenteFacade(); |
|---|
| 0 | + | Utente utente=euf.effettuaLogin(this.getUsername(),this.getPassword()); |
|---|
| 0 | + | if(utente!=null){ |
|---|
| 0 | + | this.sessionUtil(); |
|---|
| 0 | + | result="success"; |
|---|
| 0 | + | } |
|---|
| 0 | + | else |
|---|
| 0 | + | result="error"; |
|---|
| 0 | + | return result; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | private EjbUtenteFacade getEjbUtenteFacade(){ |
|---|
| 0 | + | try{ |
|---|
| 0 | + | InitialContext ctx = new InitialContext(); |
|---|
| 0 | + | return (EjbUtenteFacade)ctx.lookup("AutoShop/EjbUtenteFacadeImpl/remote"); |
|---|
| 0 | + | }catch(Exception e){ |
|---|
| 0 | + | return null; |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | private void sessionUtil(){ |
|---|
| 0 | + | FacesContext context = FacesContext.getCurrentInstance(); |
|---|
| 0 | + | SessionBean sessionBean = (SessionBean)context.getApplication().evaluateExpressionGet(context,"#{sessionBean}",SessionBean.class); |
|---|
| 0 | + | sessionBean.setUsername(this.getUsername()); |
|---|
| 0 | + | sessionBean.setIsLoggato(true); |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | SessionBean.java |
|---|
| 0 | + | |
|---|
| 0 | + | package autoshop.web; |
|---|
| 0 | + | |
|---|
| 0 | + | /** |
|---|
| 0 | + | * |
|---|
| 0 | + | * @author mill |
|---|
| 0 | + | */ |
|---|
| 0 | + | public class SessionBean { |
|---|
| 0 | + | |
|---|
| 0 | + | private String username; |
|---|
| 0 | + | private boolean isLoggato; |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | public String getUsername(){ |
|---|
| 0 | + | return username; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public void setUsername(String username){ |
|---|
| 0 | + | this.username=username; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public boolean getIsLoggato(){ |
|---|
| 0 | + | return this.isLoggato; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public void setIsLoggato(boolean isLoggato){ |
|---|
| 0 | + | this.isLoggato=isLoggato; |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | SessionUtility.java |
|---|
| 0 | + | |
|---|
| 0 | + | package autoshop.utility; |
|---|
| 0 | + | |
|---|
| 0 | + | import autoshop.web.SessionBean; |
|---|
| 0 | + | import javax.faces.context.FacesContext; |
|---|
| 0 | + | |
|---|
| 0 | + | /** |
|---|
| 0 | + | * |
|---|
| 0 | + | * @author mill |
|---|
| 0 | + | */ |
|---|
| 0 | + | public class SessionUtility { |
|---|
| 0 | + | |
|---|
| 0 | + | public static SessionBean getSessionBean(){ |
|---|
| 0 | + | return (SessionBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("sessionBean"); |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | ControlloPermessiListener.java |
|---|
| 0 | + | |
|---|
| 0 | + | /* |
|---|
| 0 | + | * To change this template, choose Tools | Templates |
|---|
| 0 | + | * and open the template in the editor. |
|---|
| 0 | + | */ |
|---|
| 0 | + | |
|---|
| 0 | + | package autoshop.utility; |
|---|
| 0 | + | |
|---|
| 0 | + | import javax.faces.application.NavigationHandler; |
|---|
| 0 | + | import javax.faces.context.FacesContext; |
|---|
| 0 | + | import javax.faces.event.PhaseEvent; |
|---|
| 0 | + | import javax.faces.event.PhaseId; |
|---|
| 0 | + | import javax.faces.event.PhaseListener; |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| 0 | + | public class ControlloPermessiListener implements PhaseListener { |
|---|
| 0 | + | |
|---|
| 0 | + | public void afterPhase(PhaseEvent event) { |
|---|
| 0 | + | FacesContext fc = event.getFacesContext(); |
|---|
| 0 | + | //controllo se sono nella pagina di login |
|---|
| 0 | + | boolean loginpage = fc.getViewRoot().getViewId().lastIndexOf("logIn") > -1 ? true : false; |
|---|
| 0 | + | //se non sono nella pagina di login e se non sono loggato |
|---|
| 0 | + | if (!loginpage && !SessionUtility.getSessionBean().getIsLoggato()) { |
|---|
| 0 | + | NavigationHandler nh = fc.getApplication().getNavigationHandler(); |
|---|
| 0 | + | nh.handleNavigation(fc, null, "logout"); |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public void beforePhase(PhaseEvent event) { |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | public PhaseId getPhaseId() { |
|---|
| 0 | + | return PhaseId.RESTORE_VIEW; |
|---|
| 0 | + | } |
|---|
| 0 | + | } |
|---|
| 0 | + | |
|---|
| 0 | + | |
|---|
| ... | |
|---|