【问题标题】:Spring BasicAuthenticationFilter.doFilter() very slowSpring BasicAuthenticationFilter.doFilter() 非常慢
【发布时间】:2018-11-29 15:14:14
【问题描述】:

在我的 Spring Boot 2.0.5 应用程序中,我使用 basic-auth 来保护 REST-API。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
   @Override
   protected void configure(final HttpSecurity http) throws Exception {
       http.authorizeRequests().antMatchers("/api/**").authenticated().and()
       .httpBasic().and().csrf().disable();
   }
   @Bean
   public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
    final InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
    manager.createUser("user").password("pwd").roles("roles").build()));
    return manager;
    }
}

其中一个 API 被 Feign 客户端(另一个 Spring Boot 应用程序)与 Feigns BasicAuthRequestInterceptor 一起使用。

NewRelic 报告 Spring 的 BasicAuthenticationFilter.doFilter() 使用最多 10 秒 (!) 的每个 Web 服务请求。

这是一个巨大的开销。这里出了什么问题,我该怎么做才能加快速度?

【问题讨论】:

  • 嗨@Saimonsez - 我也面临同样的问题。有什么解决办法吗?

标签: spring basic-authentication feign


【解决方案1】:

我也面临同样的问题。对于 150 个并发线程,我的 TPS 为 40。我们分析了这个问题,发现问题出在 Bcrypt 编码算法上。将强度更改为较低的值。如下所示。

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder(4);
}

BCryptPasswordEncoder 的默认强度为 10。将其减少到 4。同时更新密码存储中的密码。

另外,如果您使用 HikariCP,请按照此link 微调最大池大小。

目前,我的 TPS 是 300+ 2 核 CUP 300 个并发线程

希望这会有所帮助。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2018-03-22
    • 2015-05-22
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多