Guest User

Untitled

a guest
Aug 16th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package web.service.common;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.List;
  10. import javax.annotation.Resource;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.dao.DataAccessException;
  13. import org.springframework.security.core.GrantedAuthority;
  14. import org.springframework.security.core.userdetails.User;
  15. import org.springframework.security.core.userdetails.UserDetails;
  16. import org.springframework.security.core.userdetails.UserDetailsService;
  17. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  18. import org.springframework.stereotype.Repository;
  19. import org.springframework.stereotype.Service;
  20. import web.dao.UsersDAO;
  21. import web.dao.impl.jpa.UsersDAOImpl;
  22. import web.entity.Users;
  23.  
  24.  
  25. /**
  26.  *
  27.  * @author syncsys
  28.  */
  29. @Service
  30. public class CustomUserDetailsService implements UserDetailsService{
  31.  
  32.     //@Resource
  33.    @Autowired
  34.    private UsersDAO userDAO;
  35.  
  36.  
  37.  /**
  38.   * Retrieves a springUser record containing the springUser's credentials and access.
  39.   */
  40.  public UserDetails loadUserByUsername(String email)
  41.    throws UsernameNotFoundException, DataAccessException {
  42.    
  43.    
  44.   // Declare a null Spring User
  45.   UserDetails springUser = null;
  46.  
  47.    
  48.   try {
  49.     System.out.println("the email passed from CustomUserDetailsService in method loadUserByUsername is: " +email);
  50.    // Search database for a springUser that matches the specified email
  51.    // You can provide a custom DAO to access your persistence layer
  52.    // Or use JDBC to access your database
  53.    // DbUser is our custom domain springUser. This is not the same as Spring's User
  54.       System.out.println("debug ---- 1");
  55.    Users dbUser = userDAO.getUserByLoginId(email);
  56.    
  57.    // Populate the Spring User object with details from the dbUser
  58.    // Here we just pass the email, password, and access level
  59.    // getAuthorities() will translate the access level to the correct role type
  60.  System.out.println("debug ---- 2");
  61.    springUser =  new User(
  62.      dbUser.getEmail(),
  63.      dbUser.getPassword().toLowerCase(),
  64.      true,
  65.      true,
  66.      true,
  67.      true,
  68.      //getAuthorities(dbUser.getAccess()) );
  69.      getAuthorities(2) );
  70.  System.out.println("debug ---- 3");
  71.   } catch (Exception e) {
  72.    System.out.println("print Error in retrieving user");
  73.    e.printStackTrace();
  74.     System.out.println(e.getMessage());
  75.    throw new UsernameNotFoundException("Error in retrieving user");
  76.   }
  77.    System.out.println("debug ---- 4");
  78.   // Return springUser to Spring for processing.
  79.   // Take note we're not the one evaluating whether this springUser is authenticated or valid
  80.   // We just merely retrieve a springUser that matches the specified email
  81.   return springUser;
  82.  }
  83.  
  84.  /**
  85.   * Retrieves the correct ROLE type depending on the access level, where access level is an Integer.
  86.   * Basically, this interprets the access value whether it's for a regular springUser or admin.
  87.   *
  88.   * @param access an integer value representing the access of the springUser
  89.   * @return collection of granted authorities
  90.   */
  91.   public Collection<GrantedAuthority> getAuthorities(Integer access) {
  92.    // Create a list of grants for this springUser
  93.    List<GrantedAuthority> authList = (List<GrantedAuthority>) new ArrayList<GrantedAuthority>(2);
  94.    
  95.    // All users are granted with ROLE_USER access
  96.    // Therefore this springUser gets a ROLE_USER by default
  97.    System.out.println("Grant ROLE_USER to this user");
  98.    authList.add(new GrantedAuthorityImpl("ROLE_USER"));
  99.    
  100.    // Check if this springUser has admin access
  101.    // We interpret Integer(1) as an admin springUser
  102.    
  103. //   if ( access.compareTo(1) == 0) {
  104. //    // User has admin access
  105. //    logger.debug("Grant ROLE_ADMIN to this user");
  106. //    authList.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
  107. //   }
  108.  
  109.    // Return list of granted authorities
  110.    return authList;
  111.    }
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment