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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

i初学者   2020-05-02 我要评论
# 场景 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应该是一个速度与安全兼顾的值。 有了长度没有了效率也不行,所以建议使用默认长度就好。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们