站长网 安全 什么是计时攻击?Spring Boot 中该怎样防御?

什么是计时攻击?Spring Boot 中该怎样防御?

松哥最近在研究 Spring Security 源码,发现了很多好玩的代码,抽空写几篇文章和小伙伴们分享一下。 很多人吐槽 Spring Security 比 Shiro 重量级,这个重量级不是凭空来的,重量有重量的好处,就是它提供了更为强大的防护功能。 比如松哥最近看到的一段代

松哥最近在研究 Spring Security 源码,发现了很多好玩的代码,抽空写几篇文章和小伙伴们分享一下。

很多人吐槽 Spring Security 比 Shiro 重量级,这个重量级不是凭空来的,重量有重量的好处,就是它提供了更为强大的防护功能。

比如松哥最近看到的一段代码:

protected final UserDetails retrieveUser(String username, 

  UsernamePasswordAuthenticationToken authentication) 

  throws AuthenticationException { 

 prepareTimingAttackProtection(); 

 try { 

  UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username); 

  if (loadedUser == null) { 

   throw new InternalAuthenticationServiceException( 

     "UserDetailsService returned null, which is an interface contract violation"); 

  } 

  return loadedUser; 

 } 

 catch (UsernameNotFoundException ex) { 

  mitigateAgainstTimingAttack(authentication); 

  throw ex; 

 } 

 catch (InternalAuthenticationServiceException ex) { 

  throw ex; 

 } 

 catch (Exception ex) { 

  throw new InternalAuthenticationServiceException(ex.getMessage(), ex); 

 } 

这段代码位于 DaoAuthenticationProvider 类中,为了方便大家理解,我来简单说下这段代码的上下文环境。

当用户提交用户名密码登录之后,Spring Security 需要根据用户提交的用户名去数据库中查询用户。

查到用户对象之后,再去比对从数据库中查到的用户密码和用户提交的密码之间的差异。

而上面这段代码就是 Spring Security 根据用户登录时传入的用户名去数据库中查询用户,并将查到的用户返回。方法中还有一个 authentication 参数,这个参数里边保存了用户登录时传入的用户名/密码信息。

那么这段代码有什么神奇之处呢?

我们来一行一行分析。

本文来自网络,不代表站长网立场,转载请注明出处:https://www.tzzz.com.cn/html/fuwuqi/anquan/2021/0701/12000.html

作者: dawei

【声明】:站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。
联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部