|
| 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 | +} |
0 commit comments