Containerization feature of SO
[so.git] / common / src / main / java / org / onap / so / security / UserDetailsServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.security;
22
23 import org.springframework.boot.context.properties.ConfigurationProperties;
24 import org.springframework.security.core.userdetails.User;
25 import org.springframework.security.core.userdetails.UserDetails;
26 import org.springframework.security.core.userdetails.UserDetailsService;
27 import org.springframework.security.core.userdetails.UsernameNotFoundException;
28 import java.util.List;
29
30 @ConfigurationProperties(prefix = "spring.security")
31 public class UserDetailsServiceImpl implements UserDetailsService {
32
33         private List<UserCredentials> usercredentials;
34
35         public List<UserCredentials> getUsercredentials() {
36                 return usercredentials;
37         }
38
39         public void setUsercredentials(List<UserCredentials> usercredentials) {
40                 this.usercredentials = usercredentials;
41         }
42
43         @Override
44         public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
45
46                 for (int i = 0; usercredentials != null && i < usercredentials.size(); i++) {
47                         if (usercredentials.get(i).getUsername().equals(username)) {
48                                 return User.withUsername(username).password(usercredentials.get(i).getPassword())
49                                                 .roles(usercredentials.get(i).getRole()).build();
50                         }
51                 }
52
53                 throw new UsernameNotFoundException("User not found, username: " + username);
54         }
55
56 }