View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.security;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  
28  import javax.annotation.Resource;
29  
30  import fr.ifremer.quadrige2.core.dao.administration.user.QuserJdbcDao;
31  import fr.ifremer.quadrige2.core.vo.administration.user.PrivilegeVO;
32  import org.springframework.context.annotation.Lazy;
33  import org.springframework.security.core.userdetails.UserDetails;
34  import org.springframework.security.core.userdetails.UserDetailsService;
35  import org.springframework.security.core.userdetails.UsernameNotFoundException;
36  import org.springframework.stereotype.Service;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  import java.util.List;
40  
41  /**
42   *
43   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
44   * @since 1.0
45   */
46  @Service("quadrige2UserDetailsService")
47  @Lazy
48  @Transactional
49  public class Quadrige2UserDetailsServiceImpl implements UserDetailsService {
50  
51      @Resource(name = "quserJdbcDao")
52      QuserJdbcDao quserJdbcDao;
53      
54      @Override
55      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
56          Integer userId = null;
57          
58          if (username == null) {
59  			throw new UsernameNotFoundException("Provided username is null");
60  		}
61          
62          try {
63              userId = quserJdbcDao.getUserIdByUsername(username);
64          } catch (Exception ex) {
65              throw new UsernameNotFoundException("User not found from username: " + username, ex);
66          }
67          
68          if (userId == null) {
69              throw new UsernameNotFoundException("User not found from username: " + username);
70          }
71  
72          // Get user privileges
73          List<PrivilegeVO> privileges = quserJdbcDao.getPrivilegesByUserId(userId);
74  
75  
76          
77          return new Quadrige2UserDetailsImpl(userId, privileges);
78      }
79      
80  }