I have a Spring boot 2.0.1 service to which I added Basic authentication which uses BCrypt for hashing. But this service which used to give an average of 400 ms before adding Basic auth is now taking more than 1 second. I am using User details service which looks up the sent user name in a hash map and returns UserDetails. I tried reducing BCrypt rounds down to 4 but that didn't make much of a difference.
Earlier I had stateless authentication enabled which I later disabled but again performance stayed bad. This service is hosted in a Docker container.
Below is my Security config.
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private UserDetailsService userDetailsService;
@Autowired
public SecurityConfig(UserDetailsServiceImpl service) {
this.userDetailsService = service;
}
@Bean
public PasswordEncoder passwordEncoder() {
Map encoders = new HashMap<>();
encoders.put(BCRYPT_ID, new BCryptPasswordEncoder(BCRYPT_ROUNDS));
return new DelegatingPasswordEncoder(BCRYPT_ID,encoders);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.csrf().disable()
.httpBasic();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder());
}
}
Please let me know if I am missing something.
Update: I ran benchmarks and it looks like BCrypt encoder is making the application slow. I found some Stack Overflow answers discussing that BCrypt hash calculation is a blocking call.
About hardware: The service host machine has Intel Xeon E5, 16 GB memory. It hosts 4 Spring boot Services each assigned 2 GB running inside a Docker container.