|
| 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.crypto.factory; |
| 18 | + |
| 19 | +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| 20 | +import org.springframework.security.crypto.password.DelegatingPasswordEncoder; |
| 21 | +import org.springframework.security.crypto.password.NoOpPasswordEncoder; |
| 22 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 23 | +import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; |
| 24 | +import org.springframework.security.crypto.password.StandardPasswordEncoder; |
| 25 | +import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +/** |
| 31 | + * Used for creating {@link PasswordEncoder} instances |
| 32 | + * @author Rob Winch |
| 33 | + * @since 5.0 |
| 34 | + */ |
| 35 | +public class PasswordEncoderFactories { |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a {@link DelegatingPasswordEncoder} with default mappings. Additional |
| 39 | + * mappings may be added and the encoding will be updated to conform with best |
| 40 | + * practices. However, due to the nature of {@link DelegatingPasswordEncoder} the |
| 41 | + * updates should not impact users. The mappings current are: |
| 42 | + * |
| 43 | + * <ul> |
| 44 | + * <li>noop - {@link NoOpPasswordEncoder}</li> |
| 45 | + * <li>pbkdf2 - {@link Pbkdf2PasswordEncoder} (Also used for encoding)</li> |
| 46 | + * <li>scrypt - {@link SCryptPasswordEncoder}</li> |
| 47 | + * <li>sha256 - {@link StandardPasswordEncoder}</li> |
| 48 | + * </ul> |
| 49 | + * |
| 50 | + * @return the {@link PasswordEncoder} to use |
| 51 | + */ |
| 52 | + public static PasswordEncoder createDelegatingPasswordEncoder() { |
| 53 | + String encodingId = "bcrypt"; |
| 54 | + Map<String,PasswordEncoder> encoders = new HashMap<>(); |
| 55 | + encoders.put(encodingId, new BCryptPasswordEncoder()); |
| 56 | + encoders.put("noop", NoOpPasswordEncoder.getInstance()); |
| 57 | + encoders.put("pbkdf2", new Pbkdf2PasswordEncoder()); |
| 58 | + encoders.put("scrypt", new SCryptPasswordEncoder()); |
| 59 | + encoders.put("sha256", new StandardPasswordEncoder()); |
| 60 | + |
| 61 | + return new DelegatingPasswordEncoder(encodingId, encoders); |
| 62 | + } |
| 63 | + |
| 64 | + private PasswordEncoderFactories() {} |
| 65 | +} |
0 commit comments