Spring Boot 整合 Spring Security,用户登录慢

场景

  1. Spring Boot + Spring Security搭建一个Web项目。
  2. 临时用了inMemoryAuthentication。
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/static/**", "/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login")
                .permitAll()
                .defaultSuccessUrl("/index")
                .and()
                .logout()
                .permitAll()

        ;
    }

    @Autowired
    public void configureGlobal(
            AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder(16))
                .withUser(User.withUsername("user").password(new BCryptPasswordEncoder(16).encode("654321")).roles("USER"));
    }

}

发现慢的原因是使用了BCryptPasswordEncoder加密方式,而且还new了两次v

解决方案

BCryptPasswordEncoder的默认加密长度是10,所有尝试了默认的长度密码,结果瞬间完成登录。
可见10与16的速度差别,10应该是一个速度与安全兼顾的值。
有了长度没有了效率也不行,所以建议使用默认长度就好。

猜你喜欢

转载自www.cnblogs.com/zhaojz/p/12818196.html