How can I authenticate a user immediately after registration?
Hi. I started studying Spring Security and ran into a problem - when a user registers, he does not log in (that is, he has to go through authorization). I searched for information on various forums and websites - it says that you need to use UsernamePasswordAuthenticationToken, AuthenticationManager and SecurityContext. The problem is that the user becomes anonymous when going to any page, which makes it necessary to log in. Is it really necessary to use this particular option? Are there any examples of proper usage? Are there any other ways?



7 Replies
⌛ This post has been reserved for your question.
Hey @JVyacheslav! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
No exceptions are caught
Use a bean for your brcriptpasswordencoder. You're initalizing one at each request
you could try setting
SecurityContextHolder.getContext().setAuthentication(...) when registering - though you might need to set a session attribute with the key SPRING_SECURITY_CONTEXT and the value being the SecurityContext@dan1st | Daniel Thank you, it helped a lot! ❤️
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@917367712525742090>.