Skip to content

Commit cabd0a5

Browse files
committed
UserDetailsPasswordService
Issue: gh-2778
1 parent 02b857d commit cabd0a5

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.core.userdetails;
18+
19+
/**
20+
* An API for changing a {@link UserDetails} password.
21+
* @author Rob Winch
22+
* @since 5.1
23+
*/
24+
public interface UserDetailsPasswordService {
25+
26+
/**
27+
* Modify the specified user's password. This should change the user's password in the
28+
* persistent user repository (datbase, LDAP etc).
29+
*
30+
* @param user the user to modify the password for
31+
* @param newPassword the password to change to
32+
* @return the updated UserDetails with the new password
33+
*/
34+
UserDetails updatePassword(UserDetails user, String newPassword);
35+
}

core/src/main/java/org/springframework/security/provisioning/InMemoryUserDetailsManager.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.security.core.context.SecurityContextHolder;
3131
import org.springframework.security.core.userdetails.User;
3232
import org.springframework.security.core.userdetails.UserDetails;
33+
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
3334
import org.springframework.security.core.userdetails.UsernameNotFoundException;
3435
import org.springframework.security.core.userdetails.memory.UserAttribute;
3536
import org.springframework.security.core.userdetails.memory.UserAttributeEditor;
@@ -45,7 +46,8 @@
4546
* @author Luke Taylor
4647
* @since 3.1
4748
*/
48-
public class InMemoryUserDetailsManager implements UserDetailsManager {
49+
public class InMemoryUserDetailsManager implements UserDetailsManager,
50+
UserDetailsPasswordService {
4951
protected final Log logger = LogFactory.getLog(getClass());
5052

5153
private final Map<String, MutableUserDetails> users = new HashMap<>();
@@ -138,6 +140,14 @@ public void changePassword(String oldPassword, String newPassword) {
138140
user.setPassword(newPassword);
139141
}
140142

143+
@Override
144+
public UserDetails updatePassword(UserDetails user, String newPassword) {
145+
String username = user.getUsername();
146+
MutableUserDetails mutableUser = this.users.get(username);
147+
mutableUser.setPassword(newPassword);
148+
return mutableUser;
149+
}
150+
141151
public UserDetails loadUserByUsername(String username)
142152
throws UsernameNotFoundException {
143153
UserDetails user = users.get(username.toLowerCase());
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.provisioning;
18+
19+
import org.junit.Test;
20+
import org.springframework.security.core.userdetails.PasswordEncodedUser;
21+
import org.springframework.security.core.userdetails.UserDetails;
22+
23+
import static org.assertj.core.api.Assertions.*;
24+
25+
/**
26+
* @author Rob Winch
27+
* @since 5.1
28+
*/
29+
public class InMemoryUserDetailsManagerTests {
30+
private final UserDetails user = PasswordEncodedUser.user();
31+
32+
private InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(this.user);
33+
34+
@Test
35+
public void changePassword() {
36+
String newPassword = "newPassword";
37+
this.manager.updatePassword(this.user, newPassword);
38+
assertThat(this.manager.loadUserByUsername(this.user.getUsername()).getPassword()).isEqualTo(newPassword);
39+
}
40+
}

0 commit comments

Comments
 (0)