SpringSecurity Section 3 : Defining InMemory User
@Configuration public class ProjectSecurityConfig { @Bean SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests((requests) -> requests .requestMatchers("/myAccount", "/myBalance", "/myLoans", "/myCards").authenticated() .requestMatchers("/notices", "/contact", "/error").permitAll()); http.formLogin(withDefaults()); http.httpBasic(withDefaults()); return http.build(); } @Bean public UserDetailsService userDetailsService() { UserDetails user = User.withUsername("user").password("{noop}EazyBytes@12345").authorities("read").build(); UserDetails admin = User.withUsername("admin") .password("{bcrypt}$2a$12$88.f6upbBvy0okEa7OfHFuorV29qeK.sVbB9VQ6J6dWM1bW6Qef8m") .authorities("admin").build(); return new InMemoryUserDetailsManager(user, admin); } @Bean public PasswordEncoder passwordEncoder() { return PasswordEncoderFactories.createDelegatingPasswordEncoder(); } /** * From Spring Security 6.3 version * @return */ @Bean public CompromisedPasswordChecker compromisedPasswordChecker() { return new HaveIBeenPwnedRestApiPasswordChecker(); } }
please note UserDetailsService is an interface. UserDetailsManager is another interface that extends this interface. It has methods like
- createUser
- deleteUser
- updateUser
- changePassword
- InMemoryUserDetailsManager
- JDBCDetailsManager
{noop} is a password prefix used to specify that the password is in plain text and should not be encoded. The NoOpPasswordEncoder is a deprecated class that performs no hashing, making it highly insecure and strictly for testing or legacy systems only. NoOpPasswordEncoder in a few ways: - Using the
{noop}Prefix (Recommended Testing Approach): The standard and most common way to use plain-text passwords for testing is to prefix the password string with{noop}. Spring's defaultDelegatingPasswordEncoderrecognizes this prefix and handles the comparison correctly. - Declaring a
NoOpPasswordEncoderBean (Not Recommended): You can explicitly create aNoOpPasswordEncoderbean, though this is discouraged due to its deprecated status.
- Insecure for Production: Passwords stored or transmitted in plain text are vulnerable to security breaches.
- Deprecated: The
NoOpPasswordEncoderclass itself is marked as@Deprecatedin the Spring Security API documentation to emphasize its insecurity. - Modern Alternatives: Spring highly recommends using adaptive one-way encoding functions like
BCryptPasswordEncoder,Pbkdf2PasswordEncoder, orSCryptPasswordEncoderfor production environments. The defaultDelegatingPasswordEncodersupports all these options and handles password upgrades seamlessly
Password Encoder
https://stackoverflow.com/questions/46999940/spring-boot-how-to-specify-the-passwordencoder
https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-encoding
https://medium.com/@nishanthp155/spring-security-password-encoders-with-hashing-103008534e8d
inside a @configuration bean, add
@Bean public PasswordEncoder passwordEncoder() { return PasswordEncoderFactories.createDelegatingPasswordEncoder(); }
see line number 35 for BCrypt https://github.com/eazybytes/spring-security/blob/4.x.x/section3/springsecsection3/src/main/java/com/eazybytes/config/ProjectSecurityConfig.java
Compromised Password Checker
this calls a standard API
- https://api.pwnedpasswords.com/range/
- https://api.pwnedpasswords.com/range/e38ad
- https://haveibeenpwned.com/api/v3
in END this API simply returns a boolean , if this password has been leaked or not.
you get above error when you register user , hence inforcing strong password
LEFT deep dive videos
ok
Comments
Post a Comment