Bonjour à tous. J'ai un problème avec l'établissement d'une session dans une petite application web dans laquelle je voudrais permettre aux utilisateurs de s'authentifier et de conserver des informations de session tout au long de leur activité jusqu'à leur déconnexion. J'utilise JSF+Richfaces pour la couche présentation, des JavaBeans de Spring pour la couche service et Hibernate pour la couche persistance avec le SGBD MySQL.
Lors de ma tentative de création de session l'objet session reste toujours vide malgré l'ajout d'attribut. J'ai peut-être pas utilisé les moyens les plus simples, c'est un peu du bidouillage, mais j'espère que les commentaires pourront vous aider à m'aider
Voici le code du Managed Bean "UserBean":
Dans ce code, juste après l'ajout d'attribut dans la session créée, j'effectue un affichage console de l'attribut que j'ai nommé "userID" mais j'obtiens la valeur "null" à chaque essai.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 package pfe.stonesoft.web; import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.annotation.PostConstruct; import javax.faces.context.FacesContext; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component("userBean") @Scope("session") public class UserBean implements Serializable { /** * */ private static final long serialVersionUID = 1L; @Autowired private transient EmployeService employeService; @Autowired private transient ConnexionService connexionService; /* * Attributs du managed bean */ private Employes currentEmploye = new Employes(); private Connexion currentConnexion = new Connexion(); private List<Employes> employeList = new ArrayList<Employes>(); private HttpSession session; @PostConstruct public void init(){ employeList = employeService.findAll(); } //Methode de verification de l'authentification des utilisateurs public String authentification(){ //J'trouve dans la table connexion l'enregistrement correspondant au login List<Connexion> listCon = connexionService.findByLogin(currentConnexion.getLogin()); /* *Si on a un résultat on enregistre dans l'objet courant les informations *complémentaires */ if(listCon.size()!=0) { currentConnexion.setId(listCon.get(0).getId()); currentConnexion.setStatut(listCon.get(0).getStatut()); currentConnexion.setEmployeses(listCon.get(0).getEmployeses()); currentConnexion.setPartenaireses(listCon.get(0).getPartenaireses()); if(employeList.isEmpty()) //On cherche l'employé qui possède les paramètres de connexion validé Iterator<Employes> itemp = currentConnexion.getEmployeses().iterator(); while(itemp.hasNext()){ Employes emp = (Employes)itemp.next(); if(emp.getConnexion().getId()==currentConnexion.getId()) { this.setCurrentEmploye(emp); System.out.println("emp: "+emp.getNom()+" current: "+currentEmploye.getNom()); } } //Création de la session session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true); session.setAttribute("userId", currentEmploye.getId()); System.out.println(session.getAttribute("userID")); return "login"; } return "null"; } /* * Zone des accesseurs des attributs du managed bean */ public List<Employes> getEmployeList() { return employeList; } public void setEmployeList(List<Employes> employeList) { this.employeList = employeList; } public Employes getCurrentEmploye() { return currentEmploye; } public void setCurrentEmploye(Employes currentEmploye) { this.currentEmploye = currentEmploye; } public Connexion getCurrentConnexion() { return currentConnexion; } public void setCurrentConnexion(Connexion currentConnexion) { this.currentConnexion = currentConnexion; } }
Le fichier "faces-config.xml" est configuré comme ceci:
le fichier "web.xml" est ainsi:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 <?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd" version="1.2"> <application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application> <navigation-rule> <from-view-id>*</from-view-id> <navigation-case> <from-outcome>login</from-outcome> <to-view-id>/employe.jsp</to-view-id> </navigation-case> <navigation-case> <from-outcome>null</from-outcome> <to-view-id>/notlogin.jsp</to-view-id> </navigation-case> </navigation-rule> </faces-config>
Le fichier "application-context.xml"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>pfe1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:application-context.xml</param-value> </context-param> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <filter> <filter-name>RichFaces Filter</filter-name> <filter-class>org.ajax4jsf.Filter</filter-class> </filter> <filter-mapping> <filter-name>RichFaces Filter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> </web-app>
La page JSP de login:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/sgbdpfe" /> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="annotatedClasses"> <list> <value>pfe.stonesoft.model.Connexion</value> <value>pfe.stonesoft.model.Employes</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager"/> <context:annotation-config/> <context:component-scan base-package="pfe.stonesoft"/> </beans>
J'ai essayé de brasser large pour que vous ayez une idée de ce que j'ai mis en place. Il y a un élément que je dois certainement avoir loupé...ou même plusieurs. Si vous pouviez m'aider ça me ferait vraiment plaisir parce que j'ne trouve aucune précision utile dans tous les tutos que j'ai pu consulter
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%> <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%> <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Authentification</title> </head> <body> <br> <p></p> <f:view> <h:form id="mainForm"> <rich:spacer height="100px"></rich:spacer> <center><rich:panel style="border:2;width:30%;text-align:center"> <h:panelGrid columns="3"> <h:graphicImage value="/image/userauth.png" styleClass="pic" /> <h:outputLabel value="Login" for="login" /> <h:inputText id="login" value="#{userBean.currentConnexion.login}" required="true" requiredMessage="champs obligatoire" /> <rich:message for="login" style="color: red" /> <h:outputLabel value="Mot de passe" for="password" /> <h:inputSecret id="password" value="#{userBean.currentConnexion.password}" required="true" requiredMessage="champs obligatoire" /> <rich:message for="password" style="color: red" /> </h:panelGrid> <rich:spacer width="100px" height="30px"></rich:spacer> <h:commandButton value="Connexion" action="#{userBean.authentification}" /> </rich:panel></center> </h:form> </f:view> </body> </html>![]()
Partager