Spring Cloud - 15. Users Microservice AuthenticationFilter

목차

15. Users Microservice AuthenticationFilter

@Data
public class RequestLogin {

@NotNull(message = "Email cannot be null")
@Size(min = 2, message = "Email not be less than two characters")
private String email;

@NotNull(message = "Password cannot be null")
@Size(min = 8, message = "Password must be equals or grater than 8 characters")
private String password;
}
public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter {
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException {

try {
RequestLogin creds = new ObjectMapper().readValue(request.getInputStream(), RequestLogin.class);

Authentication token =
new UsernamePasswordAuthenticationToken(creds.getEmail(), creds.getPassword(), new ArrayList<>());

return getAuthenticationManager().authenticate(token);
} catch (IOException e){
throw new RuntimeException(e);
}
}

@Override
protected void successfulAuthentication(HttpServletRequest request,
HttpServletResponse response,
FilterChain chain,
Authentication authResult) throws IOException, ServletException {
super.successfulAuthentication(request, response, chain, authResult);
}
}
Share