I-Exemple JSF authentfication avec primefaces:
Tout d'abord nous allons declarer trois attributs dans la classe LoginBean qui est un managedBean.
private String username;
private String password;
private boolean loggedIn;
- créer la methode doLogin
public String doLogin() {
if (username.equals("test") && password.equals("test")) {
loggedIn = true;
return "home";
}
FacesMessage msg = new FacesMessage("Login error!", "ERROR MSG");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, msg);
return "/login.xhtml";
}
2-creer dans login.xhtml le formulaire suivant:
<h:form id="login-form">
<h:messages />
<h:panelGrid columns="2">
<h:outputText value="username:"/>
<p:inputText value="#{loginBean.username}" id="username"/>
<h:outputText value="password:"/>
<p:password value="#{loginBean.password}" id="password"/>
<p:commandButton id="button" value="Login" action="#{loginBean.doLogin}"/>
</h:panelGrid>
</h::form>
3-Dans la page home.xhtml nous allons developper le code suivant:
<h:form>
<p:panel header="Accueil">
<p>Bienvneue</p>
<h:outputText value="#{loginBean.username}"/>
<p:commandButton action="#{loginBean.doLogout}" value="Déconnecter"/>
</p:panel>
</h:form>
3-le boutton Déconnecter va appeler la methode suivante dans LoginBean
public String doLogout() {
// Set the paremeter indicating that user is logged in to falseloggedIn = false;
FacesMessage msg = new FacesMessage("Déconnexion avec success!", "INFO MSG");
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, msg);
return "/login.xhtml";
}
private String password;
private boolean loggedIn;
- créer la methode doLogin
public String doLogin() {
if (username.equals("test") && password.equals("test")) {
loggedIn = true;
return "home";
}
FacesMessage msg = new FacesMessage("Login error!", "ERROR MSG");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, msg);
return "/login.xhtml";
}
<h:form id="login-form">
<h:messages />
<h:panelGrid columns="2">
<h:outputText value="username:"/>
<p:inputText value="#{loginBean.username}" id="username"/>
<h:outputText value="password:"/>
<p:password value="#{loginBean.password}" id="password"/>
<p:commandButton id="button" value="Login" action="#{loginBean.doLogin}"/>
</h:panelGrid>
</h::form>
3-Dans la page home.xhtml nous allons developper le code suivant:
<h:form>
<p:panel header="Accueil">
<p>Bienvneue</p>
<h:outputText value="#{loginBean.username}"/>
<p:commandButton action="#{loginBean.doLogout}" value="Déconnecter"/>
</p:panel>
</h:form>
3-le boutton Déconnecter va appeler la methode suivante dans LoginBean
public String doLogout() {
// Set the paremeter indicating that user is logged in to falseloggedIn = false;
FacesMessage msg = new FacesMessage("Déconnexion avec success!", "INFO MSG");
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, msg);
return "/login.xhtml";
}
3-Dans la page home.xhtml nous allons developper le code suivant:
<h:form>
<p:panel header="Accueil">
<p>Bienvneue</p>
<h:outputText value="#{loginBean.username}"/>
<p:commandButton action="#{loginBean.doLogout}" value="Déconnecter"/>
</p:panel>
</h:form>
3-le boutton Déconnecter va appeler la methode suivante dans LoginBean
public String doLogout() {
// Set the paremeter indicating that user is logged in to falseloggedIn = false;
FacesMessage msg = new FacesMessage("Déconnexion avec success!", "INFO MSG");
msg.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, msg);
return "/login.xhtml";
}
II-Filter dans JSF et primefaces:
-Tout d'abors ous allons creer notre classe Filter:
public class LoginFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
LoginBean loginBean = (LoginBean)((HttpServletRequest)request).getSession().getAttribute("loginBean");
if (loginBean == null || !loginBean.isLoggedIn()) {
String contextPath = ((HttpServletRequest)request).getContextPath();
((HttpServletResponse)response).sendRedirect(contextPath + "/login.xhtml");
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
dans le fichier Web.xml je dois ajouter la déclaration du filter et de son mapping:
</filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.examples.filters.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/secured/*</url-pattern>
</filter-mapping>
public class LoginFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
LoginBean loginBean = (LoginBean)((HttpServletRequest)request).getSession().getAttribute("loginBean");
if (loginBean == null || !loginBean.isLoggedIn()) {
String contextPath = ((HttpServletRequest)request).getContextPath();
((HttpServletResponse)response).sendRedirect(contextPath + "/login.xhtml");
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
</filter>
<filter-name>LoginFilter</filter-name>
<filter-name>LoginFilter</filter-name>
<filter-class>com.examples.filters.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/secured/*</url-pattern>
</filter-mapping>
No comments:
Post a Comment