代码之家  ›  专栏  ›  技术社区  ›  Ketan

为什么Spring boot安全性基本身份验证速度慢?

  •  2
  • Ketan  · 技术社区  · 7 年前

    以前我启用了无状态身份验证,后来禁用了,但性能仍然很差。此服务托管在Docker容器中。

    下面是我的安全配置。

    @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());
        }
    }
    

    如果我遗漏了什么,请告诉我。

    关于硬件:服务主机有Intel Xeon E5,16 GB内存。它承载4个Spring引导服务,每个服务分配2 GB,运行在Docker容器中。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Marcin Bukowiecki    6 年前

    您正在创建 BCryptPasswordEncoder SecureRandom . 所以每次你 encode 你的密码 BCrypt 将创建的新实例 (这是一个CPU指令操作,需要生成盐)。你可以检查 BCrypt.class 源代码。

    public static String gensalt(int log_rounds) {
        return gensalt(log_rounds, new SecureRandom());
    }
    
    public static String gensalt() {
        return gensalt(10);
    }
    
    public static String gensalt(int log_rounds, SecureRandom random) {
        ...
    }
    

    BCryptPasswordEncoder.class

        if (this.random != null) {
            salt = BCrypt.gensalt(this.strength, this.random);
        } else {
            salt = BCrypt.gensalt(this.strength);
        }
    

    所以使用 public BCryptPasswordEncoder(int strength, SecureRandom random) 安全随机 每次使用实例比一直使用同一实例更安全。

        2
  •  1
  •   senjin.hajrulahovic    6 年前

    较慢的散列函数不会对可用性造成很大影响,但可以更好地防止暴力攻击。

    https://security.stackexchange.com/questions/150620/what-is-the-purpose-of-slowing-down-the-calculation-of-a-password-hash