Spring Security AuthenticationManager的authenticate()方法如何检查发送的用户名和密码是否正确?

And*_*ili 3 java spring spring-security spring-boot

我正在开发一个 Spring Boot 应用程序,该应用程序获取系统上现有用户的用户名和密码,然后生成 JWT 令牌。我从教程中复制了它,并对其进行了更改以适应我的特定用例。我对逻辑很清楚,但我对用户如何在系统上进行身份验证有很大疑问。接下来我将尝试向您解释这是结构化的以及我的疑问是什么。

JWT 生成代币系统由两个不同的微服务组成,分别是:

GET -USER-WS:该微服务简单地使用 Hibernate\JPA 来检索系统中特定用户的信息。基本上它包含一个调用服务类的控制器类,该服务类本身调用 JPA 存储库以检索特定的用户信息:

@RestController
@RequestMapping("api/users")
@Log
public class UserController {
    
    @Autowired
    UserService userService;
    
    @GetMapping(value = "/{email}", produces = "application/json")
    public ResponseEntity<User> getUserByEmail(@PathVariable("email") String eMail) throws NotFoundException  {
        
        log.info(String.format("****** Get the user with eMail %s *******", eMail) );
        
        User user = userService.getUserByEmail(eMail);
        
        if (user == null)
        {
            String ErrMsg = String.format("The user with eMail %s was not found", eMail);
            
            log.warning(ErrMsg);
            
            throw new NotFoundException(ErrMsg);
        }
            
            
        return new ResponseEntity<User>(user, HttpStatus.OK);
        
    }

}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,该控制器包含一个 API,该 API 使用电子邮件参数(即系统上的用户名)并返回包含该用户信息的 JSON。

然后,我有第二个微服务(名为AUTH-SERVER-JWT),它调用前一个 API 以获取将用于生成 JWT 令牌的用户信息。为了使描述尽可能简单,它包含以下控制器类:

@RestController
//@CrossOrigin(origins = "http://localhost:4200")
public class JwtAuthenticationRestController  {

    @Value("${sicurezza.header}")
    private String tokenHeader;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    @Qualifier("customUserDetailsService")
    //private UserDetailsService userDetailsService;
    private CustomUserDetailsService userDetailsService;
    
    private static final Logger logger = LoggerFactory.getLogger(JwtAuthenticationRestController.class);

    @PostMapping(value = "${sicurezza.uri}")
    public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtTokenRequest authenticationRequest)
            throws AuthenticationException {
        logger.info("Autenticazione e Generazione Token");

        authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());

        //final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
        final UserDetailsWrapper userDetailsWrapper = userDetailsService.loadCompleteUserByUsername(authenticationRequest.getUsername());

        final String token = jwtTokenUtil.generateToken(userDetailsWrapper);
        
        logger.warn(String.format("Token %s", token));

        return ResponseEntity.ok(new JwtTokenResponse(token));
    }

    @RequestMapping(value = "${sicurezza.uri}", method = RequestMethod.GET)
    public ResponseEntity<?> refreshAndGetAuthenticationToken(HttpServletRequest request) 
            throws Exception 
    {
        String authToken = request.getHeader(tokenHeader);
        
        if (authToken == null || authToken.length() < 7)
        {
            throw new Exception("Token assente o non valido!");
        }
        
        final String token = authToken.substring(7);
        
        if (jwtTokenUtil.canTokenBeRefreshed(token)) 
        {
            String refreshedToken = jwtTokenUtil.refreshToken(token);
            
            return ResponseEntity.ok(new JwtTokenResponse(refreshedToken));
        } 
        else 
        {
            return ResponseEntity.badRequest().body(null);
        }
    }

    @ExceptionHandler({ AuthenticationException.class })
    public ResponseEntity<String> handleAuthenticationException(AuthenticationException e) 
    {
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
    }

    private void authenticate(String username, String password) 
    {
        Objects.requireNonNull(username);
        Objects.requireNonNull(password);

        try {   
            /// ???
            authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
        } 
        catch (DisabledException e) 
        {
            logger.warn("UTENTE DISABILITATO");
            throw new AuthenticationException("UTENTE DISABILITATO", e);
        } 
        catch (BadCredentialsException e) 
        {
            logger.warn("CREDENZIALI NON VALIDE");
            throw new AuthenticationException("CREDENZIALI NON VALIDE", e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

该类包含两个方法,第一个用于生成全新的 JWT 令牌,第二个用于刷新现有的 JWT 令牌。现在考虑与createAuthenticationToken()方法相关的第一个用例(生成全新的 JWT 令牌)。此方法将与 JWT 令牌请求相关的信息作为输入参数:@RequestBody JwtTokenRequestauthenticationRequest。基本上JwtTokenRequest是一个简单的 DTO 对象,如下所示:

@Data
public class JwtTokenRequest implements Serializable 
{
    private static final long serialVersionUID = -3558537416135446309L;
    private String username;
    private String password;
}
Run Code Online (Sandbox Code Playgroud)

因此请求体中的负载将是这样的:

{
    "username": "xxx@gmail.com",
    "password": "password" 
}
Run Code Online (Sandbox Code Playgroud)

注意:在我的数据库中,我有一个具有此用户名和密码的用户,因此将在系统上检索该用户并对其进行身份验证。

正如您所看到的, createAuthenticationToken()方法执行的第一个有效操作是:

authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());
Run Code Online (Sandbox Code Playgroud)

基本上,它是调用同一类中定义的authenticate()方法,并将先前的凭据传递给它( “username”:“xxx@gmail.com”“password”:“password”)。

正如你所看到的,这是我的authenticate()方法

private void authenticate(String username, String password) 
{
    Objects.requireNonNull(username);
    Objects.requireNonNull(password);

    try {   
        /// ???
        authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
    } 
    catch (DisabledException e) 
    {
        logger.warn("UTENTE DISABILITATO");
        throw new AuthenticationException("UTENTE DISABILITATO", e);
    } 
    catch (BadCredentialsException e) 
    {
        logger.warn("CREDENTIAL ERROR");
        throw new AuthenticationException(""CREDENTIAL ERROR", e);
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,它将这些凭证传递给定义在注入的 Spring Security AuthenticationManager实例中的authenticate()方法,通过这一行:

authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
Run Code Online (Sandbox Code Playgroud)

此方法似乎能够验证或不验证这些凭据。它似乎工作正常,因为如果我输入了错误的用户名或密码,它就会进入CREDENTIAL ERROR情况并抛出AuthenticationException异常。

这是我最大的疑问:为什么它有效?!?!这怎么可能?如果您返回createAuthenticationToken()控制器方法,您可以看到它按以下顺序执行这两个操作:

authenticate(authenticationRequest.getUsername(), authenticationRequest.getPassword());

final UserDetailsWrapper userDetailsWrapper = userDetailsService.loadCompleteUserByUsername(authenticationRequest.getUsername());
Run Code Online (Sandbox Code Playgroud)

它首先执行authenticate()方法(应该检查发送的用户名和密码是否正确),然后调用检索用户信息的服务方法。

那么,authenticate()方法如何检查原始有效负载中发送的凭据是否正确?

Mar*_*gio 6

通常, 的实现AuthenticationManager是 a ProviderManager,它将循环遍历所有配置的AuthenticationProvider并尝试使用提供的凭据进行身份验证。

其中之一AuthenticationProviderDaoAuthenticationProvider,它支持UsernamePasswordAuthenticationToken并使用UserDetailsService(您有一个customUserDetailsService)来检索用户并password使用配置的进行比较PasswordEncoder

DaoAuthenticationProvider 图

有关身份验证架构的参考文档中有更详细的解释。