Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
968 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Findby Login and password Myeclipse Mysql

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Findby Login and password Myeclipse Mysql

n°2158984
smile88
Posté le 03-10-2012 à 14:46:39  profilanswer
 

bonjour,
j'espère bien que vous m'aider mes amis et merci d'avance.
tout d'abord j’éclaire l'environnement logiciel:
ma base donnée est MYSQL et j’utilise MYEclipse 10 et j'ai intégré les frameworks struts et hibernate
j'ai ajouté une methode qui test le Login et le password findByLoginAddPwd(String login,String pwd) à la classe UserDAO.java qui est déjà généré automatiquement avec myeclipse mais j'ai un erreur de developpement, veuillez bien m'aider merci
voisi le code de UserDAO.java

Code :
  1. package com.load.hibernate;
  2. import java.util.List;
  3. import org.apache.commons.logging.Log;
  4. import org.apache.commons.logging.LogFactory;
  5. import org.hibernate.LockMode;
  6. import org.hibernate.Query;
  7. import org.hibernate.criterion.Example;
  8. public class UserDAO extends BaseHibernateDAO  {
  9.   private static final Log log = LogFactory.getLog(UserDAO.class);
  10.  //property constants
  11. public static final String IP_USER = "ipUser";
  12. public static final String NOM_USER = "nomUser";
  13. public static final String PRENOM_USER = "prenomUser";
  14. public static final String EMAIL = "email";
  15. public static final String LOGIN = "login";
  16. public static final String PASSWORD = "password";
  17.     public void save(User transientInstance) {
  18.         log.debug("saving User instance" );
  19.         try {
  20.             getSession().save(transientInstance);
  21.             log.debug("save successful" );
  22.         } catch (RuntimeException re) {
  23.             log.error("save failed", re);
  24.             throw re;
  25.         }
  26.     }
  27.    
  28. public void delete(User persistentInstance) {
  29.         log.debug("deleting User instance" );
  30.         try {
  31.             getSession().delete(persistentInstance);
  32.             log.debug("delete successful" );
  33.         } catch (RuntimeException re) {
  34.             log.error("delete failed", re);
  35.             throw re;
  36.         }
  37.     }
  38.    
  39.     public User findById( java.lang.Integer id) {
  40.         log.debug("getting User instance with id: " + id);
  41.         try {
  42.             User instance = (User) getSession()
  43.                     .get("com.load.hibernate.User", id);
  44.             return instance;
  45.         } catch (RuntimeException re) {
  46.             log.error("get failed", re);
  47.             throw re;
  48.         }
  49.     }
  50.    
  51.    
  52.     public List findByExample(User instance) {
  53.         log.debug("finding User instance by example" );
  54.         try {
  55.             List results = getSession()
  56.                     .createCriteria("com.load.hibernate.User" )
  57.                     .add(Example.create(instance))
  58.             .list();
  59.             log.debug("find by example successful, result size: " + results.size());
  60.             return results;
  61.         } catch (RuntimeException re) {
  62.             log.error("find by example failed", re);
  63.             throw re;
  64.         }
  65.     }   
  66.    
  67.     public List findByProperty(String propertyName, Object value) {
  68.       log.debug("finding User instance with property: " + propertyName
  69.             + ", value: " + value);
  70.       try {
  71.          String queryString = "from User as model where model."
  72.                + propertyName + "= ?";
  73.          Query queryObject = getSession().createQuery(queryString);
  74.   queryObject.setParameter(0, value);
  75.   return queryObject.list();
  76.       } catch (RuntimeException re) {
  77.          log.error("find by property name failed", re);
  78.          throw re;
  79.       }
  80. }
  81. public List findByIpUser(Object ipUser
  82. ) {
  83.  return findByProperty(IP_USER, ipUser
  84.  );
  85. }
  86. public List findByNomUser(Object nomUser
  87. ) {
  88.  return findByProperty(NOM_USER, nomUser
  89.  );
  90. }
  91. public List findByPrenomUser(Object prenomUser
  92. ) {
  93.  return findByProperty(PRENOM_USER, prenomUser
  94.  );
  95. }
  96. public List findByEmail(Object email
  97. ) {
  98.  return findByProperty(EMAIL, email
  99.  );
  100. }
  101. public List findByLogin(Object login
  102. ) {
  103.  return findByProperty(LOGIN, login
  104.  );
  105. }
  106. public List findByPassword(Object password
  107. ) {
  108.  return findByProperty(PASSWORD, password
  109.  );
  110. }
  111. public List findAll() {
  112.  log.debug("finding all User instances" );
  113.  try {
  114.   String queryString = "from User";
  115.          Query queryObject = getSession().createQuery(queryString);
  116.    return queryObject.list();
  117.  } catch (RuntimeException re) {
  118.   log.error("find all failed", re);
  119.   throw re;
  120.  }
  121. }
  122.  public List findByLoginAddPwd(String login,String pwd){ 
  123.       log.debug("finding user instance by login and password" ); 
  124.       try
  125.           String queryString = "from User as user where user.login="+login+",user.password="+pwd; 
  126.           Query queryObject = getSession().createQuery(queryString); 
  127.           queryObject.setParameter(0, login); 
  128.           queryObject.setParameter(1, pwd); 
  129.           return queryObject.list(); 
  130.       } catch (RuntimeException re) { 
  131.           log.error("find all failed", re); 
  132.           throw re; 
  133.       }
  134.  }
  135.     public User merge(User detachedInstance) {
  136.         log.debug("merging User instance" );
  137.         try {
  138.             User result = (User) getSession()
  139.                     .merge(detachedInstance);
  140.             log.debug("merge successful" );
  141.             return result;
  142.         } catch (RuntimeException re) {
  143.             log.error("merge failed", re);
  144.             throw re;
  145.         }
  146.     }
  147.     public void attachDirty(User instance) {
  148.         log.debug("attaching dirty User instance" );
  149.         try {
  150.             getSession().saveOrUpdate(instance);
  151.             log.debug("attach successful" );
  152.         } catch (RuntimeException re) {
  153.             log.error("attach failed", re);
  154.             throw re;
  155.         }
  156.     }
  157.    
  158.     public void attachClean(User instance) {
  159.         log.debug("attaching clean User instance" );
  160.         try {
  161.             getSession().lock(instance, LockMode.NONE);
  162.             log.debug("attach successful" );
  163.         } catch (RuntimeException re) {
  164.             log.error("attach failed", re);
  165.             throw re;
  166.         }
  167.     }
  168. }


