1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.security.spring; |
17 | |
|
18 | |
import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo; |
19 | |
import org.kuali.student.common.util.security.UserWithId; |
20 | |
import org.springframework.security.providers.UsernamePasswordAuthenticationToken; |
21 | |
import org.springframework.security.userdetails.UserDetails; |
22 | |
import org.springframework.security.userdetails.UsernameNotFoundException; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class KSRiceDefaultUserDetailsService extends KSDefaultUserDetailsService{ |
31 | |
|
32 | |
|
33 | |
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
34 | 0 | return this.getUserDetails(username, null); |
35 | |
} |
36 | |
|
37 | |
public UserDetails loadUserByUsernameAndToken(String username, UsernamePasswordAuthenticationToken authentication) throws UsernameNotFoundException { |
38 | 0 | return this.getUserDetails(username, authentication); |
39 | |
} |
40 | |
|
41 | |
protected UserDetails getUserDetails(String username, UsernamePasswordAuthenticationToken authentication) throws UsernameNotFoundException { |
42 | 0 | if(username==null || username.equals("")){ |
43 | 0 | throw new UsernameNotFoundException("Username cannot be null or empty"); |
44 | |
} |
45 | |
|
46 | 0 | KimPrincipalInfo kimPrincipalInfo = null; |
47 | 0 | kimPrincipalInfo = getIdentityService().getPrincipalByPrincipalName(username); |
48 | |
|
49 | |
String userId; |
50 | |
String password; |
51 | 0 | if (null != kimPrincipalInfo) { |
52 | 0 | username = kimPrincipalInfo.getPrincipalName(); |
53 | 0 | userId = kimPrincipalInfo.getPrincipalId(); |
54 | 0 | if(authentication == null){ |
55 | 0 | password = kimPrincipalInfo.getPassword(); |
56 | |
}else{ |
57 | |
|
58 | |
|
59 | 0 | password = (String)authentication.getCredentials(); |
60 | |
} |
61 | |
} else { |
62 | |
|
63 | |
|
64 | |
|
65 | 0 | throw new KimUserNotFoundException("Invalid username or password"); |
66 | |
} |
67 | |
|
68 | 0 | password = (password == null ? "":password); |
69 | 0 | UserWithId ksuser = new UserWithId(username, password, super.enabled, true, true, super.nonlocked, getGrantedAuthority(userId)); |
70 | 0 | ksuser.setUserId(userId); |
71 | 0 | return ksuser; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
} |