Я недавно обновил приложение Spring Boot с версии 1.5 до версии 2.0.3.
Это приложение с методами, предоставляемыми как конечные точки REST и защищенными базовой HTTP-аутентификацией. Имена пользователей и пароли жестко закодированы в файле свойств, загруженном приложением.
После обновления время отклика увеличилось почти на 200 мс, и 98% времени обработки запроса тратится в BasicAuthenticationFilter.doFilter ().
В частности, время, затрачиваемое на кодирование пароля в запросе, сравнивается с паролем, указанным в конфигурации.
Вот выдержка из класса SecurityConfig:
@EnableWebSecurity
@PropertySource("classpath:auth/auth.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${user.name}")
private String userName;
@Value("${user.password}")
private String userPassword;
@Value("${user.roles}")
private String[] userRoles;
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
UserDetails user = User.withUsername(userName).password(encoder.encode(userPassword)).roles(userRoles).build();
auth.inMemoryAuthentication().withUser(user);
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
//make sure that the basic authentication header is always required
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
//set the correct authentication entry point to make sure the 401 response is in JSON format
http.exceptionHandling().authenticationEntryPoint(new AuthenticationEntryPoint());
//enable http basic authentication
http.httpBasic();
//make sure authentication is required for all requests except /actuator/info
http.authorizeRequests().antMatchers("/actuator/info").permitAll().anyRequest().authenticated();
http.authorizeRequests().antMatchers("/actuator/**").hasAnyRole("MONITOR", "ACTUATOR");
//disable the form login since this is a REST API
http.formLogin().disable();
//disable csrf since this is a REST API
http.csrf().disable();
}
}
Чтобы убедиться, что это произошло из-за обновления Spring Boot, я локально отменил изменения и провел несколько тестов. Время отклика было разделено на 4.
Я уже пробовал несколько вещей, но ни одна из них не улучшила время отклика:
Могу ли я сделать что-нибудь для ускорения фильтра аутентификации?