Spring Security:AuthenticationManager.authenticate()执行什么功能

时间:2018-09-04 20:13:53

标签: spring-boot spring-security

一段时间以来,我一直在研究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));
}

我的问题是authenticate方法做什么,为什么要使用它?

1 个答案:

答案 0 :(得分:2)

来自Spring Security Reference

  

AuthenticationManager只是一个接口,因此实现可以是我们选择的任何东西。 (...)Spring Security中的默认实现称为ProviderManager,它不处理身份验证请求本身,而是委派给已配置的AuthenticationProvider列表,依次查询每个AuthenticationProvider以确定其是否可以执行身份验证。每个提供程序将抛出异​​常或返回完全填充的Authentication对象。

相关问题