Springboot2的Security框架用的是5.0的,较之4.0的密码加密方式有了很大的改变.spring security 5中主推的加密方式为BCrypt,由于这种加密方式效率很低,属于慢加密,但是加密强度很高,现有的机器性能难以暴力破解,但是随着科技的进步,机器性能增强,破解这种加密方式也会成为可能,但是加密方式也会不断更新.
废话说到这里,由于性能要求,对该加密登录的压测,只能达到50-80qps,这无疑对高并发登录是不能接受的,所以我们需要改掉这种加密方式,我们选择了MD5的加密.修改之前的安全配置如下.
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* 全局用户信息
*
* @param auth
* 认证管理
* @throws Exception
* 用户认证异常信息
*/
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
/**
* 认证管理
*
* @return 认证管理对象
* @throws Exception
* 认证异常信息
*/
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* http安全配置
*
* @param http
* http安全对象
* @throws Exception
* http安全异常信息
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers(PermitAllUrl.permitAllUrl()).permitAll().anyRequest().authenticated().and()
.httpBasic().and().csrf().disable();
}
}
修改后,我们只使用MD5进行加密
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public UserDetailsService userDetailsService;
// @Bean
// public BCryptPasswordEncoder bCryptPasswordEncoder() {
// return new BCryptPasswordEncoder();
// }
@Bean
PasswordEncoder passwordEncoder(){
String idForEncode = "MD5";
Map encoders = new HashMap<>();
encoders.put(idForEncode, new BCryptPasswordEncoder());
encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
PasswordEncoder delegatingPasswordEncoder =
new DelegatingPasswordEncoder(idForEncode, encoders);
return delegatingPasswordEncoder;
// return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
/**
* 全局用户信息
*
* @param auth
* 认证管理
* @throws Exception
* 用户认证异常信息
*/
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
/**
* 认证管理
*
* @return 认证管理对象
* @throws Exception
* 认证异常信息
*/
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* http安全配置
*
* @param http
* http安全对象
* @throws Exception
* http安全异常信息
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers(PermitAllUrl.permitAllUrl()).permitAll().anyRequest().authenticated().and()
.httpBasic().and().csrf().disable();
}
}
这里需要注意的是,由于只使用了MD5进行加密,在访问的时候,假设我们使用的是superadmin用户名,密码123456
http://192.168.5.182:36178/oauth/token?grant_type=password&client_id=system&client_secret=system&scope=app&username=superadmin&password=123456
这个client_id=system&client_secret=system在数据库中是有对应的,之前的对应是用BCrypt加密的,所以在oauth_client_details表中,是这样的
这里面的client_secret的值其实是system字符串的BCrypt加密结果,我们需要改成如下所示
这个值同样也是system,不过是由MD5加密的结果,主要需要加前缀{MD5}.这样在app_user表中,信息如下
密码是由123456的MD5加密结果.这样在访问中,我们就可以获取访问的结果access_token
{"access_token":"65411be1-b7be-46e5-8d72-c0624da33ed7","token_type":"bearer","refresh_token":"43eb498f-d431-4c96-a5b5-3bc007c9c189","expires_in":25690,"scope":"app"}
但值得注意的是,有时候这样修改后未必能得到我们所需要的结果,那是因为在redis中的缓存问题,由于前一次在BCrypt的加密下已经有了缓存,所以会报错,我们需要手工清除一下Redis中的缓存,这样就会重新建立MD5加密后的缓存,就不会再出现问题了.经过压测,结果已经达到了2000左右的qps,已经大大高于50-80了.