|
1 | 1 | package com.baeldung.passwordstorage; |
2 | 2 |
|
3 | | -import org.springframework.beans.factory.annotation.Autowired; |
4 | 3 | import org.springframework.context.annotation.Bean; |
5 | 4 | import org.springframework.context.annotation.Configuration; |
6 | | -import org.springframework.security.authentication.AuthenticationEventPublisher; |
7 | 5 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
8 | 6 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 7 | +import org.springframework.security.core.userdetails.User; |
9 | 8 | import org.springframework.security.core.userdetails.UserDetailsService; |
10 | 9 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
11 | 10 | import org.springframework.security.crypto.password.DelegatingPasswordEncoder; |
12 | | -import org.springframework.security.crypto.password.MessageDigestPasswordEncoder; |
| 11 | +import org.springframework.security.crypto.password.NoOpPasswordEncoder; |
13 | 12 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 13 | +import org.springframework.security.crypto.password.StandardPasswordEncoder; |
14 | 14 | import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder; |
| 15 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
15 | 16 |
|
| 17 | +import java.util.Collections; |
16 | 18 | import java.util.HashMap; |
17 | 19 | import java.util.Map; |
18 | 20 |
|
19 | 21 | @Configuration |
20 | 22 | public class PasswordStorageWebSecurityConfigurer extends WebSecurityConfigurerAdapter { |
21 | 23 |
|
22 | | - private final AuthenticationEventPublisher eventPublisher; |
23 | | - private final UserDetailsService userDetailsService; |
24 | | - |
25 | | - @Autowired |
26 | | - public PasswordStorageWebSecurityConfigurer(AuthenticationEventPublisher eventPublisher, UserDetailsService userDetailsService) { |
27 | | - this.eventPublisher = eventPublisher; |
28 | | - this.userDetailsService = userDetailsService; |
29 | | - } |
30 | 24 |
|
31 | 25 | @Override |
32 | 26 | protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
33 | 27 | auth.eraseCredentials(false) // 4 |
34 | | - .authenticationEventPublisher(eventPublisher) |
35 | | - .userDetailsService(userDetailsService) |
| 28 | + .userDetailsService(getUserDefaultDetailsService()) |
36 | 29 | .passwordEncoder(passwordEncoder()); |
37 | 30 | } |
38 | 31 |
|
| 32 | + @Bean |
| 33 | + public UserDetailsService getUserDefaultDetailsService() { |
| 34 | + User testUser = new User("baeldung", "{noop}SpringSecurity5", Collections.emptyList()); |
| 35 | + return new InMemoryUserDetailsManager(testUser); |
| 36 | + } |
| 37 | + |
39 | 38 | @Bean |
40 | 39 | public PasswordEncoder passwordEncoder() { |
41 | 40 | // set up the list of supported encoders and their prefixes |
42 | | - String encodingId = "bcrypt"; |
| 41 | + PasswordEncoder defaultEncoder = new StandardPasswordEncoder(); |
43 | 42 | Map<String, PasswordEncoder> encoders = new HashMap<>(); |
44 | | - encoders.put(encodingId, new BCryptPasswordEncoder()); |
| 43 | + encoders.put("bcrypt", new BCryptPasswordEncoder()); |
45 | 44 | encoders.put("scrypt", new SCryptPasswordEncoder()); |
46 | | - encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256")); |
| 45 | + encoders.put("noop", NoOpPasswordEncoder.getInstance()); |
47 | 46 |
|
48 | | - DelegatingPasswordEncoder delegatingPasswordEncoder = new DelegatingPasswordEncoder(encodingId, encoders); |
49 | | - delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(encoders.get(encodingId)); |
| 47 | + DelegatingPasswordEncoder passwordEncoder = new DelegatingPasswordEncoder("bcrypt", encoders); |
| 48 | + passwordEncoder.setDefaultPasswordEncoderForMatches(defaultEncoder); |
50 | 49 |
|
51 | | - return delegatingPasswordEncoder; |
| 50 | + return passwordEncoder; |
52 | 51 | } |
53 | 52 |
|
54 | 53 | } |
0 commit comments