前言

在Web应用开发中,安全一直是非常重要的一个方面。在庞大的spring生态圈中,权限校验框架也是非常完善的。其中,spring security是非常好用的。今天记录一下在开发中遇到的一个spring-security相关的问题。

问题描述

使用spring security进行授权登录的时候,发现登录接口无法正常捕捉UsernameNotFoundException异常,捕捉到的一直是BadCredentialsException异常。我们的预期是:

  • UsernameNotFoundException -> 用户名错误
  • BadCredentialsException -> 密码错误

贴几个比较重要的代码:

1. 登录业务逻辑

@Service
public class AuthServiceImpl implements AuthService { @Autowired
private UserDetailsService userDetailsService; @Autowired
private AuthenticationManager authenticationManager; @Autowired
private JwtTokenUtil jwtTokenUtil; @Override
public JwtAuthenticationResponse login(String username, String password) {
//构造spring security需要的UsernamePasswordAuthenticationToken
UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(username, password);
//调用authenticationManager.authenticate(upToken)方法验证
//该方法将会执行UserDetailsService的loadUserByUsername验证用户名
//以及PasswordEncoder的matches方法验证密码
val authenticate = authenticationManager.authenticate(upToken);
JwtUser userDetails = (JwtUser) authenticate.getPrincipal();
val token = jwtTokenUtil.generateToken(userDetails);
return new JwtAuthenticationResponse(token, userDetails.getId(), userDetails.getUsername());
}
}

2. spring security 的UserDetailsService 实现类

@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository; @Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
AbstractUser abstractUser = userRepository.findByUsername(username);
//如果通过用户名找不到用户,则抛出UsernameNotFoundException异常
if (abstractUser == null) {
throw new UsernameNotFoundException(String.format("No abstractUser found with username '%s'.", username));
} else {
return JwtUserFactory.create(abstractUser);
}
}
}

3. 登录接口

try {
final JwtAuthenticationResponse jsonResponse = authService.login(authenticationRequest.getUsername(), authenticationRequest.getPassword());
//存入redis
redisService.setToken(jsonResponse.getToken());
return ok(jsonResponse);
} catch (BadCredentialsException e) {
//捕捉到BadCredentialsException,密码不正确
return forbidden(LOGIN_PASSWORD_ERROR, request);
} catch (UsernameNotFoundException e) {
//捕捉到UsernameNotFoundException,用户名不正确
return forbidden(LOGIN_USERNAME_ERROR, request);
}

在上述代码中,如果用户名错误,应该执行

catch (UsernameNotFoundException e) {
return forbidden(LOGIN_USERNAME_ERROR, request);
}

如果密码错误,应该执行

catch (BadCredentialsException e) {
return forbidden(LOGIN_PASSWORD_ERROR, request);
}

实际上,不管是抛出什么错,最后抓到的都是BadCredentialsException

问题定位

debug大法

断点

跟踪

经过步进法跟踪代码,发现问题所在,位于

AbstractUserDetailsAuthenticationProvider
public Authentication authenticate(Authentication authentication)

结论

  1. loadUserByUsername方法确实抛出了UsernameNotFoundException
  2. 走到AbstractUserDetailsAuthenticationProvider的authenticate方法的时候,如果hideUserNotFoundExceptions = true,直接就覆盖了UsernameNotFoundException异常并抛出BadCredentialsException异常,这也就解释了,为什么总是捕捉到BadCredentialsException异常

问题解决

既然已经找到了是因为hideUserNotFoundExceptions = true导致的问题,那把hideUserNotFoundExceptions = false不就完事了吗?

方案1

参考stackoverflow大神回答

修改WebSecurityConfig配置,添加AuthenticationProvider Bean

@Bean
public AuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(userDetailsService);
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
return daoAuthenticationProvider;
}

配置AuthenticationProvider Bean

@Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
.authenticationProvider(daoAuthenticationProvider());
}

方案2

由于以前项目中也是一样的技术栈,而且代码也差不多,登录这段逻辑可以说是完全相同,不过之前就一直都没有这个问题。反复查看之后发现,在login的代码有些不同

在

val authenticate = authenticationManager.authenticate(upToken);

前面还有一个

//执行UserDetailsService的loadUserByUsername验证用户名
userDetailsService.loadUserByUsername(authenticationRequest.getUsername());

该方法会直接抛出UsernameNotFoundException,而不走spring security的AbstractUserDetailsAuthenticationProvider,也就不存在被转换为BadCredentialsException了。

但是这个方案有个缺点,

如果验证用户名通过以后,再次调用

val authenticate = authenticationManager.authenticate(upToken);

