How can I recover cookies when the user logs in through Spring Security and with the implementation of the AuthenticationProvider
interface? If I retrieve an instance of HttpServletRequest
from a class with a controller I have access to the Cookies, but doing so returns null
the attribute with Cookies:
Obs : I have Cookies set, they are displayed in the DevTools of Chrome.
Code :
@Component
public class LoginAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
/* Aqui tento recuperar os cookies */
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
/* Aqui tento recuperar os cookies */
String username = authentication.getName().toString();
String password = (String) authentication.getCredentials();
if (! AuthenticationConvert.validaSchema(username)) {
throw new SchemaNotFoundException();
}
SecurityContextHolder.getContext().setAuthentication(authentication);
Usuario usuario = usuarioService.findUsuario(AuthenticationConvert.getLogin(authentication.getName().toString()));
if (usuario == null)
throw new UsernameNotFoundException("Usuário não encontrado.");
if (! usuario.getSenha().equalsIgnoreCase(password))
throw new BadCredentialsException("Senha incorreta.");
return new UsernamePasswordAuthenticationToken(username, password, null);
}
}
Image :
Is there another way to recover cookies from an authentication? What?