Advertisement
Guest User

Untitled

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