Skip to content

Commit c46837f

Browse files
rwinchthomasdarimont
authored andcommitted
Add UserDetailsMapFactoryBean
Fixes spring-projectsgh-4804
1 parent 41d19b0 commit c46837f

File tree

2 files changed

+88
-26
lines changed

2 files changed

+88
-26
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.config.core.userdetails;
18+
19+
import org.springframework.beans.factory.FactoryBean;
20+
import org.springframework.lang.Nullable;
21+
import org.springframework.security.core.userdetails.User;
22+
import org.springframework.security.core.userdetails.UserDetails;
23+
import org.springframework.security.core.userdetails.memory.UserAttribute;
24+
import org.springframework.security.core.userdetails.memory.UserAttributeEditor;
25+
import org.springframework.util.Assert;
26+
27+
import java.util.ArrayList;
28+
import java.util.Collection;
29+
import java.util.Map;
30+
31+
/**
32+
* Creates a {@code Collection<UserDetails>} from a @{code Map} in the format of
33+
* <p>
34+
* <code>
35+
* username=password[,enabled|disabled],roles...
36+
* </code>
37+
* <p>
38+
* The enabled and disabled properties are optional with enabled being the default. For example:
39+
* <p>
40+
* <code>
41+
* user=password,ROLE_USER
42+
* admin=secret,ROLE_USER,ROLE_ADMIN
43+
* disabled_user=does_not_matter,disabled,ROLE_USER
44+
* </code>
45+
*
46+
* @author Rob Winch
47+
* @since 5.0
48+
*/
49+
public class UserDetailsMapFactoryBean implements FactoryBean<Collection<UserDetails>> {
50+
private final Map<String, String> userProperties;
51+
52+
public UserDetailsMapFactoryBean(Map<String, String> userProperties) {
53+
Assert.notNull(userProperties, "userProperties cannot be null");
54+
this.userProperties = userProperties;
55+
}
56+
57+
@Nullable
58+
@Override
59+
public Collection<UserDetails> getObject() throws Exception {
60+
Collection<UserDetails> users = new ArrayList<>(this.userProperties.size());
61+
62+
UserAttributeEditor editor = new UserAttributeEditor();
63+
for (Map.Entry<String, String> entry : this.userProperties.entrySet()) {
64+
String name = entry.getKey();
65+
String property = entry.getValue();
66+
editor.setAsText(property);
67+
UserAttribute attr = (UserAttribute) editor.getValue();
68+
if (attr == null) {
69+
throw new IllegalStateException("The entry with username '" + name
70+
+ "' and value '" + property + "' could not be converted to a UserDetails.");
71+
}
72+
UserDetails user = User.withUsername(name)
73+
.password(attr.getPassword())
74+
.disabled(!attr.isEnabled())
75+
.authorities(attr.getAuthorities())
76+
.build();
77+
users.add(user);
78+
} return users;
79+
}
80+
81+
@Nullable
82+
@Override
83+
public Class<?> getObjectType() {
84+
return Collection.class;
85+
}
86+
}

config/src/main/java/org/springframework/security/config/core/userdetails/UserDetailsResourceFactoryBean.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@
2121
import org.springframework.core.io.DefaultResourceLoader;
2222
import org.springframework.core.io.Resource;
2323
import org.springframework.core.io.ResourceLoader;
24-
import org.springframework.security.core.userdetails.User;
2524
import org.springframework.security.core.userdetails.UserDetails;
26-
import org.springframework.security.core.userdetails.memory.UserAttribute;
27-
import org.springframework.security.core.userdetails.memory.UserAttributeEditor;
2825
import org.springframework.security.util.InMemoryResource;
2926
import org.springframework.util.Assert;
3027

3128
import java.io.InputStream;
32-
import java.util.ArrayList;
3329
import java.util.Collection;
34-
import java.util.Enumeration;
30+
import java.util.Map;
3531
import java.util.Properties;
3632

3733
/**
@@ -72,27 +68,7 @@ public Collection<UserDetails> getObject() throws Exception {
7268
try(InputStream in = resource.getInputStream()){
7369
userProperties.load(in);
7470
}
75-
76-
Collection<UserDetails> users = new ArrayList<>(userProperties.size());
77-
Enumeration<?> names = userProperties.propertyNames();
78-
UserAttributeEditor editor = new UserAttributeEditor();
79-
80-
while (names.hasMoreElements()) {
81-
String name = (String) names.nextElement();
82-
String property = userProperties.getProperty(name);
83-
editor.setAsText(property);
84-
UserAttribute attr = (UserAttribute) editor.getValue();
85-
if(attr == null) {
86-
throw new IllegalStateException("The entry with username '" + name + "' and value '" + property + "' could not be converted to a UserDetails.");
87-
}
88-
UserDetails user = User.withUsername(name)
89-
.password(attr.getPassword())
90-
.disabled(!attr.isEnabled())
91-
.authorities(attr.getAuthorities())
92-
.build();
93-
users.add(user);
94-
}
95-
return users;
71+
return new UserDetailsMapFactoryBean((Map) userProperties).getObject();
9672
}
9773

9874
@Override

0 commit comments

Comments
 (0)