Retrieve Cookies in Authentication with Spring Security

1

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?

    
asked by anonymous 16.12.2015 / 13:02

1 answer

0

It is possible to recover with an annotation, making binding through a parameter.

public void metodo(@CookieValue("nome") String cookie) {

}

To add you need a HttpServletResponse

public void metodo2(HttpServletResponse response) {
   Cookie cookie = new Cookie("chave", "valor");
   //configurações do cookie
   response.addCookie(cookie);
}

link

link

    
16.12.2015 / 13:20