sockeqwe
sockeqwe

Reputation: 15929

User Authentication with Hibernate

im new in hibernate. I would like to build a simple autentication / login system in java with hibernate.

So let's say, i have a class User

public class User {

private int id;
private String username;
private String passwordHash;
...
}

Now i have a DAO to store a new User, and to get all users (as a list). Now im wondering, if its possible to get a list of users without the passwordHash field (for security reason)?

It would be nice if it was a question of configuration.

An other idea would be to split the User class into

public class User {

private int id;
private String username;
...
}


public class UserWithPassword extends User {

private String passwordHash;
...
}

So i could use UserWithPassword to store a new user into the database and use the User class to query the list of all users (without password).

Any other suggestion?

Upvotes: 0

Views: 2203

Answers (2)

you can use java.util.List temp = hibernateTemplate.find("select u from user u ");

you can take all user from temp;

but if you want authenticate,you can use spring security,i suggest

Upvotes: 1

Thom
Thom

Reputation: 15052

Your split class won't work because you have to link a class to Hibernate.

Your DAO doesn't have to return the class itself. You can write an HQL query such:

select username
from User

See?

Then your DAO would have a method like public Collection getUserNames()

Upvotes: 1

Related Questions