l'erreur:
javax.servlet.ServletException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: , near line 1, column 60 [from com.load.hibernate.User as user where user.login=admin,user.password=admin]
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

 
et la classe AuthentificationAction

Code :
  1. /*
  2. * Generated by MyEclipse Struts
  3. * Template path: templates/java/JavaClass.vtl
  4. */
  5. package com.load.struts.action;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.struts.action.Action;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import com.load.hibernate.User;
  13. import com.load.hibernate.UserDAO;
  14. import com.load.struts.form.AuthentifForm;
  15. import java.util.List;
  16. public class AuthentifAction extends Action {
  17. public ActionForward execute(ActionMapping mapping, ActionForm form,
  18.   HttpServletRequest request, HttpServletResponse response) {
  19.  AuthentifForm authentifForm = (AuthentifForm) form;// TODO Auto-generated method stub
  20.  String map="ko";
  21.  String login=authentifForm.getLogin();
  22.  String password = authentifForm.getPassword();
  23.  List test ;
  24.  UserDAO dao =new UserDAO();
  25.  test= dao.findByLoginAddPwd(login, password);
  26.  System.out.println("test size  = "+test.size());
  27.  if(test.size()!=0)
  28.  {System.out.println("utilisateur authentifié" );
  29.   map="ok";}
  30.  return mapping.findForward(map);
  31. }
  32. }

mood
Publicité
Posté le 03-10-2012 à 14:46:39  profilanswer
 

n°2160959
liouan
Posté le 22-10-2012 à 15:40:53  profilanswer
 

Code :
  1. String queryString = "from User as user where user.login="+login+",user.password="+pwd;
  2. Query queryObject = getSession().createQuery(queryString);
  3. queryObject.setParameter(0, login);
  4. queryObject.setParameter(1, pwd);


 
Tu mélanges requête préparée et requête concaténée.
 
Soit tu fais une requête concaténée (ça marche mais à ne surtout pas faire : tu t'exposes aux attaques par injection SQL !!!):

Code :
  1. String queryString = "from User as user where user.login="+login+",user.password="+pwd;


 
soit tu écris une requête préparée (la bonne méthode) :
 

Code :
  1. String queryString = "from User as user where user.login=?,user.password=?";
  2. Query queryObject = getSession().createQuery(queryString);
  3. queryObject.setParameter(0, login);
  4. queryObject.setParameter(1, pwd);


 
Dans le premier cas (requête concaténée), tu lui donnes la requête toute prête.
Dans le deuxième cas, tu lui donnes une requête préparée puis tu renseignes les paramètres
 
Le premier cas est très dangereux car si je mets  

Code :
  1. Login ="admin #" password="qqch"


la requête devient

Code :
  1. from User as user where user.login=admin #,user.password= qqch


Hop, je suis loggé en admin, quelque soit le mot de passe que je rentre
 
 

n°2161044
liouan
Posté le 23-10-2012 à 09:41:02  profilanswer
 

De plus, je ne connais pas la syntaxe spécifique à Hibernate mais la virgule entre "user.login=admin" et "user.password=admin" me parait hautement suspecte. J'y mettrais plutôt un "AND" (sinon, comment spécifierait-on un OR?)


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Findby Login and password Myeclipse Mysql

 

Sujets relatifs
Remplir une base MySQL avec une base SQLinstallation de MyEclipse sous ubuntu 12.04
Configuration apache - php -mysqlRequête mysql dans un fichier bash
Formulaire php et mysqlExclure enregistrements d'une requête MySQL
[MySQL Linux] Bases de données sur différentes partitions?PHP ne peut charger l'extension mysql,
[MySQL] Convertir une base en UTF8comment on peut backup les data de mysql
Plus de sujets relatifs à : Findby Login and password Myeclipse Mysql


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR