1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import org .springframework .security .core .authority .AuthorityUtils ;
25
25
import org .springframework .security .core .authority .SimpleGrantedAuthority ;
26
26
import org .springframework .security .core .context .SecurityContextHolder ;
27
+ import org .springframework .security .core .userdetails .User ;
27
28
import org .springframework .security .core .userdetails .UserCache ;
28
29
import org .springframework .security .core .userdetails .UserDetails ;
29
30
import org .springframework .security .core .userdetails .cache .NullUserCache ;
@@ -145,15 +146,51 @@ protected void initDao() throws ApplicationContextException {
145
146
// ~ UserDetailsManager implementation
146
147
// ==============================================================================
147
148
149
+ /**
150
+ * Executes the SQL <tt>usersByUsernameQuery</tt> and returns a list of UserDetails
151
+ * objects. There should normally only be one matching user.
152
+ */
153
+ protected List <UserDetails > loadUsersByUsername (String username ) {
154
+ return getJdbcTemplate ().query (getUsersByUsernameQuery (), new String []{username },
155
+ (rs , rowNum ) -> {
156
+
157
+ String userName = rs .getString (1 );
158
+ String password = rs .getString (2 );
159
+ boolean enabled = rs .getBoolean (3 );
160
+
161
+ boolean accLocked = false ;
162
+ boolean accExpired = false ;
163
+ boolean credsExpired = false ;
164
+
165
+ if (rs .getMetaData ().getColumnCount () > 3 ) {
166
+ //NOTE: acc_locked, acc_expired and creds_expired are also to be loaded
167
+ accLocked = rs .getBoolean (4 );
168
+ accExpired = rs .getBoolean (5 );
169
+ credsExpired = rs .getBoolean (6 );
170
+ }
171
+ return new User (userName , password , enabled , !accExpired , !credsExpired , !accLocked ,
172
+ AuthorityUtils .NO_AUTHORITIES );
173
+ });
174
+ }
175
+
148
176
public void createUser (final UserDetails user ) {
149
177
validateUserDetails (user );
178
+
150
179
getJdbcTemplate ().update (createUserSql , new PreparedStatementSetter () {
180
+ @ Override
151
181
public void setValues (PreparedStatement ps ) throws SQLException {
152
182
ps .setString (1 , user .getUsername ());
153
183
ps .setString (2 , user .getPassword ());
154
184
ps .setBoolean (3 , user .isEnabled ());
155
- }
156
185
186
+ int paramCount = ps .getParameterMetaData ().getParameterCount ();
187
+ if (paramCount > 3 ) {
188
+ //NOTE: acc_locked, acc_expired and creds_expired are also to be inserted
189
+ ps .setBoolean (4 , !user .isAccountNonLocked ());
190
+ ps .setBoolean (5 , !user .isAccountNonExpired ());
191
+ ps .setBoolean (6 , !user .isCredentialsNonExpired ());
192
+ }
193
+ }
157
194
});
158
195
159
196
if (getEnableAuthorities ()) {
@@ -163,11 +200,25 @@ public void setValues(PreparedStatement ps) throws SQLException {
163
200
164
201
public void updateUser (final UserDetails user ) {
165
202
validateUserDetails (user );
203
+
166
204
getJdbcTemplate ().update (updateUserSql , new PreparedStatementSetter () {
205
+ @ Override
167
206
public void setValues (PreparedStatement ps ) throws SQLException {
168
207
ps .setString (1 , user .getPassword ());
169
208
ps .setBoolean (2 , user .isEnabled ());
170
- ps .setString (3 , user .getUsername ());
209
+
210
+ int paramCount = ps .getParameterMetaData ().getParameterCount ();
211
+ if (paramCount == 3 ) {
212
+ ps .setString (3 , user .getUsername ());
213
+ } else {
214
+ //NOTE: acc_locked, acc_expired and creds_expired are also updated
215
+ ps .setBoolean (3 , !user .isAccountNonLocked ());
216
+ ps .setBoolean (4 , !user .isAccountNonExpired ());
217
+ ps .setBoolean (5 , !user .isCredentialsNonExpired ());
218
+
219
+ ps .setString (6 , user .getUsername ());
220
+ }
221
+
171
222
}
172
223
});
173
224
0 commit comments