开发者

How to use own database as authentication in spring security

开发者 https://www.devze.com 2023-02-18 15:06 出处:网络
I have class regsitration with field email and password. I have class regsitrationService public Registration searchUser(String email)

I have class regsitration with field email and password.

I have class regsitrationService

public Registration searchUser(String email)
{
      logger.debug("Getting Email");
      Session session = sessionFactory.getCurrentSession();
      String query = "FROM Registration WHERE email=:email";
      Query query2 = session.createQuery(query);
      query2.setString("email",email);
      return (Registration) query2.uniqueResult(); 
}

I have applied the spring security using the online example http://krams915.blogspot.com/2010/12/sprin开发者_StackOverflow中文版g-security-mvc-integration_18.html

He has written this in Customuserdetailsservice

 private UserDAO userDAO = new UserDAO();

 /**
  * Retrieves a user record containing the user's credentials and access.
  */
 public UserDetails loadUserByUsername(String username)
   throws UsernameNotFoundException, DataAccessException {

  // Declare a null Spring User
  UserDetails user = null;

  try {

   // Search database for a user that matches the specified username
   // You can provide a custom DAO to access your persistence layer
   // Or use JDBC to access your database
   // DbUser is our custom domain user. This is not the same as Spring's User
   DbUser dbUser = userDAO.searchDatabase(username);

   // Populate the Spring User object with details from the dbUser
   // Here we just pass the username, password, and access level
   // getAuthorities() will translate the access level to the correct role type

   user =  new User(
     dbUser.getUsername(),
     dbUser.getPassword().toLowerCase(),
     true,
     true,
     true,
     true,
     getAuthorities(dbUser.getAccess()) );

  } catch (Exception e) {
   logger.error("Error in retrieving user");
   throw new UsernameNotFoundException("Error in retrieving user");
  }

The example database given in the sample is working fine , but i am confused how to use my Registration and RegistrationService class to authenticate users from that username and password

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号