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 java.util.List; |
19 | |
|
20 | |
import org.kuali.rice.core.api.config.property.Config; |
21 | |
import org.kuali.rice.core.api.config.property.ConfigContext; |
22 | |
import org.kuali.rice.kim.api.identity.principal.Principal; |
23 | |
import org.kuali.rice.kim.api.identity.IdentityService; |
24 | |
import org.kuali.student.common.util.security.UserWithId; |
25 | |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
26 | |
import org.springframework.security.core.GrantedAuthority; |
27 | |
import org.springframework.security.core.authority.AuthorityUtils; |
28 | |
import org.springframework.security.core.userdetails.User; |
29 | |
import org.springframework.security.core.userdetails.UserDetails; |
30 | |
import org.springframework.security.core.userdetails.UserDetailsService; |
31 | |
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class KSRiceDefaultUserDetailsService implements UserDetailsService{ |
40 | |
|
41 | 0 | private UserWithId ksuser = null; |
42 | 0 | private String password = ""; |
43 | |
|
44 | 0 | private boolean enabled = true; |
45 | 0 | private boolean nonlocked = true; |
46 | |
|
47 | 0 | private IdentityService identityService = null; |
48 | |
|
49 | |
|
50 | |
|
51 | 0 | private List<GrantedAuthority> authorities = |
52 | |
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_KS_ADMIN, ROLE_KS_USER"); |
53 | |
|
54 | |
@Override |
55 | |
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
56 | 0 | if(username==null || username.equals("")){ |
57 | 0 | throw new UsernameNotFoundException("Username cannot be null or empty"); |
58 | |
} |
59 | |
|
60 | 0 | Config config = ConfigContext.getCurrentContextConfig(); |
61 | 0 | String ksIgnoreRiceLogin = config.getProperty("ks.ignore.rice.login"); |
62 | |
|
63 | |
|
64 | |
|
65 | 0 | if(Boolean.valueOf(ksIgnoreRiceLogin) == true){ |
66 | 0 | return new User(username, password, enabled, true, true, nonlocked, authorities); |
67 | |
} |
68 | |
|
69 | 0 | Principal principal = null; |
70 | 0 | principal = identityService.getPrincipalByPrincipalName(username); |
71 | |
|
72 | |
String userId; |
73 | 0 | if (null != principal) { |
74 | 0 | username = principal.getPrincipalName(); |
75 | 0 | userId = principal.getPrincipalId(); |
76 | |
} else { |
77 | |
|
78 | |
|
79 | |
|
80 | 0 | throw new KimUserNotFoundException("Invalid username or password"); |
81 | |
} |
82 | 0 | ksuser = new UserWithId(username, password, enabled, true, true, nonlocked, authorities); |
83 | 0 | ksuser.setUserId(userId); |
84 | 0 | return ksuser; |
85 | |
} |
86 | |
|
87 | |
public UserDetails loadUserByUsernameAndToken(String username, UsernamePasswordAuthenticationToken authentication) throws UsernameNotFoundException { |
88 | 0 | if(username==null || username.equals("")){ |
89 | 0 | throw new UsernameNotFoundException("Username cannot be null or empty"); |
90 | |
} |
91 | |
|
92 | 0 | Config config = ConfigContext.getCurrentContextConfig(); |
93 | 0 | String ksIgnoreRiceLogin = config.getProperty("ks.ignore.rice.login"); |
94 | |
|
95 | |
|
96 | |
|
97 | 0 | if(Boolean.valueOf(ksIgnoreRiceLogin) == true){ |
98 | 0 | return null; |
99 | |
} |
100 | |
|
101 | 0 | password = (String)authentication.getCredentials(); |
102 | |
|
103 | 0 | Principal principal = null; |
104 | |
|
105 | 0 | principal = identityService.getPrincipalByPrincipalNameAndPassword(username, password); |
106 | |
String userId; |
107 | 0 | if (null != principal) { |
108 | 0 | username = principal.getPrincipalName(); |
109 | 0 | userId = principal.getPrincipalId(); |
110 | |
} else { |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | 0 | throw new KimUserNotFoundException("Invalid username or password"); |
116 | |
} |
117 | 0 | ksuser = new UserWithId(username, password, enabled, true, true, nonlocked, authorities); |
118 | 0 | ksuser.setUserId(userId); |
119 | 0 | return ksuser; |
120 | |
} |
121 | |
|
122 | |
public void setAuthorities(String[] roles) { |
123 | 0 | this.authorities = AuthorityUtils.createAuthorityList(roles); |
124 | 0 | } |
125 | |
|
126 | |
public void setIdentityService(IdentityService identityService) { |
127 | 0 | this.identityService = identityService; |
128 | 0 | } |
129 | |
} |