Jitendra Khedar
Jitendra Khedar

Reputation: 129

Authentication for same username and different password using spring

I have a web application. Only users can be entered into application with username and password those are created by different clients. Means users with same username can enter into application with different password. Right now my code return all users with same username but accepts only user at top position. gives error for remaining users.

public User findbyUserName(String username)
        { 
        Criteria crit = getSessionFactory().getCurrentSession().createCriteria(User.class);
        crit.add( Restrictions.eq("username", username));
        List<User> list=crit.list();
        if(list!=null && list.size()>0)
             return (User)list.get(0);
        else
             return null;
            }

Please help me in this.

Upvotes: 0

Views: 309

Answers (1)

Marginean Vlad
Marginean Vlad

Reputation: 329

If you have same user name but with different passwords you can modify findByUserName to return the whole list of users

public List<User> findbyUserName(String username)
{
    Criteria crit = getSessionFactory().getCurrentSession().createCriteria(User.class);
    crit.add(Restrictions.eq("username",username));
    return crit.list();
}

or you can make your search specific by username and password

public User findbyUserNameAndPassword(String username,String password)
{
    Criteria crit =getSessionFactory().getCurrentSession().createCriteria(User.class);
    crit.add(Restrictions.eq("username",username);
    crit.add(Restrictions.eq("password",password);
    try{
    return (User)crit.uniqueResult();
    }
    catch(Exception ex){
       //you have same user with same password twice
    }
}  

Upvotes: 1

Related Questions