Guest User

Untitled

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