我一直在用JWT学习Spring安全性,我注意到在我读过的每个教程中都会使用用户名和密码,用UsernamePasswordAuthenticationToken包装并传递给AuthenticationManager.authenticate(),如下所示:
@RequestMapping(value = "${jwt.route.authentication.path}", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest) throws AuthenticationException {
authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword()));
// Reload password post-security so we can generate the token
final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
final String token = jwtTokenUtil.generateToken(userDetails);
// Return the token
return ResponseEntity.ok(new JwtAuthenticationResponse(token));
}
我的问题是验证方法做了什么,为什么使用它?
AuthenticationManager只是一个接口,因此实现可以是我们选择的任何东西。 (...)Spring Security中的默认实现称为ProviderManager,它不是处理身份验证请求本身,而是委托给已配置的AuthenticationProviders列表,每个身份验证器依次查询,以查看它是否可以执行身份验证。每个提供程序将抛出异常或返回完全填充的Authentication对象。