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应该是一个速度与安全兼顾的值。
有了长度没有了效率也不行,所以建议使用默认长度就好。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM