Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting username in PasswordEncoder

I created my own authentication provider for my spring application, on which I specified the org.springframework.security.crypto.password.PasswordEncoder. I have my users stored in the database with their encrypted passwords (after a database algorithm). For being able to make the authentication, I would need to have access from the org.springframework.security.crypto.password.PasswordEncoder class to the username that is sent for authentication. Can anyone guide me how can I do this? Or is there any other approach?

N.B. I'm using SpringSecurity 3.2.

like image 355
artaxerxe Avatar asked Oct 20 '22 10:10

artaxerxe


1 Answers

I don't even care if it's an old question, I've just spent 9 hours trying to figure this out. Might as well leave it here in case someone else stumbles upon this again.

String username = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest().getParameter("username");

Using the code above I was able to extract the username from current request.

ATTENTION: You must use the following class org.springframework.web.context.request.RequestContextHolder

There was a similar one that was doing me wrong.

like image 172
Rodrigo Siqueira Avatar answered Oct 23 '22 01:10

Rodrigo Siqueira