|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.userdetails.jdbc; |
|
17 |
| |
|
18 |
| import java.sql.ResultSet; |
|
19 |
| import java.sql.SQLException; |
|
20 |
| import java.sql.Types; |
|
21 |
| import java.util.List; |
|
22 |
| |
|
23 |
| import javax.sql.DataSource; |
|
24 |
| |
|
25 |
| import org.acegisecurity.GrantedAuthority; |
|
26 |
| import org.acegisecurity.GrantedAuthorityImpl; |
|
27 |
| import org.acegisecurity.userdetails.User; |
|
28 |
| import org.acegisecurity.userdetails.UserDetails; |
|
29 |
| import org.acegisecurity.userdetails.UserDetailsService; |
|
30 |
| import org.acegisecurity.userdetails.UsernameNotFoundException; |
|
31 |
| import org.apache.commons.logging.Log; |
|
32 |
| import org.apache.commons.logging.LogFactory; |
|
33 |
| import org.springframework.context.ApplicationContextException; |
|
34 |
| import org.springframework.dao.DataAccessException; |
|
35 |
| import org.springframework.jdbc.core.SqlParameter; |
|
36 |
| import org.springframework.jdbc.core.support.JdbcDaoSupport; |
|
37 |
| import org.springframework.jdbc.object.MappingSqlQuery; |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService { |
|
69 |
| |
|
70 |
| |
|
71 |
| public static final String DEF_USERS_BY_USERNAME_QUERY = "SELECT username,password,enabled FROM users WHERE username = ?"; |
|
72 |
| public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "SELECT username,authority FROM authorities WHERE username = ?"; |
|
73 |
| private static final Log logger = LogFactory.getLog(JdbcDaoImpl.class); |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| protected MappingSqlQuery authoritiesByUsernameMapping; |
|
78 |
| protected MappingSqlQuery usersByUsernameMapping; |
|
79 |
| private String authoritiesByUsernameQuery; |
|
80 |
| private String rolePrefix = ""; |
|
81 |
| private String usersByUsernameQuery; |
|
82 |
| private boolean usernameBasedPrimaryKey = true; |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
10
| public JdbcDaoImpl() {
|
|
87 |
10
| usersByUsernameQuery = DEF_USERS_BY_USERNAME_QUERY;
|
|
88 |
10
| authoritiesByUsernameQuery = DEF_AUTHORITIES_BY_USERNAME_QUERY;
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
1
| public void setAuthoritiesByUsernameQuery(String queryString) {
|
|
104 |
1
| authoritiesByUsernameQuery = queryString;
|
|
105 |
| } |
|
106 |
| |
|
107 |
1
| public String getAuthoritiesByUsernameQuery() {
|
|
108 |
1
| return authoritiesByUsernameQuery;
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
1
| public void setRolePrefix(String rolePrefix) {
|
|
122 |
1
| this.rolePrefix = rolePrefix;
|
|
123 |
| } |
|
124 |
| |
|
125 |
1
| public String getRolePrefix() {
|
|
126 |
1
| return rolePrefix;
|
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
0
| public void setUsernameBasedPrimaryKey(boolean usernameBasedPrimaryKey) {
|
|
144 |
0
| this.usernameBasedPrimaryKey = usernameBasedPrimaryKey;
|
|
145 |
| } |
|
146 |
| |
|
147 |
0
| public boolean isUsernameBasedPrimaryKey() {
|
|
148 |
0
| return usernameBasedPrimaryKey;
|
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
1
| public void setUsersByUsernameQuery(String usersByUsernameQueryString) {
|
|
166 |
1
| this.usersByUsernameQuery = usersByUsernameQueryString;
|
|
167 |
| } |
|
168 |
| |
|
169 |
1
| public String getUsersByUsernameQuery() {
|
|
170 |
1
| return usersByUsernameQuery;
|
|
171 |
| } |
|
172 |
| |
|
173 |
8
| public UserDetails loadUserByUsername(String username)
|
|
174 |
| throws UsernameNotFoundException, DataAccessException { |
|
175 |
8
| List users = usersByUsernameMapping.execute(username);
|
|
176 |
| |
|
177 |
8
| if (users.size() == 0) {
|
|
178 |
1
| throw new UsernameNotFoundException("User not found");
|
|
179 |
| } |
|
180 |
| |
|
181 |
7
| UserDetails user = (UserDetails) users.get(0);
|
|
182 |
| |
|
183 |
7
| List dbAuths = authoritiesByUsernameMapping.execute(user.getUsername());
|
|
184 |
| |
|
185 |
7
| if (dbAuths.size() == 0) {
|
|
186 |
1
| throw new UsernameNotFoundException("User has no GrantedAuthority");
|
|
187 |
| } |
|
188 |
| |
|
189 |
6
| GrantedAuthority[] arrayAuths = {};
|
|
190 |
| |
|
191 |
6
| addCustomAuthorities(user.getUsername(), dbAuths);
|
|
192 |
| |
|
193 |
6
| arrayAuths = (GrantedAuthority[]) dbAuths.toArray(arrayAuths);
|
|
194 |
| |
|
195 |
6
| String returnUsername = user.getUsername();
|
|
196 |
| |
|
197 |
6
| if (!usernameBasedPrimaryKey) {
|
|
198 |
0
| returnUsername = username;
|
|
199 |
| } |
|
200 |
| |
|
201 |
6
| return new User(returnUsername, user.getPassword(), user.isEnabled(),
|
|
202 |
| true, true, true, arrayAuths); |
|
203 |
| } |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
6
| protected void addCustomAuthorities(String username, List authorities) {}
|
|
214 |
| |
|
215 |
7
| protected void initDao() throws ApplicationContextException {
|
|
216 |
7
| initMappingSqlQueries();
|
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
7
| protected void initMappingSqlQueries() {
|
|
224 |
7
| this.usersByUsernameMapping = new UsersByUsernameMapping(getDataSource());
|
|
225 |
7
| this.authoritiesByUsernameMapping = new AuthoritiesByUsernameMapping(getDataSource());
|
|
226 |
| } |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| protected class AuthoritiesByUsernameMapping extends MappingSqlQuery { |
|
234 |
7
| protected AuthoritiesByUsernameMapping(DataSource ds) {
|
|
235 |
7
| super(ds, authoritiesByUsernameQuery);
|
|
236 |
7
| declareParameter(new SqlParameter(Types.VARCHAR));
|
|
237 |
7
| compile();
|
|
238 |
| } |
|
239 |
| |
|
240 |
9
| protected Object mapRow(ResultSet rs, int rownum)
|
|
241 |
| throws SQLException { |
|
242 |
9
| String roleName = rolePrefix + rs.getString(2);
|
|
243 |
9
| GrantedAuthorityImpl authority = new GrantedAuthorityImpl(roleName);
|
|
244 |
| |
|
245 |
9
| return authority;
|
|
246 |
| } |
|
247 |
| } |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| protected class UsersByUsernameMapping extends MappingSqlQuery { |
|
253 |
7
| protected UsersByUsernameMapping(DataSource ds) {
|
|
254 |
7
| super(ds, usersByUsernameQuery);
|
|
255 |
7
| declareParameter(new SqlParameter(Types.VARCHAR));
|
|
256 |
7
| compile();
|
|
257 |
| } |
|
258 |
| |
|
259 |
7
| protected Object mapRow(ResultSet rs, int rownum)
|
|
260 |
| throws SQLException { |
|
261 |
7
| String username = rs.getString(1);
|
|
262 |
7
| String password = rs.getString(2);
|
|
263 |
7
| boolean enabled = rs.getBoolean(3);
|
|
264 |
7
| UserDetails user = new User(username, password, enabled, true,
|
|
265 |
| true, true, |
|
266 |
| new GrantedAuthority[] {new GrantedAuthorityImpl("HOLDER")}); |
|
267 |
| |
|
268 |
7
| return user;
|
|
269 |
| } |
|
270 |
| } |
|
271 |
| } |