11/*
2- * Copyright 2002-2016 the original author or authors.
2+ * Copyright 2002-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2424import org .springframework .security .core .authority .AuthorityUtils ;
2525import org .springframework .security .core .authority .SimpleGrantedAuthority ;
2626import org .springframework .security .core .context .SecurityContextHolder ;
27+ import org .springframework .security .core .userdetails .User ;
2728import org .springframework .security .core .userdetails .UserCache ;
2829import org .springframework .security .core .userdetails .UserDetails ;
2930import org .springframework .security .core .userdetails .cache .NullUserCache ;
@@ -145,15 +146,51 @@ protected void initDao() throws ApplicationContextException {
145146 // ~ UserDetailsManager implementation
146147 // ==============================================================================
147148
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+
148176 public void createUser (final UserDetails user ) {
149177 validateUserDetails (user );
178+
150179 getJdbcTemplate ().update (createUserSql , new PreparedStatementSetter () {
180+ @ Override
151181 public void setValues (PreparedStatement ps ) throws SQLException {
152182 ps .setString (1 , user .getUsername ());
153183 ps .setString (2 , user .getPassword ());
154184 ps .setBoolean (3 , user .isEnabled ());
155- }
156185
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+ }
157194 });
158195
159196 if (getEnableAuthorities ()) {
@@ -163,11 +200,25 @@ public void setValues(PreparedStatement ps) throws SQLException {
163200
164201 public void updateUser (final UserDetails user ) {
165202 validateUserDetails (user );
203+
166204 getJdbcTemplate ().update (updateUserSql , new PreparedStatementSetter () {
205+ @ Override
167206 public void setValues (PreparedStatement ps ) throws SQLException {
168207 ps .setString (1 , user .getPassword ());
169208 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+
171222 }
172223 });
173224
0 commit comments