Springboot 2-OAuth 2修改登录加密方式

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表中,是这样的

bcf33be439fc24789da13a16beaaa37084f.jpg

这里面的client_secret的值其实是system字符串的BCrypt加密结果,我们需要改成如下所示

3e7def0a62027151afd6ba88d202502fd9b.jpg

这个值同样也是system,不过是由MD5加密的结果,主要需要加前缀{MD5}.这样在app_user表中,信息如下

87f500320b52ac2f922b624fc094e6fb925.jpg

密码是由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了.

转载于:https://my.oschina.net/u/3768341/blog/1931573

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值