Let's say I want to handle an api endpoint /login, which gets a username&password body and returns a JWT token in a header.
What's the difference between using UsernamePasswordAuthenticationFilter with
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
try {
LoginDto loginDto = new ObjectMapper().readValue(request.getInputStream(), LoginDto.class);
return getAuthenticationManager().authenticate(
new UsernamePasswordAuthenticationToken(
loginDto.getUsername(),
loginDto.getPassword(),
Collections.emptyList()
)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
over providing my own @PostMapping("/login"), where I call a UserService myself?
What's the reason/advantage for calling a filter on a single endpoint?