还会再执行一遍

userDetailsService.loadUserByUsername(authenticationRequest.getUsername());

该操作是冗余的,产生了不必要的数据库查询工作。

推荐使用方案1

Spring-Security无法正常捕捉到UsernameNotFoundException异常的更多相关文章

  1. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  2. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

  3. Spring boot 前后台分离项目 怎么处理spring security 抛出的异常

    最近在开发一个项目 前后台分离的 使用 spring boot + spring security + jwt 实现用户登录权限控制等操作.但是 在用户登录的时候,怎么处理spring  securi ...

  4. Spring Security(05)——异常信息本地化

    Spring Security支持将展现给终端用户看的异常信息本地化,这些信息包括认证失败.访问被拒绝等.而对于展现给开发者看的异常信息和日志信息(如配置错误)则是不能够进行本地化的,它们是以英文硬编 ...

  5. Spring Security中异常上抛机制及对于转型处理的一些感悟

    在使用Spring Security的过程中,我们会发现框架内部按照错误及问题出现的场景,划分出了许许多多的异常,但是在业务调用时一般都会向外抛一个统一的异常出来,为什么要这样做呢,以及对于抛出来的异 ...

  6. Spring 捕捉校验参数异常并统一处理

    使用 @Validated ,@Valid ,@NotBlank 之类的,请自行百度,本文着重与捕捉校验失败信息并封装返回出去 参考: https://mp.weixin.qq.com/s/EaZxY ...

  7. 从源码看Spring Security之采坑笔记(Spring Boot篇)

    一:唠嗑 鼓捣了两天的Spring Security,踩了不少坑.如果你在学Spring Security,恰好又是使用的Spring Boot,那么给我点个赞吧!这篇博客将会让你了解Spring S ...

  8. spring security入门demo

    一.前言 因项目需要引入spring security权限框架,而之前也没接触过这个一门,于是就花了点时间弄了个小demo出来,说实话,刚开始接触这个确实有点懵,看网上资料写的权限大都是静态,即就是在 ...

  9. Spring Security 自定义登录认证(二)

    一.前言 本篇文章将讲述Spring Security自定义登录认证校验用户名.密码,自定义密码加密方式,以及在前后端分离的情况下认证失败或成功处理返回json格式数据 温馨小提示:Spring Se ...

随机推荐

  1. Paper慢慢读 - AB实验人群定向 Double Machine Learning

    Hetergeneous Treatment Effect旨在量化实验对不同人群的差异影响,进而通过人群定向/数值策略的方式进行差异化实验,或者对实验进行调整.Double Machine Learn ...

  2. 痞子衡嵌入式:ARM Cortex-M内核那些事(3.3)- 为AI,ML而生(M55)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是ARM Cortex-M55. 鼠年春节,大家都在时刻关心 2019nCoV 疫情发展,没太多心思搞技术,就在这个时候,ARM 不声不响 ...

  3. Notepad++ Plugin Manager更新插件出错

    作为墙内人士,更新个插件都TMD浪费很长时间! 解决方案: 定位文件notepaid++\updater\GUP.exe What is WinGup? --------------- WinGup ...

  4. python书籍推荐以及杂七杂八

    黄哥推荐学习Python 10本好书 Python3网络爬虫开发实战 github daily-reading 想对大家说的话 POJ 算法练习 http://openjudge.cn/ http:/ ...

  5. C# 接口和继承

    转:https://www.cnblogs.com/songhe123/p/9558545.html 接口是方法的抽象,如果不同的类有同样的方法,那么就应该考虑使用接口. 例1: using Syst ...

  6. Android Studio 学习笔记(三):简单控件及实例

    控件.组件.插件概念区分 说到控件,就不得不区分一些概念. 控件(Control):编程中用到的部件 组件(Component):软件的组成部分 插件(plugin): 应用程序中已经预留接口的组件 ...

  7. JUC中的锁

    ★.不同角度的锁的理解: #1.公平锁.非公平锁 公平锁:eg: ReentrantLock 关键词:先来先服务. 加锁前检查是否有排队等锁的线程,若有,当前线程参与排队,先排的线程优先获取锁.相对没 ...

  8. Go语言实现:【剑指offer】矩阵中的路径

    该题目来源于牛客网<剑指offer>专题. 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向 ...

  9. 在CentOS8 上安装Python3

    从centos开始入手学习linux.感觉安装python很费劲,之前centos6因为python2和python3兼容的问题一直无法彻底解决,python3一旦安装影响到python2,cento ...

  10. python上获得随机字符

    import random import string print(string.ascii_letters) # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNO ...