Skip to content

Commit 52560b5

Browse files
committed
PasswordEncodedUser
Fixes gh-4680
1 parent 7c95c88 commit 52560b5

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2017 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+
import java.util.function.Function;
20+
21+
/**
22+
* @author Rob Winch
23+
* @since 5.0
24+
*/
25+
public class PasswordEncodedUser {
26+
private static final UserDetails USER = withUsername("user").password("password").roles("USER").build();
27+
28+
private static final UserDetails ADMIN = withUsername("admin").password("password").roles("USER","ADMIN").build();
29+
30+
public static UserDetails user() {
31+
return User.withUserDetails(USER).build();
32+
}
33+
34+
public static UserDetails admin() {
35+
return User.withUserDetails(ADMIN).build();
36+
}
37+
38+
public static User.UserBuilder builder() {
39+
return User.builder().passwordEncoder(passwordEncoder());
40+
}
41+
42+
public static User.UserBuilder withUsername(String username) {
43+
return builder().username(username);
44+
}
45+
46+
public static User.UserBuilder withUserDetails(UserDetails userDetails) {
47+
return User.withUserDetails(userDetails)
48+
.passwordEncoder(passwordEncoder());
49+
}
50+
51+
private static Function<String,String> passwordEncoder() {
52+
return rawPassword -> "{noop}" + rawPassword;
53+
}
54+
55+
private PasswordEncodedUser() {}
56+
}

0 commit comments

Comments
 (0)